简体   繁体   English

从SPARQL结果集中检索数据

[英]Retrieving data from SPARQL result set

I've got an ontology with the objects ( NamedIndividual ) and their coordinates (X,Y) in form of datatype . 我有一个本体的对象( NamedIndividual )和他们的坐标(X,Y)以datatype形式。 The individual look like this: 个人看起来像这样:

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

I do a SPARQL query: 我做一个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")
}

My query in Eclipse is this: 我在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);

This gives me back a result set printed to the console of my IDE, like: 这给了我一个打印到IDE控制台的结果集,如:

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

What I need is to get those integers 56 and 28 and store them in the int x and int y . 我需要的是得到那些整数56和28并将它们存储在int xint y Could someone help me to understand how can I get them? 有人可以帮我理解我怎样才能得到它们? While debugging process I've found the DataSetImpl called dataset in the branch of ResultSet result 在调试过程中,我发现DataSetImplResultSet result的分支中称为dataset

Each ResultSet provides access to each row as a QuerySolution through use of the next() method. 每个ResultSet都通过使用next()方法提供对每行的访问作为QuerySolution Then, you can use methods like getLiteral(String) to get the literal value of the variable with a specified name. 然后,您可以使用getLiteral(String)等方法来获取具有指定名称的变量的文字值。 There are some other methods for getting variable values, too: get(String) returns an RDFNode , and getResource(String) returns a Resource . 还有一些其他方法可以获取变量值: get(String)返回RDFNodegetResource(String)返回Resource

In this case, you could use get() or getLiteral() since the numbers are literals, and thus RDF nodes, but you can't use getResource() , since the numbers aren't resources (ie, not IRIs). 在这种情况下,您可以使用get()getLiteral(),因为数字是文字,因此是RDF节点,但您不能使用getResource() ,因为数字不是资源(即不是IRI)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM