简体   繁体   中英

SPARQL Query Printing Literal in Jena

I am trying to write a query for this owl model.

:Sensor rdf:type owl:Class;
:hasId rdf:type owl:DatatypeProperty,
                rdfs:domain :Sensor;
                rdfs:range xsd:int.
:MedicalCountainer rdf:type :owlNamedIndividual,
                            :Sensor;
                            :hasId "55"^^xsd:int .

I want to use sensor-id to retrieve the sensor name. This is my query in Java, but I don't know why it doesn't print anything. I knew that my query is right because I will get the answer in Protégé.

String file = "C:/users/src/data.ttl";
Model model = FileManager.get().loadModel(file);
String queryString = "PREFIX : <http://semanticweb.org/sensor#>" +
                     "SELECT ?sensor" +
                     "WHERE {?sensor :hasId \"55"\^^<xsd:int>}";
Query query = QueryFactory.create(queryString);
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
          ResultSet result = qexec.execSelect();
          for ( ; result.hasNext(); ) {
                  QuerySolution soln = result.nextSolution();
                  Resource r = soln.getResource("sensor");
                  System.out.println(r);
          }
}

The usage of the literal in the SPARQL query is wrong. Either you use

  1. a prefixed URI for the literal, ie "55"^^xsd:int , or
  2. you put the full URI into angle brackets, ie "55"\\^^<http://www.w3.org/2001/XMLSchema#int>

but not a mixture of both.

And always prefer to add all PREFIX declarations to the beginning of the SPARQL query in order to ensure proper parsing across all SPARQL services:

PREFIX : <http://semanticweb.org/sensor#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?sensor
WHERE {
  ?sensor :hasId "55"^^xsd:int
}

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