Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Thinking XML: Basic XML and RDF techniques for knowledge management, Part 6

RDF Query using Versa

Uche Ogbuji (uche@ogbuji.net), Principal Consultant, Fourthought, Inc.
Photo of Uche Ogbuji
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 uche@ogbuji.net.

Summary:  Uche Ogbuji moves on to a discussion of a far more sophisticated RDF query language than the primitive API he has discussed thus far. This is the foundation for establishing the middleware for the Issue Tracker article in coming installments.

Date:  10 Apr 2002
Level:  Intermediate
Also available in:   Japanese

Activity:  9432 views
Comments:  

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: an RDF query language

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 -> *

The 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 - and -> 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 rdf:type. The * indicates that you want all the end-points of this traversal. In effect, this traversal expression returns the objects of all statements with an rdf:type predicate.

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="&it;"
  xmlns:dc="&dc;"
  xmlns:foaf="&foaf;"
  xmlns="&it;"
>

  <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:uche.ogbuji@fourthought.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:uche.ogbuji@fourthought.com'/>
    </action>
  </Issue>

  <rdf:Description rdf:about='mailto:Alexandre.Fayolle@logilab.fr'>
    <foaf:name>Alexandre Fayolle</foaf:name>
  </rdf:Description>

  <rdf:Description rdf:about='mailto:uche.ogbuji@fourthought.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
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.


Traversals under the glass

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 rdf becomes http://www.w3.org/1999/02/22-rdf-syntax-ns#) and then concatenating to the second part. In this way, rdf:type becomes 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")

The 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
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-")

which yields:

<List>
  <String>2001-03-04</String>
</List>


Traversing backwards

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 -> *

which yields:

<List>
  <String>Unnecessary abbreviation</String>
</List>


Distributing the wealth

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
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>


Conclusion

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.


Resources

About the author

Photo of Uche Ogbuji

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 uche@ogbuji.net.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=86614
ArticleTitle=Thinking XML: Basic XML and RDF techniques for knowledge management, Part 6
publish-date=04102002
author1-email=uche@ogbuji.net
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers