从 SPARQL 结果集中检索数据

Retrieving data from SPARQL result set

我有一个 ontology,其中包含对象 (NamedIndividual) 及其坐标 (X,Y),形式为 datatype。 个人长这样:

<owl:NamedIndividual rdf:about="http://www.semanticweb.org/PredefinedOntology#Door1">
<rdf:type rdf:resource="http://www.semanticweb.org/PredefinedOntology#Objects"/>
<Y rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">20</Y>
<X rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">33</X>
</owl:NamedIndividual>

我执行 SPARQL 查询:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX onto: <http://www.semanticweb.org/PredefinedOntology#>

SELECT ?objects ?X ?Y
    WHERE { 
?objects rdf:type owl:NamedIndividual
; onto:X ?X
; onto:Y ?Y
 FILTER regex(str(?objects),"Door1")
}

我在 Eclipse 中的查询是这样的:

Model model = FileManager.get().loadModel("/home/aidos/workspace/OntologicalFramework/files/ontologies/NewOnt.owl");
String queryString = "//THE QUERY I'VE WRITTEN ABOVE IN A STRING FORM"
Query query = QueryFactory.create(queryString);
QueryExecution  qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out, results, query);

这会返回打印到 IDE 控制台的结果集,例如:

--------------------------
| objects      | X  | Y  |
==========================
| onto:Window1 | 56 | 28 |
--------------------------

我需要的是获取整数 56 和 28 并将它们存储在 int xint y 中。 有人可以帮助我了解如何获得它们吗? 在调试过程中,我在 ResultSet result

的分支中找到了名为 datasetDataSetImpl

每个ResultSet provides access to each row as a QuerySolution through use of the next() method. Then, you can use methods like getLiteral(String)获取指定名称变量的字面值。还有其他一些获取变量值的方法:get(String) returns an RDFNode getResource(String) returns 一个 Resource.

在这种情况下,您可以使用 get()getLiteral() 因为数字是文字,因此是 RDF 节点,但您不能使用 getResource(),因为数字不是资源(即,不是 IRI)。