Versa: Path-Based RDF Query Language
by Chimezie Ogbuji
|
Pages: 1, 2, 3, 4
Traversal Expressions
The most fundamental kind of expression in Versa is the Traversal Expression. There are three kinds of these:
- Forward Traversal Operators
- Forward Filter Operators
- Backwards Traversal Operators
Forward Traversal Operator
A forward traversal operator is an expression that returns a list of objects from statements that match the pattern it represents. Traversal operators are almost direct, visual analogs of SPARQL's basic Triple Patterns [2.2] with Versa expressions at each part, which limits the result set to statements where the corresponding component is a member of a list formed from the result of evaluating the expression. A Versa forward filter operator always takes on the following form:
subjects - predicates -> boolean
The first item (marked subjects above) is evaluated and the result is converted to a list. Statements that match must have a subject whose value is equivalent to any of the resources in the resulting list.
Versa has an explicit set of rules that are followed when converting from one data type to another. This rule matrix is very similar to XPaths. The most common example is to convert a single resource or literal to a list. A resource or literal can be converted to a list by returning a list with the resource or literal as its only member (a list with a length of 1). The complete set of rules is expressed as a matrix in the Versa Specification [3.2].
The second item (marked predicates above) is an expression that is also expected to result in a list of resources. In this case, the expression is evaluated with respect to a list consisting of the resources from the first expression (subjects). Statements that have matched the first criteria (the subjects expression) are examined and excluded if their predicate is not a member of this resulting list.
Finally, the third expression (marked as boolean) is evaluated with respect to the objects of each of the statements that have matched the pattern so far. These expressions are expected to evaluate to a boolean value. If this value is true then the objects of the resulting statements are returned. They are discarded otherwise.
Below is a diagram of this process:
|
|
Consider,
all()-rdfs:label->*
all is a built-in versa function that returns all the resources in the underlying RDF model. The prefix rdfs is always associated with the URI http://www.w3.org/2000/01/rdf-schema#. This URI identifies the domain of concepts that have to do with defining an RDF vocabulary. Within this domain, the property http://www.w3.org/2000/01/rdf-schema#label is defined as:
A human-readable name for the subject.
The example will return the human-readable names of every resource in the underlying RDF model. This is an example of the most common way the "*" character is used.
The SPARQL equivalent would be:
SELECT ?label
WHERE {
?resource rdfs:label ?label
}
Forward Filter Operator
Forward Filter Operators behave the same way as Forward Traversal Operators with the exception that they return the subjects of the candidate statements. They are expressed in the following form:
subjects |- predicates -> boolean
So we could augment the previous query to return all resources that have labels by using a Forward Filter Operator instead:
all()|-rdfs:label->*
In SPARQL:
SELECT ?resource
WHERE {
?resource rdfs:label ?label
}
Backward Traversal Operators
A Backward Traversal Operator essentially expresses the inverse constraint along the same predicate(s). Backward traversal operator expressions are of the form:
object <- predicate - boolean
Below is a more complex example that demonstrates combining traversal expressions in both directions:
(rdfs:Class <- rdf:type - *) - rdfs:comment -> *
This query returns commentary on all classes in the graph and is semantically equivalent to the following SPARQL query:
SELECT ?comment
WHERE {
?class rdfs:comment ?comment.
?class rdf:type rdfs:Class.
}
