An RDF Graph
The constructor accepts one argument, the ‘store’ that will be used to store the graph data (see the ‘store’ package for stores currently shipped with rdflib).
Stores can be context-aware or unaware. Unaware stores take up (some) less space but cannot support features that require context, such as true merging/demerging of sub-graphs and provenance.
The Graph constructor can take an identifier which identifies the Graph by name. If none is given, the graph is assigned a BNode for its identifier. For more on named graphs, see: http://www.w3.org/2004/03/trix/
Ontology for __str__ provenance terms:
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://rdflib.net/store#> .
@prefix rdfg: <http://www.w3.org/2004/03/trix/rdfg-1/>.
@prefix owl: <http://www.w3.org/2002/07/owl#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
:Store a owl:Class;
rdfs:subClassOf <http://xmlns.com/wordnet/1.6/Electronic_database>;
rdfs:subClassOf
[a owl:Restriction;
owl:onProperty rdfs:label;
owl:allValuesFrom [a owl:DataRange;
owl:oneOf ("IOMemory"
"Sleepcat"
"MySQL"
"Redland"
"REGEXMatching"
"ZODB"
"AuditableStorage"
"Memory")]
].
:ConjunctiveGraph a owl:Class;
rdfs:subClassOf rdfg:Graph;
rdfs:label "The top-level graph within the store - the union of all the Graphs within."
rdfs:seeAlso <http://rdflib.net/rdf_store/#ConjunctiveGraph>.
:DefaultGraph a owl:Class;
rdfs:subClassOf rdfg:Graph;
rdfs:label "The 'default' subgraph of a conjunctive graph".
:identifier a owl:Datatypeproperty;
rdfs:label "The store-associated identifier of the formula. ".
rdfs:domain log:Formula
rdfs:range xsd:anyURI;
:storage a owl:ObjectProperty;
rdfs:domain [
a owl:Class;
owl:unionOf (log:Formula rdfg:Graph :ConjunctiveGraph)
];
rdfs:range :Store.
:default_context a owl:FunctionalProperty;
rdfs:label "The default context for a conjunctive graph";
rdfs:domain :ConjunctiveGraph;
rdfs:range :DefaultGraph.
{?cg a :ConjunctiveGraph;:storage ?store}
=> {?cg owl:sameAs ?store}.
{?subGraph rdfg:subGraphOf ?cg;a :DefaultGraph}
=> {?cg a :ConjunctiveGraph;:default_context ?subGraphOf} .
Bind prefix to namespace
If override is True will bind namespace to given prefix if namespace was already bound to a different prefix.
Close the graph store
Might be necessary for stores that require closing a connection to a database or releasing some resource.
Query for the RDFS.comment of the subject
Return default if no comment exists
Check if the Graph is connected
The Graph is considered undirectional.
Performs a search on the Graph, starting from a random node. Then iteratively goes depth-first through the triplets where the node is subject and object. Return True if all nodes have been visited and False if it cannot continue and there are still unvisited nodes left.
Generator over all items in the resource specified by list
list is an RDF collection.
Query for the RDFS.label of the subject
Return default if no label exists
Open the graph store
Might be necessary for stores that require opening a connection to a database or acquiring some resource.
Parse source adding the resulting triples to the Graph.
The source is specified using one of source, location, file or data.
Parameters: |
|
---|---|
Returns: |
self, the graph instance.
Examples:
>>> my_data = '''
... <rdf:RDF
... xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
... xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'
... >
... <rdf:Description>
... <rdfs:label>Example</rdfs:label>
... <rdfs:comment>This is really just an example.</rdfs:comment>
... </rdf:Description>
... </rdf:RDF>
... '''
>>> import tempfile
>>> file_name = tempfile.mktemp()
>>> f = file(file_name, "w")
>>> f.write(my_data)
>>> f.close()
>>> g = Graph()
>>> result = g.parse(data=my_data, format="application/rdf+xml")
>>> len(g)
2
>>> g = Graph()
>>> result = g.parse(location=file_name, format="application/rdf+xml")
>>> len(g)
2
>>> g = Graph()
>>> result = g.parse(file=file(file_name, "r"), format="application/rdf+xml")
>>> len(g)
2
Executes a SPARQL query (eventually will support Versa queries with same method) against this Graph.
- strOrQuery: Either a string consisting of the SPARQL query or
an instance of rdflib.sparql.bison.Query.Query
- initBindings: A mapping from a Variable to an RDFLib term (used
as initial bindings for SPARQL query)
- initNS: A mapping from a namespace prefix to an instance of
rdflib.Namespace (used for SPARQL query)
- DEBUG: A boolean flag passed on to the SPARQL parser and
evaluation engine
- processor: The kind of RDF query (must be ‘sparql’ until Versa
is ported)
- USE_PYPARSING: A flag indicating whether to use the
experimental pyparsing parser for SPARQL
Remove a triple from the graph
If the triple does not provide a context attribute, removes the triple from all contexts.
Check if subject is an rdf:Seq
If yes, it returns a Seq class instance, None otherwise.
Serialize the Graph to destination
If destination is None serialize method returns the serialization as a string. Format defaults to xml (AKA rdf/xml).
Convenience method to update the value of object
Remove any existing triples for subject and predicate before adding (subject, predicate, object).
Generates transitive closure of a user-defined function against the graph
>>> from rdflib.collection import Collection
>>> g=Graph()
>>> a=BNode('foo')
>>> b=BNode('bar')
>>> c=BNode('baz')
>>> g.add((a,RDF.first,RDF.type))
>>> g.add((a,RDF.rest,b))
>>> g.add((b,RDF.first,RDFS.label))
>>> g.add((b,RDF.rest,c))
>>> g.add((c,RDF.first,RDFS.comment))
>>> g.add((c,RDF.rest,RDF.nil))
>>> def topList(node,g):
... for s in g.subjects(RDF.rest,node):
... yield s
>>> def reverseList(node,g):
... for f in g.objects(node,RDF.first):
... print f
... for s in g.subjects(RDF.rest,node):
... yield s
>>> [rt for rt in g.transitiveClosure(topList,RDF.nil)]
[rdflib.term.BNode('baz'), rdflib.term.BNode('bar'), rdflib.term.BNode('foo')]
>>> [rt for rt in g.transitiveClosure(reverseList,RDF.nil)]
http://www.w3.org/2000/01/rdf-schema#comment
http://www.w3.org/2000/01/rdf-schema#label
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
[rdflib.term.BNode('baz'), rdflib.term.BNode('bar'), rdflib.term.BNode('foo')]
Transitively generate objects for the property relationship
Generated objects belong to the depth first transitive closure of the property relationship starting at subject.
Transitively generate objects for the property relationship
Generated objects belong to the depth first transitive closure of the property relationship starting at subject.
Generator over the triple store
Returns triples that match the given triple pattern. If triple pattern does not provide a context, all contexts will be searched.
Get a value for a pair of two criteria
Exactly one of subject, predicate, object must be None. Useful if one knows that there may only be one value.
It is one of those situations that occur a lot, hence this ‘macro’ like utility
Parameters: |
---|
subject, predicate, object – exactly one must be None default – value to be returned if no values found any – if True: return any value in the case there is more than one
else: raise UniquenessError