Sections
You are here: Home » Data » Documents » Development » RDF files » Java developers only » Querying/Reasoning with Jena and SPARQL

Querying/Reasoning with Jena and SPARQL

Here is a short overview and comparison of RDF querying with SPARQL and Jena which is presented as follows: 1. SPARQL; 2. SPARQL from inside Jena; 3. Explicit and implicit relations when querying with Jena 4.Querying remote SPARQL endpoitns

1. SPARQL

The Simple Protocol and RDF Query Language (SPARQL) is a SQL-like language for querying RDF data. For expressing RDF graphs in the matching part of the query.  SPARQL is emerging as the de-facto RDF query language, and is a W3C Recommendation.

For our purposes SPARQL queries could be executed either directly through the SPARQL query panel in Protege or from inside a JAVA application using the specialised Jena library methods.  Both approaches are able to handle queries concerning explicit object and property relations but Jena libraries have the advantage of using a reasoner. Thus queries executed using Jena library methods can return results taking in account also the transitive and inferred relations.

Here is an example for querying the http://www.eswc2006.org/technologies/ontology ontology 

PREFIX ot: <http://www.opentox.org/api/1.1#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT  ?uri 
WHERE { 
    ?uri rdfs:subClassOf <ot:Feature>. 
}

 

Each SPARQL request starts with PREFIXes which denote the namespaces used in the query afterwards.The first lines defines namespace prefix, the last two lines use the prefix to express a RDF graph to be matched. Identifiers beginning with question mark ? identify variables. In this query, we are looking for resource ?uri participating in triples with predicates rdfs:subClassOf and want the subjects of these triples. Detailed SPARQL syntax description can be found here.

Since SPARQL is not aware of the rdfs semantics (hence SPARQL is not aware of owl's semantics). SPARQL doesn't understand the subclass assertions (which are processed as any assertion) and it doesn't understand that rdfs:subClassOf is transitive. Executed from inside Protege this example would return as result only the direct descendats of the class ot:Feature and nothing else. If we want to obtain a full list of subclasses of the ot:Feature class we need to do it using Jena Lib methods.

2. SPARQL from inside Jena

Jena supports several different ontology model specifications.  OntModelSpec.OWL_MEM_MICRO_RULE_INF  contains Transitive Reasoner which can be used to infer rdfs:subClassOf and rdfs:subPropertyOf. In contrast the model OWL_MEM does not contain such. Thus The same query we've shown above would return the same result as in Protege if we run in from inside Jena using the simpler model and it will return the entire subtree of subclass if we run it with the model using inference. Executing the following example you will be able to see the difference in the resultset using the two different models.

public static void main (String args[]) {
        String SOURCE = "http://www.opentox.org/api/1.1";
        String NS = SOURCE + "#";
       //create a model using reasoner
        OntModel model1 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF);
       //create a model which doesn't use a reasoner
        OntModel model2 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM);
                // read the RDF/XML file         model1.read( SOURCE, "RDF/XML" );         model2.read( SOURCE, "RDF/XML" );         //prints out the RDF/XML structure         qe.close();         System.out.println(" ");             // Create a new query     String queryString =              "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+         "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  "+         "select ?uri "+         "where { "+          "?uri rdfs:subClassOf <http://www.opentox.org/api/1.1#Feature>  "+         "} \n ";     Query query = QueryFactory.create(queryString);     System.out.println("----------------------");     System.out.println("Query Result Sheet");     System.out.println("----------------------");     System.out.println("Direct&Indirect Descendants (model1)");     System.out.println("-------------------");        // Execute the query and obtain results     QueryExecution qe = QueryExecutionFactory.create(query, model1);     com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();     // Output query results        ResultSetFormatter.out(System.out, results, query);     qe.close();         System.out.println("----------------------");     System.out.println("Only Direct Descendants");     System.out.println("----------------------");         // Execute the query and obtain results     qe = QueryExecutionFactory.create(query, model2);     results =  qe.execSelect();     // Output query results        ResultSetFormatter.out(System.out, results, query);     qe.close(); }

Prints out the following result:

----------------------
Query Result Sheet
----------------------
Direct&Indirect Descendants (model 1)
-----------------------------------------------------
| uri                                               |
=====================================================
| <http://www.opentox.org/api/1.1#NumericFeature>   |
| <http://www.opentox.org/api/1.1#NominalFeature>   |
| <http://www.opentox.org/api/1.1#StringFeature>    |
| <http://www.opentox.org/api/1.1#Feature>          |
| <http://www.w3.org/2002/07/owl#Nothing>           |
| <http://www.opentox.org/api/1.1#Identifier>       |
| <http://www.opentox.org/api/1.1#ChemicalName>     |
| <http://www.opentox.org/api/1.1#IUPACName>        |
| <http://www.opentox.org/api/1.1#InChI>            |
| <http://www.opentox.org/api/1.1#MolecularFormula> |
| <http://www.opentox.org/api/1.1#CASRN>            |
| <http://www.opentox.org/api/1.1#SMILES>           |
-----------------------------------------------------

Only Direct Descendants (model 2)
---------------------------------------------------
| uri                                             |
===================================================
| <http://www.opentox.org/api/1.1#NumericFeature> |
| <http://www.opentox.org/api/1.1#NominalFeature> |
| <http://www.opentox.org/api/1.1#StringFeature>  |
---------------------------------------------------

3. Explicit and implicit relations when querying with Jena predefined methods only

From inside Jena one could query the RDF knowledge base also without using SPARQL but by predefined predicates. Here if one wants to query for transitive and inferred relations he must use again a model with a reasoner. Most of the methods for selection have a boolean argument specifying whether only direct or indirect relations must be analysed. The same example shown above executed using only Jena methods would look as follows.

public static void main (String args[]) {
               
         // create the base model using reasoner
        String SOURCE = "http://www.opentox.org/api/1.1";
        String NS = SOURCE + "#";
        OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF);
        base.read( SOURCE, "RDF/XML" );

        System.out.println( "These are all direct&indirect subClasses of Feature");
        OntClass event = base.getOntClass( NS + "Feature" );

//listSubClasses(bool direct) - the false stands for direct and indirect descendants         for (Iterator<OntClass> i = event.listSubClasses(false); i.hasNext(); ) {            OntClass c = (OntClass) i.next();           System.out.println( c.getURI() );         }                 System.out.println( "-------------------------" );         System.out.println( "These are only the direct subClasses of Feature");         System.out.println( "-------------------------" );         OntClass event = base.getOntClass( NS + "Feature" ); //listSubClasses(bool direct) -  true stands for direct descendants only         for (Iterator<OntClass> i = event.listSubClasses(true); i.hasNext(); ) {            OntClass c = (OntClass) i.next();           System.out.println( c.getURI() );         }     }

 

4. Querying remote SPARQL services

Jena provides convenience methods for querying and processing results of a remote SPARQL endpoint, via its QueryExecutionFactory class:

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

        String ontology_service = "http://ambit.uni-plovdiv.bg:8080/ontology";
	String endpoint = "otee:Endpoints";
	String endpointsSparql = 
	"PREFIX ot:<http://www.opentox.org/api/1.1#>\n"+
	"	PREFIX ota:<http://www.opentox.org/algorithms.owl#>\n"+
	"	PREFIX owl:<http://www.w3.org/2002/07/owl#>\n"+
	"	PREFIX dc:<http://purl.org/dc/elements/1.1/>\n"+
	"	PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
	"	PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"+
	"	PREFIX otee:<http://www.opentox.org/echaEndpoints.owl#>\n"+
	"		select ?url ?title\n"+
	"		where {\n"+
	"		?url rdfs:subClassOf %s.\n"+
	"		?url dc:title ?title.\n"+
	"		}\n";

 QueryExecution x = QueryExecutionFactory.sparqlService(ontology_service, String.format(endpointsSparql,endpoint));
 ResultSet results = x.execSelect();
 ResultSetFormatter.out(System.out, results);

Results

--------------------------------------------------------------------------------------------------------------------------------------------------------
| url                                                                    | title                                                                       |
========================================================================================================================================================
| <http://www.opentox.org/echaEndpoints.owl#PhysicoChemicalEffects>      | "Physicochemical effects "^^<http://www.w3.org/2001/XMLSchema#string>       |
| <http://www.opentox.org/echaEndpoints.owl#ToxicoKinetics>              | "Toxicokinetics "^^<http://www.w3.org/2001/XMLSchema#string>                |
| <http://www.opentox.org/echaEndpoints.owl#EcotoxicEffects>             | "Ecotoxic effects"^^<http://www.w3.org/2001/XMLSchema#string>               |
| <http://www.opentox.org/echaEndpoints.owl#HumanHealthEffects>          | "Human health effects"^^<http://www.w3.org/2001/XMLSchema#string>           |
| <http://www.opentox.org/echaEndpoints.owl#EnvironmentalFateParameters> | "Environmental fate parameters "^^<http://www.w3.org/2001/XMLSchema#string> |
--------------------------------------------------------------------------------------------------------------------------------------------------------

Additional information may be found at:

http://en.wikipedia.org/wiki/RDF_query_language
http://www.w3.org/2001/11/13-RDF-Query-Rules/
http://www.w3.org/TR/rdf-sparql-query/
http://jena.sourceforge.net/ARQ/Tutorial/
http://jena.sourceforge.net/documentation.html
http://jena.sourceforge.net/javadoc/overview-summary.html

Document Actions