Jena 中的 SPARQL 查询打印文字

SPARQL Query Printing Literal in Jena

我正在尝试为此 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 .

我想使用 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);
          }
}

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
}