Contexts in rdflib
Friday, February 29th, 2008A short example…
-
from rdflib import Namespace, BNode, Literal, URIRef
-
from rdflib.Graph import Graph, ConjunctiveGraph
-
from rdflib.store.IOMemory import IOMemory
-
-
ns = Namespace("http://love.com#")
-
-
mary = URIRef("http://love.com/lovers/mary#")
-
john = URIRef("http://love.com/lovers/john#")
-
-
cmary=URIRef("http://love.com/lovers/mary#")
-
cjohn=URIRef("http://love.com/lovers/john#")
-
-
store = IOMemory()
-
-
g = ConjunctiveGraph(store=store)
-
g.bind("love",ns)
-
-
gmary = Graph(store=store, identifier=cmary)
-
-
gmary.add((mary, ns[‘hasName’], Literal("Mary")))
-
gmary.add((mary, ns[‘loves’], john))
-
-
gjohn = Graph(store=store, identifier=cjohn)
-
gjohn.add((john, ns[‘hasName’], Literal("John")))
-
-
#enumerate contexts
-
for c in g.contexts():
-
print "—- %s " % c
-
-
#separate graphs
-
print gjohn.serialize(format=‘n3′)
-
print gmary.serialize(format=‘n3′)
-
-
#full graph
-
print g.serialize(format=‘n3′)