missrg
missrg

Reputation: 585

Trying to create a Sparql query using Jena's java api

I created an Ontology model in Java using the Apache Jena library, and I entered pizza ontology. I am trying to make a sparql query but the table print is blank, although normaly there are answers to my query. Am I doing something wrong...? Here is the code:

OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF);
String inputFileName="pizza.owl";
InputStream in = FileManager.get().open( inputFileName );
if (in == null) {
    throw new IllegalArgumentException(
         "File: " + inputFileName + " not found");
}
model.read(in, null);

String queryString =
        "prefix pizza: <www.co-ode.org/ontologies/pizza/pizza.owl#Pizza> "+        
        "prefix rdfs: <" + RDFS.getURI() + "> "           +
        "prefix owl: <" + OWL.getURI() + "> "             +
        "select ?pizza where {?pizza a owl:Class ; "      +
        "rdfs:subClassOf ?restriction. "                  +
        "?restriction owl:onProperty pizza:hasTopping ;"  +
        "owl:someValuesFrom pizza:PeperoniSausageTopping" +
        "}";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();

ResultSetFormatter.out(System.out, results, query);
qe.close();

Upvotes: 3

Views: 2902

Answers (1)

Ian Dickinson
Ian Dickinson

Reputation: 13305

Your prefix declaration is wrong. You have accidentally included the name of the Pizza class, and also left off the http protocol prefix. Corrected, it should be:

"prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#> "+

The way prefixes work in RDF and SPARQL is that you replace the prefix: with whatever the prefix is defined to be, and the resulting string must exactly match the URI of the resource you are trying to match. It must be an exact match - even differences in the case of letters is significant.

By the way, you can also simplify loading the ontology via the FileManager:

OntModel model = ModelFactory.createOntologyModel( 
                                  OntModelSpec.OWL_MEM_MICRO_RULE_INF);
FileManager.get().readModel( model, "pizza.owl" );

Upvotes: 3

Related Questions