Package rdfalchemy :: Package sparql :: Class SPARQLGraph
[hide private]
[frames] | no frames]

Class SPARQLGraph

source code

object --+
         |
        SPARQLGraph
Known Subclasses:

provides (some) rdflib api via http to a SPARQL endpoint gives 'read-only' access to the graph constructor takes http endpoint and repository name e.g. SPARQLGraph('http://localhost:2020/sparql')
Instance Methods [hide private]
 
__init__(self, url, context=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
construct(self, strOrTriple, initBindings={}, initNs={})
Executes a SPARQL Construct :param strOrTriple: can be either
source code
 
triples(self, (s, p, o), method='CONSTRUCT')
Use SELECT if you expect a large result set or may consume less than the entire result
source code
 
__iter__(self)
Iterates over all triples in the store
source code
 
__contains__(self, triple)
Support for 'triple in graph' syntax
source code
 
subjects(self, predicate=None, object=None)
A generator of subjects with the given predicate and object
source code
 
predicates(self, subject=None, object=None)
A generator of predicates with the given subject and object
source code
 
objects(self, subject=None, predicate=None)
A generator of objects with the given subject and predicate
source code
 
subject_predicates(self, object=None)
A generator of (subject, predicate) tuples for the given object
source code
 
subject_objects(self, predicate=None)
A generator of (subject, object) tuples for the given predicate
source code
 
predicate_objects(self, subject=None)
A generator of (predicate, object) tuples for the given subject
source code
 
value(self, subject=None, predicate=rdflib.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#value'), object=None, default=None, any=True)
Get a value for a pair of two criteria
source code
 
label(self, subject, default='')
Query for the RDFS.label of the subject
source code
 
comment(self, subject, default='')
Query for the RDFS.comment of the subject
source code
 
items(self, list)
Generator over all items in the resource specified by list
source code
 
transitive_objects(self, subject, property, remember=None)
Transitively generate objects for the property relationship
source code
 
transitive_subjects(self, predicate, object, remember=None)
Transitively generate objects for the property relationship
source code
 
qname(self, uri)
turn uri into a qname given self.namespaces This works for rdflib graphs and is defined for SesameGraph but is not part of SPARQLGraph
source code
 
query(self, strOrQuery, initBindings={}, initNs={}, resultMethod='xml', processor='sparql', rawResults=False)
Executes a SPARQL query against this Graph
source code
 
getParser(self, resultMethod, url) source code
 
describe(self, s_or_po, initBindings={}, initNs={})
Executes a SPARQL describe of resource
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Class Methods [hide private]
 
_processInitBindings(cls, query, initBindings)
_processInitBindings will convert a query by replacing the Variables
source code
Class Variables [hide private]
  parsers = {'json': <class 'rdfalchemy.sparql.parsers._JSONSPAR...
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, url, context=None)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

construct(self, strOrTriple, initBindings={}, initNs={})

source code 

Executes a SPARQL Construct :param strOrTriple: can be either

  • a string in which case it it considered a CONSTRUCT query
  • a triple in which case it acts as the rdflib triples((s,p,o))
Parameters:
  • 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 a namespace
Returns:
an instance of rdflib.ConjuctiveGraph('IOMemory')

triples(self, (s, p, o), method='CONSTRUCT')

source code 
Use SELECT if you expect a large result set or may consume less than the entire result
Parameters:
  • method - must be 'CONSTRUCT' or 'SELECT'

    • CONSTRUCT calls CONSTRUCT query and returns a Graph result
    • SELECT calls a SELECT query and returns an interator streaming over the results
Returns:
a generator over triples matching the pattern

value(self, subject=None, predicate=rdflib.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#value'), object=None, default=None, any=True)

source code 

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 more than one answer return any one answer, otherwise raise UniquenessError

label(self, subject, default='')

source code 

Query for the RDFS.label of the subject

Return default if no label exists

comment(self, subject, default='')

source code 

Query for the RDFS.comment of the subject

Return default if no comment exists

items(self, list)

source code 

Generator over all items in the resource specified by list

list is an RDF collection.

transitive_objects(self, subject, property, remember=None)

source code 

Transitively generate objects for the property relationship

Generated objects belong to the depth first transitive closure of the property relationship starting at subject.

transitive_subjects(self, predicate, object, remember=None)

source code 

Transitively generate objects for the property relationship

Generated objects belong to the depth first transitive closure of the property relationship starting at subject.

query(self, strOrQuery, initBindings={}, initNs={}, resultMethod='xml', processor='sparql', rawResults=False)

source code 
Executes a SPARQL query against this Graph
Parameters:
  • strOrQuery - Is either a string consisting of the SPARQL query
  • initBindings - optional mapping from a Variable to an RDFLib term (used as initial bindings for SPARQL query)
  • initNs - optional mapping from a namespace prefix to a namespace
  • resultMethod - results query requested (must be 'xml' or 'json') xml streams over the result set and json must read the entire set to succeed
  • processor - The kind of RDF query (must be 'sparql' or 'serql')
  • rawResults - If set to True, returns the raw xml or json stream rather than the parsed results.

_processInitBindings(cls, query, initBindings)
Class Method

source code 

_processInitBindings will convert a query by replacing the Variables

>>> SPARQLGraph._processInitBindings('SELECT ?x { ?x ?y ?z }', {'z' : 'hi'})
u'SELECT ?x { ?x ?y "hi" }'
>>> SPARQLGraph._processInitBindings('SELECT ?x { ?x <http://example/?z=1> ?z }', {'z' : 'hi'})
u'SELECT ?x { ?x <http://example/?z=1> "hi" }'
Parameters:
  • query - the query to process
  • initBindings - a dict of variable to value

describe(self, s_or_po, initBindings={}, initNs={})

source code 
Executes a SPARQL describe of resource
Parameters:
  • s_or_po - is either

    • a subject ... should be a URIRef
    • a tuple of (predicate,object) ... pred should be inverse functional
    • a describe query string
  • 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 a namespace

Class Variable Details [hide private]

parsers

Value:
{'json': <class 'rdfalchemy.sparql.parsers._JSONSPARQLHandler'>,
 'xml': <class 'rdfalchemy.sparql.parsers._XMLSPARQLHandler'>}