GraphPath Reference

GraphPath expressions operate on Directed, Labeled Graphs (DLG's) including RDF graphs.

A GraphPath expression consists of steps connected by operators. This section lists all the GraphPath steps and operators.

Each GraphPath expression matches zero or more paths in a graph. A path is a part of the graph consisting of a initial node, a sequence of zero or more adjacent arcs, and a terminal node.

The usual way to evaluate a GraphPath expression is to take just the terminal nodes of the matched paths. In that case, the result of evaluating a GraphPath expression is simply a set of nodes.

You can try the examples below by running test.py and entering the GraphPath expressions at the query> prompt:. When run with the --prompt option, this script loads some sample data, prints it, and allows you to query it.

$ python test.py --prompt
ready
query> _

Steps

Steps are the most primitive GraphPath expressions. A step matches a path consisting of a single node and no arcs (a zero-length path) or a path consisting of an initial node, a single arc, and a terminal node.

In the following, v represents any graph node, r represents a resource node, and s represents a set of graph nodes. The syntax of r and v depends on the host RDF handling system.

For the python Redland environment r and v must be Node objects. For rdflib r should be a URIRef and v should be either a URIRef or Literal. In addition, all implementations are expected to accept a string for v.

Node(v) Name Node step.
Matches

A path consisting of the single node specified by u.

Example Node(ex.julie)

Yields a set containing the single node ex.julie.

Property(r) Name Property step.
Matches

All paths containing a single arc belonging to the property specified by r.

Example Node( ex.julie )/Property(ex.parent )

Yields Julie's parents.

Nodes(s) Name Enumerated set step.
Matches

All paths consisting of a single node from the set specified by s. In a python environment, s must be an iterable yielding elements suitable for the Node(...) step.

Example Nodes([ex.julie, ex.lisa])

Yields a set containing the two nodes ex.julie and ex.lisa.

Class(r) Name Class step.
Matches

All paths consisting of a single node of type r. (The type(s) of a node is specified by the RDF type property.

Example Class( ex.Female )

Yields a set containing all nodes whose type is ex.Female.

Subject() Name Subject step.
Matches

All paths consisting of a single subject node. A subject node is any resource appearing as the subject of a statement or, in other words, the initial node of any arc.

Example Subject()

Yields a set containing all subjects.

Self()
Any()
Name Self step.
Matches

Any node. Any() and Self() are equivalent and will match any resource or value. The two forms are provided for readability in different usage.

Example Any()[Property( ex.parent )/Node( ex.julie)]
Node( ex.julie)//(Self()|Property( ex.parent ))

The first example yields all children of Julie. The second yields Julie's entire family tree including herself.

Note

In the present implementation Any() and Self() may only be used as part of a larger expression. Attempting to evaluate Any() as an expression will yield an error.

HasNo(p) Name Negative predicate step.
Matches

Any node that is not not among the initial nodes of the matches of p where p is a GraphPath expression.

Example Class( ex.Male )[HasNo(Property( ex.parent ))]

Yields all males who have no parent in the sample population.

Note
  1. In the present implementation HasNo(...) may only be used within a predicate.
  2. Caution must exercised when using HasNo(..) in inference rules because it violates the assumptions of monotonic reasoning. The inference algorithm may loop.

Operators

Operators combine expressions (including simple steps) to form more complex expressions. In the following, p and q are GraphPath expressions and G is a graph.

p/q Name Traversal operator.
Matches

All paths constructed by concatenating a match of p with a match of q where the terminal node of the p path is the initial node of the q path.

Example Node( ex.julie )/Property( ex.parent )

Yields Julie's parents.

p[q] Name Predicate operator.
Matches

All matches of p whose terminal node is the same as the initial node of some match of q.

Example Class( ex.Male )[Property( ex.parent )/Node( ex.julie )]

Yields Julie's sons.

p|q Name Union operator.
Matches

The union of the set of paths matching p and q respectively.

Example Class( ex.Male )|Class( ex.Female )

All men and women.

p&q Name Intersection operator.
Matches

The intersection of the nodes sets specified by p and q respectively.

Each node set is formed from the paths matching the respective expression. If the intersection operator appears in a predicate, then the initial node of each path is considered. Otherwise, the terminal nodes are used. The result is a set of zero-length paths, each consisting of s single node common to the two node sets.

Example Class( ex.Female )[Property( ex.parent )/Node( john )&Property( ex.initial)/Node( “a” )]

Yields John's daughters whose names begins with “a”.

Note

The definitions of the union and intersection operators do not parallel each other. The intersection operator flattens its arguments to zero-length paths in order to compute an intersection. This gives useful (and expected) behaviour in practice.

~p Name Inverse operator.
Matches

The matches of p reversed so that for each path the initial node becomes the terminal node, the order of arcs is reversed, and the terminal node becomes the initial node.

Example Node( ex.jack )/~(Property( ex.parent )/Property( ex.parent ))

Yields Jack's grandchildren.

p//q Name Transitive traversal operator.
Matches

All paths constructed by concatenating a match of p and one or more matches of q in sequence such that the terminal node of each component path is the initial node of the following component path.

Example Node( ex.julie )//Property( ex.parent )

Yields Julie's ancestors.

G>>p Name Context binding operator.
Matches

Paths within the graph G that match the expression p. G is said to be the context for evaluating p, or G is bound to p. Note that in the python implementation G may be any object supporting the Population protocol.

Example Genealogy1975>>Class( ex.Male )[Property(ex.parent)/Node(ex.julie)]

Yields Julie's sons as of 1975 (ie in a context called Genealogy1975).

Note

You can bind sub-expressions to different contexts and thereby mix different data sources in a controlled way within a single query.

Context binding is essential for executing expressions within a python program. The the binding operator takes an rdflib triple store or Redland model on the left side. The resulting bound expression is then iterable, yielding results.

Additional Operators

p%t Name Projection operator.
Evaluates

A set of tuples (r1, r2, ...rn) constructed from the expression, p, and the template, t, which is a tuple of expressions (t1, t2, ...tn).

  1. The expression p is evaluated as a query to yield a set of nodes, s.

  2. A result tuple (r1, r2, ...rn) is constructed for each node s such that s is the initial node and rn is the terminal node of a single match of the template expression tn.

  3. If there is no such path, rn is given the special value None.

  4. The result is the set of tuples so constructed (without duplicates).

A variation on the projection operator is defined where the template is a single expression. The expression p%q is equivalent to p%(q,) except the result is a set of nodes, and possibly the value None, instead of singleton tuples containing these nodes.

Example Class( ex.Male ) % (Property( ex.name ), Node( ex.initial ))

Yields the name and initial of each male.

Note

The projection operator is useful in python code to map graph data to tuples. This is convenient in for loops and avoids queries within loops in many cases. For example:

for name, initial in graph>>Class(ex.Male) % \
	  (Property(ex.name ), Node(ex.initial)):
	      print name, initial

Operator Precedence

The GraphPath operators have the same precedence as the python operators. (The python implementation does not perform any tricks to change operator precedence.) The operators are listed below from lowest precedence (least binding) to highest precedence (most binding).

p|q
p&q
G>>p
p/qp%t
~p
p[q]
(expressions...)