Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sparql

jena

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();
like image 838
missrg Avatar asked Nov 30 '25 18:11

missrg


1 Answers

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" );
like image 130
Ian Dickinson Avatar answered Dec 03 '25 08:12

Ian Dickinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!