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 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 |
|
Example | Node(ex.julie) | |
Yields a set containing the single
node |
Property(r) | Name | Property step. |
---|---|---|
Matches |
All paths containing a single arc
belonging to the property specified by |
|
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 |
|
Example | Nodes([ex.julie, ex.lisa]) | |
Yields a set containing the two nodes
|
Class(r) | Name | Class step. |
---|---|---|
Matches |
All paths consisting of a single node
of type |
|
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. |
|
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 |
HasNo(p) | Name | Negative predicate step. |
---|---|---|
Matches |
Any node that is not
not among the initial nodes of the matches
of |
|
Example | Class( ex.Male )[HasNo(Property( ex.parent ))] | |
Yields all males who have no parent in the sample population. |
||
Note |
|
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 |
|
Example | Node( ex.julie )/Property( ex.parent ) | |
Yields Julie's parents. |
p[q] | Name | Predicate operator. |
---|---|---|
Matches |
All matches of |
|
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
|
|
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
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 |
|
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 |
|
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. |
p%t | Name | Projection operator. |
---|---|---|
Evaluates |
A set of tuples
A variation on the projection operator
is defined where the template is a single expression. The
expression |
|
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 name, initial in graph>>Class(ex.Male) % \ (Property(ex.name ), Node(ex.initial)): print name, initial |
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/q | p%t |
~p | |
p[q] | |
(expressions...) |