Main Heading

Node()

Nodes are the smallest atomic parts of an RDF system. They come in four different flavours: URI, bNode, Literal, and Var. Each of these are implemented as sub classes of Node. The Node class will always return one of its four sub classes upon instantiation, and never just a node.

For example:-

>>> from rdf import Node
>>> n = Node('<http://www.w3.org/>')
>>> n
<http://www.w3.org/>
>>> type(n)
<class 'rdf.URI'>
>>> isinstance(n, Node)
True
>>> 

A Node will always have lang, dtype, string, and value properties.

>>> n.lang, n.dtype, n.string, n.value
(None, None, 'http://www.w3.org/', 'http://www.w3.org/')
>>> 

Node must be passed a single N-Triples encoded term; its sub classes take unencoded terms.

>>> from rdf import Node, URI
>>> URI('http://www.w3.org/')
<http://www.w3.org/>
>>> Node('<http://www.w3.org/>') == URI('http://www.w3.org/')
True
>>> 

Here's an example of what each of the sub classes of Node produce:-

>>> URI('http://www.w3.org/')
<http://www.w3.org/>
>>> bNode('blargh')
_:blargh
>>> Var('blargh')
?blargh
>>> Literal('blargh')
"blargh"
>>> 

Note again that they produce literals which are valid N-Triples terms, except for Var, which is an extension borrowed from N3 and RDQL for the purposes of querying.

Triple()

A triple is simply an immutable ordered list of three Node instances. It is derived from tuple. Example:-

>>> from rdf import Node, Triple
>>> subj = Node('<http://example.org/>')
>>> pred = Node('<http://ns.example.org/#title>')
>>> objt = Node('"Example Dot Org"')
>>> Triple(subj, pred, objt)
(<http://example.org/>, <http://ns.example.org/#title>, "Example Dot Org")
>>> 

Passing anything which isn't a Node will raise an error. Passing anything other than 3 arguments will raise an error.

>>> Triple('x', 'y', 'z')

Traceback (most recent call last):
  [...]
AssertionError
>>> Triple(subj)

Traceback (most recent call last):
  [...]
NonTripleError: Not a triple: 
>>> 

There's a single exception: Triple will happily take three Nodes in a list or tuple as a single argument.

>>> Triple([subj, pred, objt])
(<http://example.org/>, <http://ns.example.org/#title>, "Example Dot Org")
>>> 

It's helpful like that.

Graph()

Graph corresponds to the graph concept in RDF, i.e. a collection of triples. In other RDF APIs, Graphs are also known as stores, formulae, contexts, worlds, lists, etc.

Graphs can be created with no args.

>>> from rdf import Graph
>>> Graph()
[]
>>> 

Graph is derived from list, hence the representation. Creating an empty Graph as above is not very interesting, so we can get it to parse some N-Triples by passing them using an ntriples keyword arg.

>>> Graph(ntriples='_:p <q:r> "s" .')
[(_:p, , "s")]
>>> 

Graphs can be merged simply by adding them.

>>> F = Graph(ntriples='_:x _:y "z" .')
>>> G = Graph(ntriples='_:p <q:r> "s" .')
>>> H = F + G
>>> len(H)
2
>>> 

Note that bNode and Var labels are renamed in all merges to prevent collisions.

>>> H
[(_:p, <q:r>, "s"), (_:id176100372x, _:id176100372y, "z")]

The scary-looking ids are to prevent merge collisions. The hash of the graph being merged is used. Since Graphs have hashes, it means that they can be compared; this is graph isomorphism testing.

>>> G.hash()
182181844
>>> F == G
False
>>> 

Example Application

[ rdf:type :Major, :Project; 
  :date "2003-05"; 
  :name "The Splunge Project"; 
  :homepage <http://example.org/splunge> ] .

[ rdf:type :Major, :Project; 
  :date "2003-05"; 
  :name "Project Plimpy"; 
  :homepage <http://example.org/plimpy> ] .

[ rdf:type :Minor, :Project; 
  :date "2003-05"; 
  :name "The Flimble Project"; 
  :homepage <http://example.org/flimble> ] .

To print...

G = Graph(uri="projects.rdf")

li = '<li><a href="{%s}">{%s}</a></li>'
print '<h2>Major Projects</h2>'
print '<ul>'
for project in G.getPath("*/rdf:type/:Major", sortBy="*/:date"): 
   project.print(G, li % ("*/:homepage/*", "*/:name/*"))
print '</ul>'

print '<h2>Minor Projects</h2>'
print '<ul>'
for project in G.getPath("*/rdf:type/:Minor", sortBy="*/:date"): 
   project.print(G, li % ("*/:homepage/*", "*/:name/*"))
print '</ul>'