Jena中的SPARQL查询打印文字

问题描述 投票:0回答:1

我正在尝试为此猫头鹰模型编写查询。

: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 .

我想使用sensor-id检索传感器名称。这是我在Java中的查询,但是我不知道为什么它不打印任何内容。我知道我的查询是正确的,因为我将在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);
          }
}
java sparql jena semantic-web ontology
1个回答
2
投票

SPARQL查询中文字的使用是错误的。您可以使用

  1. 文字的前缀URI,即"55"^^xsd:int
  2. 您将完整URI放在尖括号中,即"55"\^^<http://www.w3.org/2001/XMLSchema#int>

但不是两者的混合物。

并且总是希望将所有PREFIX声明添加到SPARQL查询的开头,以确保跨所有SPARQL服务进行正确的解析:

PREFIX : <http://semanticweb.org/sensor#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?sensor
WHERE {
  ?sensor :hasId "55"^^xsd:int
}
© www.soinside.com 2019 - 2024. All rights reserved.