简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM