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
"55"^^xsd:int
, or"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
}