[英]SPARQL Query Printing Literal in Jena
I am trying to write a query for this owl model.我正在尝试为这个 owl 模型编写一个查询。
: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.我想使用 sensor-id 来检索传感器名称。 This is my query in Java, but I don't know why it doesn't print anything.
这是我在 Java 中的查询,但我不知道为什么它不打印任何内容。 I knew that my query is right because I will get the answer in Protégé.
我知道我的问题是正确的,因为我会在 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. SPARQL 查询中文字的用法是错误的。 Either you use
要么你用
"55"^^xsd:int
, or"55"^^xsd:int
,或"55"\\^^<http://www.w3.org/2001/XMLSchema#int>
"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 声明添加到 SPARQL 查询的开头,以确保在所有 SPARQL 服务中正确解析:
PREFIX : <http://semanticweb.org/sensor#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?sensor
WHERE {
?sensor :hasId "55"^^xsd:int
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.