Home > bossam, manual > Querying RDF/OWL using Bossam

Querying RDF/OWL using Bossam

February 1, 2007 Leave a comment Go to comments

(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();
  }
 }
}

Advertisements
Categories: bossam, manual
  1. Rich
    August 11, 2007 at 4:14 am

    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!

  2. Rich
    August 11, 2007 at 6:37 am

    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!

  3. August 16, 2007 at 4:54 pm

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

  1. July 4, 2007 at 9:43 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: