diff options
Diffstat (limited to 'examples/conjunctive_graphs.py')
-rw-r--r-- | examples/conjunctive_graphs.py | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/examples/conjunctive_graphs.py b/examples/conjunctive_graphs.py index 216fa015..892582a0 100644 --- a/examples/conjunctive_graphs.py +++ b/examples/conjunctive_graphs.py @@ -1,37 +1,58 @@ -from rdflib import Namespace, BNode, Literal, URIRef +""" + +An RDFLib ConjunctiveGraph is an (unamed) aggregation of all the named graphs +within a Store. The :meth:`~rdflib.graph.ConjunctiveGraph.get_context` +method can be used to get a particular named graph, or triples can be +added to the default graph + +This example shows how to create some named graphs and work with the +conjunction of all the graphs. + +""" + +from rdflib import Namespace, Literal, URIRef from rdflib.graph import Graph, ConjunctiveGraph from rdflib.plugins.memory import IOMemory -ns = Namespace("http://love.com#") +if __name__=='__main__': + + + 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#") -mary = URIRef("http://love.com/lovers/mary#") -john = URIRef("http://love.com/lovers/john#") + store = IOMemory() -cmary=URIRef("http://love.com/lovers/mary#") -cjohn=URIRef("http://love.com/lovers/john#") + g = ConjunctiveGraph(store=store) + g.bind("love",ns) -store = IOMemory() + gmary = Graph(store=store, identifier=cmary) -g = ConjunctiveGraph(store=store) -g.bind("love",ns) + gmary.add((mary, ns['hasName'], Literal("Mary"))) + gmary.add((mary, ns['loves'], john)) -gmary = Graph(store=store, identifier=cmary) + gjohn = Graph(store=store, identifier=cjohn) + gjohn.add((john, ns['hasName'], Literal("John"))) -gmary.add((mary, ns['hasName'], Literal("Mary"))) -gmary.add((mary, ns['loves'], john)) + #enumerate contexts + for c in g.contexts(): + print("-- %s " % c) -gjohn = Graph(store=store, identifier=cjohn) -gjohn.add((john, ns['hasName'], Literal("John"))) + #separate graphs + print(gjohn.serialize(format='n3')) + print("===================") + print(gmary.serialize(format='n3')) + print("===================") -#enumerate contexts -for c in g.contexts(): - print("-- %s " % c) + #full graph + print(g.serialize(format='n3')) -#separate graphs -print(gjohn.serialize(format='n3')) -print("===================") -print(gmary.serialize(format='n3')) -print("===================") + # query the conjunction of all graphs -#full graph -print(g.serialize(format='n3')) + print 'Mary loves:' + for x in g[mary : ns.loves/ns.hasName]: + print x |