Using TrOWL for SPARQL Conjunctive Query Answering

1. Overview

TrOWL (from v1.0) comes with the TrOWL-BGP API, an adapted version of the OWL-BGP API, so as to support answering conjunctive queries in SPARQL. TrOWL treats SPARQL variables as distinguished variables that can be evaluated as individuals. Here we briefly introduce how to use TrOWL and TrOWL-BGP to answer conjunctive SPARQL queries. This example can be found in the TrOWLBGPDemo.java file in the release. All required ontologies are also included in the release.

2. Initialise Query Graph

To start with, we initialise the dataset and the query graph, where queriedOntology in the code below is an OWLOntology object representing the ontology that you query against:

dataset=new OWLOntologyDataSet(queriedOntology, new HashMap<String, OWLOntology>());
OWLOntologyGraph graph=dataset.getDefaultGraph();

During the above steps, a TrOWL reasoner will be created and attached to the graph object, hence reasoning will be performed for query answering. This reasoner will automatically materialise the query ontology, computing all concept/role hierarchies and ABox assertions. Such a materialisation is realised via syntactic approximation as in a usual TrOWL reasoner.

 3. Construct Query

Construct a query with the following code. The example used here is the query 9 in the LUBM benchmark.

String s="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+LB
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+LB
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> "+LB
+ "PREFIX ub: <http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#>" +LB
+ "PREFIX : <http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#>" +LB
+ "SELECT ?x ?y ?z WHERE {"+LB
+ "?x rdf:type ub:Student . "+LB
+ "?y rdf:type ub:Faculty . "+LB
+ "?z rdf:type ub:Course . "+LB
+ "?x ub:advisor ?y . "+LB
+ "?y ub:teacherOf ?z . "+LB
+ "?x ub:takesCourse ?z"+LB
+ "}";
Query query=QueryFactory.create(s);

4. Execute Query

The following code can be used to execute the query constructed above:

OWLReasonerSPARQLEngine sparqlEngine=new OWLReasonerSPARQLEngine(new MinimalPrintingMonitor());
sparqlEngine.execQuery(query,dataset);

Particularly, the execQuery method returns the set of the query results, which can be further retrieved .