RDF: The Resource Description Framework 

by Bob DuCharme 



Listing One



# rdf1.nt: sample RDF file in N-Triples format.



<http://level2.cap.gov/documents/u_090403105113.doc>

<http://purl.org/dc/elements/1.1/title>

"MISHAP REPORT FORM".



<http://level2.cap.gov/documents/u_090403105113.doc>

<http://purl.org/dc/elements/1.1/format>

"Microsoft Word 97".



<http://level2.cap.gov/index.cfm?nodeID=5464>

<http://www.snee.com/ns/rel#linksTo> 

<http://level2.cap.gov/documents/u_090403105113.doc>.





Listing Two



<rdf:RDF xmlns:sn="http://www.snee.com/ns/rel#"

         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

         xmlns:dc="http://purl.org/dc/elements/1.1/">

  <rdf:Description rdf:about=

             "http://level2.cap.gov/documents/u_090403105113.doc">

    <dc:title>MISHAP REPORT FORM</dc:title>

    <format xmlns=

             "http://purl.org/dc/elements/1.1/">Microsoft Word 97</format>

  </rdf:Description>

  <rdf:Description rdf:about="http://level2.cap.gov/index.cfm?nodeID=5464">

    <sn:linksTo rdf:resource=

             "http://level2.cap.gov/documents/u_090403105113.doc"/>

  </rdf:Description>

</rdf:RDF>





Listing Three



# This is an n3 comment line.

@prefix : <http://snee.com/ns/rel#> .

@prefix dc: <http://purl.org/dc/elements/1.1/> .

<http://level2.cap.gov/documents/u_090403105113.doc> 

  dc:format "Microsoft Word 97";

  dc:title "MISHAP REPORT FORM" .

<http://level2.cap.gov/index.cfm?nodeID=5464>    

  :linksTo <http://level2.cap.gov/documents/u_090403105113.doc> .





Listing Four



#! /usr/bin/python

# addRDF2DB.py: add triples from XML file to disk-based store.

import sys



# Import RDFLib's InformationStore (a Sleepycat BTree backed

# TripleStore with contexts).

from rdflib.InformationStore import InformationStore as TripleStore



if len(sys.argv) != 3:

  print "Enter\n\n   addRDF2DB.py filename.rdf dbName\n"

  print "to add triples from filename.rdf to Sleepycat database dbName."

  sys.exit()



RDFfile = sys.argv[1]

db = sys.argv[2]

store = TripleStore()

store.open(db)

try:

    store.load(RDFfile)

    print RDFfile + " successfully loaded to " + db + "."

except:

    print "Problem parsing " + RDFfile + "."

store.close()





Listing Five



#! /usr/bin/python

# getSubjectData.py: list triples for supplied subject.

import sys



# Import RDFLib's InformationStore (a Sleepycat BTree backed

# TripleStore with contexts).

from rdflib.InformationStore import InformationStore as TripleStore

from rdflib.URIRef import URIRef



if len(sys.argv) != 3:

  print "Enter\n\n   getSubjectData.py dbName URI\n"

  print "to get data for the subject identified by URI "

  print "from the Sleepycat database dbName."

  sys.exit()

db = sys.argv[1]

subjectURI = sys.argv[2]

store = TripleStore()

store.open(db)

for docInfo in store.predicate_objects(URIRef(subjectURI)):

  print docInfo

store.close()











2



