So far, in brief discussions of how one might use and query the Issue Tracker RDF metadata, we used a simple and primitive query API. Now we move on to a more full-blooded query language. This will help make the middleware code clearer and will provide the performance needed to incorporate huge models such as the WordNet model demonstrated a few installments ago.
Versa is an open RDF query language based on the typical needs of a developer for RDF tools that can be used within other applications. It is a product of more than just the RDF world view. Versa focuses on the nodes and arcs in the RDF model rather than looking at the model as a collection of triples. It provides a core data model and a very rich set of functions and primitives for flexible query. Because Versa uses functions heavily, it sometimes has a LISP-like feel. Versa also provides facilities -- such as full boolean logic and set operations, transitive operations, aggregates, substring matching, and other core data type manipulation -- that are lacking in many other RDF query systems. I am one of the authors of the original Versa spec.
The heart of Versa is the traversal expression which matches patterns in the model's graph. The following is an example of a traversal expression:
all() - rdf:type -> *
all() function returns a set of all the resources in the model. According to the conventional drawing of RDF models, this means it returns all ovals and arcs (but not rectangles). The
-> tokens form what is known as the traversal operator, which indicates that you wish to follow a particular arc from each resource. In this case, the arc is
* indicates that you want all the end-points of this traversal. In effect, this traversal expression returns the objects of all statements with an
As an illustration, let's look at the sample RDF issue tracker instance from the last installment, repeated here in Listing 1.
Listing 1. Sample RDF issue tracker instance
<?xml version='1.0'?> <!DOCTYPE rdf:RDF [ <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#"> <!ENTITY daml "http://www.daml.org/2001/03/daml+oil#"> <!ENTITY dc "http://purl.org/dc/elements/1.1/"> <!ENTITY foaf "http://xmlns.com/foaf/0.1/"> <!ENTITY it "http://rdfinference.org/schemata/issue-tracker/"> <!ENTITY rit "http://rdfinference.org/ril/issue-tracker/"> ]> <rdf:RDF xmlns:rdf="&rdf;" xmlns:rdfs="&rdfs;" xmlns:daml="&daml;" xmlns:rit="&rit;" xmlns:it="⁢" xmlns:dc="&dc;" xmlns:foaf="&foaf;" xmlns="⁢" > <rdf:Description rdf:about='http://rdfinference.org/ril/ril-20010502'> <issue rdf:resource='&rit;i2001030423'/> <issue rdf:resource='&rit;i2001042003'/> </rdf:Description> <Issue rdf:about='&rit;i2001030423'> <dc:title>Unnecessary abbreviation</dc:title> <dc:creator rdf:resource='mailto:Alexandre.Fayolle@logilab.fr'/> <dc:description>Is the abbreviation of rdf:type predicates needed?</dc:description> <dc:date>2001-03-04</dc:date> <comment rdf:parseType="Resource"> <dc:creator rdf:resource='mailto:Alexandre.Fayolle@logilab.fr'/> <dc:description>The abbreviation in listing 8 doesn't seem needed.</dc:description> </comment> <action rdf:parseType="Resource"> <dc:description>Organize a vote on this topic</dc:description> <it:assignee rdf:resource='mailto:email@example.com'/> </action> </Issue> <Issue rdf:about='&rit;i2001042003'> <dc:title>Inconsistent versioning</dc:title> <dc:creator rdf:resource='mailto:Nicolas.Chauvat@logilab.fr'/> <dc:description>RIL versioning is unclear (mix of 0.1, 0/1, 0.2 and 0/2)</dc:description> <dc:date>2001-04-20</dc:date> <action rdf:parseType="Resource"> <dc:description>Correct all to use the "0/1" form in the next draft.</dc:description> <it:assignee rdf:resource='mailto:firstname.lastname@example.org'/> </action> </Issue> <rdf:Description rdf:about='mailto:Alexandre.Fayolle@logilab.fr'> <foaf:name>Alexandre Fayolle</foaf:name> </rdf:Description> <rdf:Description rdf:about='mailto:email@example.com'> <foaf:name>Uche Ogbuji</foaf:name> </rdf:Description> <rdf:Description rdf:about='mailto:Nicolas.Chauvat@logilab.fr'> <foaf:name>Nicolas Chauvat</foaf:name> </rdf:Description> </rdf:RDF>
Figure 1 shows this in graph form.
Figure 1. A graph model of the RDF issue tracker sample
If we ran this query on this model, we would get a list of resources (because the objects of
rdf:type statements are resources). If we were using a predicate which could have literal and resource objects, the result would be a list of literals and resources. To test this using 4Suite (see earlier installments for more details on 4Suite), you could copy Listing 1 to a file, issues.rdf, and execute the command highlighted in red on the command line:
$4versa --rdf-file=issues.rdf "all() - rdf:type -> *" ::: Using cDomlette Executing Query: all() - rdf:type -> * With nsMapping of: vtrav --> http://rdfinference.org/versa/0/2/traverse/ xml --> http://www.w3.org/XML/1998/namespace vsort --> http://rdfinference.org/versa/0/2/sort/ rdfs --> http://www.w3.org/2000/01/rdf-schema# rit --> http://rdfinference.org/schemata/issue-tracker/ it --> http://rdfinference.org/schemata/issue-tracker/ rdf --> http://www.w3.org/1999/02/22-rdf-syntax-ns# foaf --> http://xmlns.com/foaf/0.1/ versa --> http://rdfinference.org/versa/0/2/ None --> http://rdfinference.org/schemata/issue-tracker/ daml --> http://www.daml.org/2001/03/daml+oil# <List> <Resource>http://rdfinference.org/schemata/issue-tracker/Issue/<Resource> <Resource>http://rdfinference.org/schemata/issue-tracker/Issue</Resource> </List>
The resulting list of resources, presented in a simple XML form, is shown in bold. The other output from the command is just for your information. It echoes the Versa query being executed, and shows the namespace declarations that the engine is aware of. 4versa automatically grabs all the namespace declarations within the root element of the source file as a convenience to the user.
In general, the format of a traversal expression is:
list-expression - list-expression -> boolean-expression
A list expression is any expression that returns a list of resources or a result that can be converted to a list of resources. Therefore, any expression that returns a single resource (
rdf:type) is converted to a list type with a single entry. You have already seen RDF URIs abbreviated to forms such as
rdf:type and forms that are called qualified names or QNames. They are converted to full URIs by expanding the first part into a URI base (for example
http://www.w3.org/1999/02/22-rdf-syntax-ns#) and then concatenating to the second part. In this way,
http://www.w3.org/1999/02/22-rdf-syntax-ns#type. Versa also allows you to spell out URIs in all their tedious detail, which means you can just write
@"http://www.w3.org/1999/02/22-rdf-syntax-ns#type". For example:
all() - @"http://www.w3.org/1999/02/22-rdf-syntax-ns#type" -> *
The third part of the traversal expression is a boolean expression. I have already shown how you can use
* to select all objects. You can also be more selective about the results. For instance, to get all the date properties of resources in the model, you might use:
all() - dc:date -> *
which results in:
<List> <String>2001-03-04</String> <String>2001-04-20</String> </List>
To select a specific date, you can write:
all() - dc:date -> eq("2001-04-20")
eq function compares an argument to the context and returns true if they are the same. The idea of context in Versa is similar to that in XPath, but simpler. In Versa, the context is a single value that is considered while evaluating an expression. You can access the context directly by using a dot symbol. You can also use the
eq function to compare two explicit arguments, so the above can also be written as follows:
all() - dc:date -> eq(., "2001-04-20")
In the third part of a traversal expression, the context is one of the partial results from the first and second parts. For instance, in the above, each object is compared to "2001-04-20" and the final result is the list of objects for which this comparison is true, in this case:
<List> <String>2001-04-20</String> </List>
Figure 2 illustrates some of the workings of a traversal expression.
Figure 2. An illustration of the workings of a traversal expression
This seemingly obvious query is useful in determining whether a particular value is the model. For instance, if you replaced "2001-04-20" with "2002-03-15" in the above example, the result would be an empty list. You can certainly do much more with traversal expressions. For example, to retrieve all dates for resources in the month of March, you could write:
all() - dc:date -> contains("-03-")
<List> <String>2001-03-04</String> </List>
It is probably more useful to get the resources with particular dates. To do this, you need to work backwards, from a date against the
dc:date arc to the subject resource. Versa provides this operation in the form of a backward traversal. For example:
"2001-03-04" <- dc:date - *
returns all resources with date "2001-03-04":
<List> <Resource>http://rdfinference.org/ril/issue-tracker/i2001030423</Resource> </List>
Backward traversals are in the form:
list-expression <- list-expression - boolean-expression
which works quite similarly to forward traversals. Both types of traversals can be chained, so that one could get the titles of all resources dated "2001-03-04":
("2001-03-04" <- dc:date - *) - dc:title -> *
<List> <String>Unnecessary abbreviation</String> </List>
So far, all the queries have returned a single value. Often, you may wish to return more than one value at a go. Versa takes care of this with list operations which work on the results of traversal expressions. One common function for dealing with lists is
distribute, which applies one or more expressions to each item in the list. The result is a list of lists. You can directly express a list in Versa by using an expression such as
list(rit:i2001030423, rit:i2001042003), which is a list of two resources. The following expression obtains the title and date of each of these issues:
distribute(list(rit:i2001030423, rit:i2001042003), ".-dc:title->*", ".-dc:date->*")
This yields a list of lists:
<List> <List> <List> <String>Unnecessary abbreviation</String> </List> <List> <String>2001-03-04</String> </List> </List> <List> <List> <String>Inconsistent versioning</String> </List> <List> <String>2001-04-20</String> </List> </List> </List>
The first argument of the
distribute function is a list. Each item in the list is taken in turn. The second and subsequent arguments are strings, which are treated as sub-queries. These are dynamically evaluated using the current list item for context (which is referred to using a dot, as discussed earlier). Figure 3 illustrates the workings of this query.
Figure 3. An illustration of the workings of distribute
You can use this technique on the results of traversal expressions, which are lists. For the final example, I'll explain how to use a special shortcut function in Versa. The
type function retrieves all resources of a given RDF type (as expressed with
rdf:type predicates). To get the IDs and names of all people who have submitted issues, you can write:
distribute(type(it:Issue)-dc:creator->*, ".", ".-foaf:name->*")
Notice that you can use the context directly in a sub-expression, without necessarily making it part of another expression. The result is:
<List> <List> <Resource>mailto:Nicolas.Chauvat@logilab.fr</Resource> <List> <String>Nicolas Chauvat</String> </List> </List> <List> <Resource>mailto:Alexandre.Fayolle@logilab.fr</Resource> <List> <String>Alexandre Fayolle</String> </List> </List> </List>
There is one tricky detail here. The strings that result from the
.-foaf:name->* sub-expression are in a list, but the resources that come from the
. sub-expression are not. This is because traversal operators always return lists, even if there is only one item, or none, in the result. Since we know that in our model, we only expect to have a single name for each person's resource, we usually ignore the lowest level of lists anyway. Versa provides data conversion functions, one of which is the
string function, which converts its argument to a string. A list would be converted by getting the string value of its first (or only) item. Therefore,
distribute(type(it:Issue)-dc:creator->*, ".", "string(.-foaf:name->*)")
eliminates the extraneous lists around the traversal sub-expression and results in the following:
<List> <List> <Resource>mailto:Nicolas.Chauvat@logilab.fr</Resource> <String>Nicolas Chauvat</String> </List> <List> <Resource>mailto:Alexandre.Fayolle@logilab.fr</Resource> <String>Alexandre Fayolle</String> </List> </List>
I've explained the basics of Versa in this article. If you have followed it, you can be productive with it right away. Versa has many more facilities, but most of them are in the form of specialized functions, with which you'll gain experience quickly. There are more resources on Versa listed in the Resources section. In the next installment, I will explain how you can put Versa to work on all the query needs discussed so far in this series.
- Participate in the discussion forum.
- For a more extensive tutorial of Versa, see the Versa by example document.
- Find out all the details on Versa in the Versa 0.2 draft specification.
- Dave Beckett's extensive RDF resource guide includes pointers to other RDF query languages.
- IBM offers professional services to help organizations implement Portals, knowledge and content management solutions.
- IBM's Knowledge Management journal covers many technology topics in the field.
- Check out Thinking XML's previous columns.
Uche Ogbuji is a consultant and co-founder of Fourthought Inc., a software vendor and consultancy specializing in XML solutions for enterprise knowledge management. Fourthought develops 4Suite, an open source platform for XML, RDF, and knowledge-management applications. Mr. Ogbuji is a Computer Engineer and writer born in Nigeria, living and working in Boulder, Colorado, USA. You can contact Mr. Ogbuji at firstname.lastname@example.org.