Querying RDF/OWL using Bossam

(Last Update: 2007-07-04T09:01:58)

Here’s a simple example of loading and querying RDF documents. Currently, Bossam does not support SPARQL. You need to write queries in Buchingae rule language for Bossam, and it’s very easy. :-)

First, we need to load RDF documents into a Bossam reasoner. For our lesson, let’s load the W3C Wine ontology. Once the ontology is loaded, we should perform a reasoning over the wine ontology. As Bossam is a forward-chaining reasoner, a full forward-chaining derivation should be performed first to get full query results. The following is the code sequence.

1) Create an OWL reasoner instance.
IReasoner r = ReasonerFactory.getInstance().createOwlDlTrMReasoner();

2) Load the W3C Wine ontology from a URL.
r.load(IReasoner.OWL, "http://www.w3.org/TR/2004/REC-owl-guide-20040210/wine.rdf");

3) Perform a silent but full forward-chaining derivation. silentRun() does not return conclusions, which benefits it with a slight better performance than run().
r.silentRun();

4) Set some namespaces to write queries in a more compact form.
r.setNamespacePrefix("w", "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#");
r.setNamespacePrefix("o", "http://www.w3.org/2002/07/owl#");

Now, we’re ready to query the ontology. Some samples follow:

Query 1) (A Simple Query) Retrieve all the Wine instances.
Answer answer = r.ask1("query q is w:Wine(?x);");

Query 2) (A Conjunctive Query) Retrieve all the wines with White color and Full body.
Answer answer = r.ask1("query q is w:hasColor(?x,w:White) and w:hasBody(?x,w:Full);");

Query 3) (A Disjunctive Query) Retrieve all the wines with White color or Rose color.
Answer answer = r.ask1("query q is w:hasColor(?x,w:White) or w:hasColor(?x,w:Rose);");

Query 4) (A Query with Negation As Failure) Retrieve all the wines that are neither White wine nor Red wine.
Answer answer = r.ask1("query q is w:Wine(?x) and not w:WhiteWine(?x) and not w:RedWine(?x);");

Also, not for the Wine ontology, but it’s possible to use filters in the query, as shown below.

Query 5) Retrieve all the teen agers.
Answer answer = r.ask1("query q is p:hasAge(?x,?age) and [?age >= 10] and [?age < 20];");

Well, that’s it for today. Any questions or suggestions are welcomed!

The following is a full Java code for testing. Be sure to add all the JAR files included in the bossam release to your project’s classpath.

package org.etri.bossam.test;

import bossam.app.Answer;
import bossam.app.IReasoner;
import bossam.app.IReasonerFactory;
import bossam.app.ReasonerFactory;

public class WineQuery01
{
 final static String wineURI = "http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#";

 public static void main(String[] args)
 {
  try
  {
   // Creates a reasoner factory
   IReasonerFactory factory = ReasonerFactory.getInstance();
   // Creates a rule-based OWL DL reasoner
   IReasoner r = factory.createOwlDlTrMReasoner();
   // Loads the wine ontology into Bossam
   r.load(IReasoner.OWL, wineURI);
   // Perform a reasoning session
   String result = r.run();
   // Prints out the conclusions
   System.out.println("Conclusions: n" + result);
   // Sets the namespace prefix for querying the wine ontology
   r.setNamespacePrefix("w", wineURI);
   // Throws a query and gets an answer
   Answer answer = r.ask1("query q is w:WhiteWine(?x);");
   // Prints out the query result
   if (answer == null)
   {
    System.out.println("The query returns false!");
   }
   else
   {
    System.out.println("Answer (" + answer.getBindings().size() + "):n"
                             + answer);
   }
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
}

4 Responses to “Querying RDF/OWL using Bossam”

  1. An error in the code sample for RDF/OWL Ontology Querying fixed « Bossam Rule/OWL Reasoner Says:

    [...] RDF/OWL Ontology Querying fixed I’ve just realized that the sample code I presented in my previous post has some error. The base namespace of the wine ontology is not correct in the code. I’ve just [...]

  2. Rich Says:

    Hi,

    Sorry – I tried to post this to your forum at semwebcentral, but the registration process isn’t working properly – it wont sent me an email to confirm my registration.

    My question is hopefully simple. Can you show me how to write a query to find max() or min()?

    For example,

    Query 6) Retrieve the youngest person
    Query 7) Retrieve the oldest person

    Thanks!

  3. Rich Says:

    I’ve answered my own question:

    rule r1 is if p:hasAge(?x , ?age1) and p:hasAge(?y, ?age2) and [?age1 < ?age2] then p:hasLowerAge(?x, ?y)

    # returns the min age
    ask p:hasAge(?x, ?age) and not p:hasLowerAge(?y, ?x)

    # returns the max age
    ask p:hasAge(?x, ?age) and not p:hasLowerAge(?x, ?y)

    Is there a better way?

    It would be nice to see some more examples of these rules in Buchingae…

    Cheers!

  4. zebehn Says:

    I’m sorry for poor documentations and samples… I’ll try to expose some more examples. Your rules look great, btw.


Leave a Reply