rdflib.Graph.ConjunctiveGraph – ConjunctiveGraph class definition

Conjunctive Graph

Instantiating Graphs with default store (IOMemory) and default identifier (a BNode):

>>> g=Graph()
>>> g.store.__class__
<class 'rdflib.store.IOMemory.IOMemory'>
>>> g.identifier.__class__
<class 'rdflib.term.BNode'>

Instantiating Graphs with a specific kind of store (IOMemory) and a default identifier (a BNode):

Other store kinds: Sleepycat, MySQL, ZODB, SQLite

>>> store = plugin.get('IOMemory',Store)()
>>> store.__class__.__name__
'IOMemory'
>>> graph = Graph(store)
>>> graph.store.__class__
<class 'rdflib.store.IOMemory.IOMemory'>

Instantiating Graphs with Sleepycat store and an identifier - <http://rdflib.net>:

>>> g=Graph('IOMemory',URIRef("http://rdflib.net"))
>>> g.identifier
rdflib.term.URIRef('http://rdflib.net')
>>> str(g)
"<http://rdflib.net> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory']."

Creating a ConjunctiveGraph - The top level container for all named Graphs in a ‘database’:

>>> g=ConjunctiveGraph()
>>> str(g.default_context)
"[a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory']]."

Adding / removing reified triples to Graph and iterating over it directly or via triple pattern:

>>> g=Graph('IOMemory')
>>> statementId = BNode()
>>> print len(g)
0
>>> g.add((statementId,RDF.type,RDF.Statement))
>>> g.add((statementId,RDF.subject,URIRef('http://rdflib.net/store/ConjunctiveGraph')))
>>> g.add((statementId,RDF.predicate,RDFS.label))
>>> g.add((statementId,RDF.object,Literal("Conjunctive Graph")))
>>> print len(g)
4
>>> for s,p,o in g:  print type(s)
...
<class 'rdflib.term.BNode'>
<class 'rdflib.term.BNode'>
<class 'rdflib.term.BNode'>
<class 'rdflib.term.BNode'>
>>> for s,p,o in g.triples((None,RDF.object,None)):  print o
...
Conjunctive Graph
>>> g.remove((statementId,RDF.type,RDF.Statement))
>>> print len(g)
3

None terms in calls to triple can be thought of as ‘open variables’

Graph Aggregation - ConjunctiveGraphs and ReadOnlyGraphAggregate within the same store:

>>> store = plugin.get('IOMemory',Store)()
>>> g1 = Graph(store)
>>> g2 = Graph(store)
>>> g3 = Graph(store)
>>> stmt1 = BNode()
>>> stmt2 = BNode()
>>> stmt3 = BNode()
>>> g1.add((stmt1,RDF.type,RDF.Statement))
>>> g1.add((stmt1,RDF.subject,URIRef('http://rdflib.net/store/ConjunctiveGraph')))
>>> g1.add((stmt1,RDF.predicate,RDFS.label))
>>> g1.add((stmt1,RDF.object,Literal("Conjunctive Graph")))
>>> g2.add((stmt2,RDF.type,RDF.Statement))
>>> g2.add((stmt2,RDF.subject,URIRef('http://rdflib.net/store/ConjunctiveGraph')))
>>> g2.add((stmt2,RDF.predicate,RDF.type))
>>> g2.add((stmt2,RDF.object,RDFS.Class))
>>> g3.add((stmt3,RDF.type,RDF.Statement))
>>> g3.add((stmt3,RDF.subject,URIRef('http://rdflib.net/store/ConjunctiveGraph')))
>>> g3.add((stmt3,RDF.predicate,RDFS.comment))
>>> g3.add((stmt3,RDF.object,Literal("The top-level aggregate graph - The sum of all named graphs within a Store")))
>>> len(list(ConjunctiveGraph(store).subjects(RDF.type,RDF.Statement)))
3
>>> len(list(ReadOnlyGraphAggregate([g1,g2]).subjects(RDF.type,RDF.Statement)))
2

ConjunctiveGraphs have a ‘quads’ method which returns quads instead of triples, where the fourth item is the Graph (or subclass thereof) instance in which the triple was asserted:

>>> uniqueGraphNames = set([graph.identifier for s,p,o,graph in ConjunctiveGraph(store).quads((None,RDF.predicate,None))])
>>> len(uniqueGraphNames)
3
>>> unionGraph = ReadOnlyGraphAggregate([g1,g2])
>>> uniqueGraphNames = set([graph.identifier for s,p,o,graph in unionGraph.quads((None,RDF.predicate,None))])
>>> len(uniqueGraphNames)
2

Parsing N3 from StringIO

>>> g2=Graph()
>>> src = """
... @prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
... @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
... [ a rdf:Statement ;
...   rdf:subject <http://rdflib.net/store#ConjunctiveGraph>;
...   rdf:predicate rdfs:label;
...   rdf:object "Conjunctive Graph" ] """
>>> g2=g2.parse(StringIO(src),format='n3')
>>> print len(g2)
4

Using Namespace class:

>>> RDFLib = Namespace('http://rdflib.net')
>>> RDFLib.ConjunctiveGraph
rdflib.term.URIRef('http://rdflib.netConjunctiveGraph')
>>> RDFLib['Graph']
rdflib.term.URIRef('http://rdflib.netGraph')

SPARQL Queries

>>> print len(g)
3
>>> q = '''
... PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?pred WHERE { ?stmt rdf:predicate ?pred. }
... '''   
>>> for pred in g.query(q):  print pred
(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),)

SPARQL Queries with namespace bindings as argument

>>> nsMap = {u"rdf":RDF.RDFNS}
>>> for pred in g.query("SELECT ?pred WHERE { ?stmt rdf:predicate ?pred. }", initNs=nsMap): print pred
(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),)

Parameterized SPARQL Queries

>>> from rdflib.term import Variable
>>> top = { Variable("?term") : RDF.predicate }
>>> for pred in g.query("SELECT ?pred WHERE { ?stmt ?term ?pred. }", initBindings=top): print pred
(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),)

Module Contents

class rdflib.graph.ConjunctiveGraph(store='default', identifier=None)
add((s, p, o))
Add the triple to the default context
addN(quads)
Add a sequence of triples with context
context_id(uri, context_id=None)
URI#context
contexts(triple=None)

Iterate over all contexts in the graph

If triple is specified, iterate over all contexts the triple is in.

get_context(identifier, quoted=False)

Return a context graph for the given identifier

identifier must be a URIRef or BNode.

parse(source=None, publicID=None, format='xml', location=None, file=None, data=None, **args)

Parse source adding the resulting triples to it’s own context (sub graph of this graph).

See rdflib.graph.Graph.parse for documentation on arguments.

Returns:

The graph into which the source was parsed. In the case of n3 it returns the root context.

quads((s, p, o))
Iterate over all the quads in the entire conjunctive graph
remove((s, p, o))
Removes from all its contexts
remove_context(context)
Removes the given context from the graph
triples((s, p, o), context=None)
Iterate over all the triples in the entire conjunctive graph
triples_choices((s, p, o))
Iterate over all the triples in the entire conjunctive graph