Package rdflib :: Package sparql :: Module sparql :: Class _SPARQLNode
[show private | hide private]
[frames | no frames]

Class _SPARQLNode


The SPARQL implementation is based on the creation of a tree, each level for each statement in the 'where' clause of SPARQL.

Each node maintains a 'binding' dictionary, with the variable names and either a None if not yet bound, or the binding itself. The method 'expand' tries to make one more step of binding by looking at the next statement: it takes the statement of the current node, binds the variables if there is already a binding, and looks at the triple store for the possibilities. If it finds valid new triplets, that will bind some more variables, and children will be created with the next statement in the 'where' array with a new level of bindings. This is done for each triplet found in the store, thereby branching off the tree. If all variables are already bound but the statement, with the bound variables, is not 'true' (ie, there is no such triple in the store), the node is marked as 'clash' and no more expansion is made; this node will then be thrown away by the parent. If all children of a node is a clash, then it is marked as a clash itself.

At the end of the process, the leaves of the tree are searched; if a leaf is such that:

then the bindings are returned as possible answers to the query.

The optional clauses are treated separately: each 'valid' leaf is assigned an array of expansion trees that contain the optional clauses (that may have some unbound variables bound at the leaf, though).
Method Summary
  __init__(self, parent, bindings, statements, tripleStore)
  _bind(self, r)
Return returns None if no bindings occured yet, the binding otherwise
  expand(self, constraints)
The expansion itself.
  expandOptions(self, bindings, statements, constraints)
Managing optional statements.
  expandSubgraph(self, subTriples, pattern)
Method used to collect the results.
  returnResult(self, select)
Collect the result by search the leaves of the the tree.

Instance Variable Summary
dictionary bindings: copy of the bindings locally
Boolean bound: True or False depending on whether all variables are bound in self.binding
array of _SPARQLNode children: the children (in an array)
Boolean clash: intialized to False
array of _SPARQLNode instances optionalTrees: expansion trees for optional statements
_SPARQLNode parent: parent in the tree
  rest: the rest of the statements (an array)
a (s,p,o,f) tuple ('f' is the local filter or None) statement: the current statement

Method Details

__init__(self, parent, bindings, statements, tripleStore)
(Constructor)

Parameters:
parent - parent node
bindings - a dictionary with the bindings that are already done or with None value if no binding yet
statements - array of statements from the 'where' clause. The first element is for the current node, the rest for the children. If empty, then no expansion occurs (ie, the node is a leaf)
tripleStore - the 'owner' triple store
           (type=myTripleStore)

_bind(self, r)

Parameters:
r - string
Returns:
returns None if no bindings occured yet, the binding otherwise

expand(self, constraints)

The expansion itself. See class comments for details.
Parameters:
constraints - array of global constraining (filter) methods

expandOptions(self, bindings, statements, constraints)

Managing optional statements. These affect leaf nodes only, if they contain 'real' results. A separate Expansion tree is appended to such a node, one for each optional call.
Parameters:
bindings - current bindings dictionary
statements - array of statements from the 'where' clause. The first element is for the current node, the rest for the children. If empty, then no expansion occurs (ie, the node is a leaf). The bindings at this node are taken into account (replacing the unbound variables with the real resources) before expansion
constraints - array of constraint (filter) methods

expandSubgraph(self, subTriples, pattern)

Method used to collect the results. There are two ways to invoke the method:
  • if the pattern argument is not None, then this means the construction of a separate triple store with the results. This means taking the bindings in the node, and constuct the graph via the construct method. This happens on the valid leafs; intermediate nodes call the same method recursively
  • otherwise, a leaf returns an array of the bindings, and intermediate methods aggregate those.
In both cases, leaf nodes may successifely expand the optional trees that they may have.
Parameters:
subTriples - the triples so far
           (type=myTripleStore)
pattern - a graph pattern used to construct a graph
           (type=GraphPattern)
Returns:
if pattern is not None, an array of binding dictionaries

returnResult(self, select)

Collect the result by search the leaves of the the tree. The variables in the select are exchanged against their bound equivalent (if applicable). This action is done on the valid leaf nodes only, the intermediate nodes only gather the children's results and combine it in one array.
Parameters:
select - the array of unbound variables in the original select that do not appear in any of the optionals. If None, the full binding should be considered (this is the case for the SELECT * feature of SPARQL)
Returns:
an array of dictionaries with non-None bindings.

Instance Variable Details

bindings

copy of the bindings locally
Type:
dictionary

bound

True or False depending on whether all variables are bound in self.binding
Type:
Boolean

children

the children (in an array)
Type:
array of _SPARQLNode

clash

intialized to False
Type:
Boolean

optionalTrees

expansion trees for optional statements
Type:
array of _SPARQLNode instances

parent

parent in the tree
Type:
_SPARQLNode

rest

the rest of the statements (an array)

statement

the current statement
Type:
a (s,p,o,f) tuple ('f' is the local filter or None)

Generated by Epydoc 2.1 on Wed Jun 29 09:50:16 2005 http://epydoc.sf.net