Y.A.
Y.A.

Reputation: 13

retrieve individuals from OWL file using sparql query Jena

This is part of my CS.owl that shows individual DropBox of class CloudService.

 <!-- http://www.semanticweb.org/ontologies/SaaS-24-03-2013.owl#DropBox -->
    <owl:NamedIndividual rdf:about="&SaaS-24-03-2013;DropBox">
        <rdf:type rdf:resource="&SaaS-24-03-2013;CloudService"/>
        <hasPriceModel rdf:resource="&SaaS-24-03-2013;Freemium"/>
        <hasDeliveryModel rdf:resource="&SaaS-24-03-2013;Software-as-a-Service"/>
    </owl:NamedIndividual>

I need to retrieve individuals (such as DropBox) of class CloudService using Jena. The following SPARQL query runs perfectly in Protege 4.3. It retrieves many services including "DropBox". I need to run it using Jena. Here is my code

        String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
                        "PREFIX owl: <http://www.w3.org/2002/07/owl#> "+
                            "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "+
                                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
                                    "PREFIX : <http://www.semanticweb.org/ontologies/SaaS-24-03-2013.owl#> "+
                                        "SELECT ?Service "+
                                                " WHERE {"+
                                            " ?Service  a   :CloudService} "; 
           model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
                model.read("ontologies/CS.owl");
        Query query = QueryFactory.create(SparqlQuery);
                QueryExecution qe = QueryExecutionFactory.create(query, model);
                com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();         
    qe.close();
ResultSetFormatter.out(System.out, results);
}

The query returns empty result under the column header While it works on Protege and returns results (including DropBox) What is wrong with my code?

Upvotes: 0

Views: 898

Answers (1)

AndyS
AndyS

Reputation: 16630

You closed the execution qe then tried to print results. Code must finish working with the results calling qe.close. Move the ResultSetFormatter line.

Upvotes: 1

Related Questions