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