jena.query.ResultSet and jena.query.QuerySolution: empty iterator after SPARQL request

跟風遠走 提交于 2019-12-11 11:38:14

问题


A have a problem with receiving SPARQL response. A problem is method ((ResultSet) response).hasNext() returns false despite response shouldn't be empty.

Request is:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX ontology: <http://www.semanticweb.org/kseniia/ontologies/2013/1/untitled-ontology-12#> SELECT ?x 
WHERE {?x rdfs:subClassOf ontology:Visual}

This works correctly in Protege and returns 3 objects:

Location
Relation
Descriptive

Query was executed in jena such way:

Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
Object response = qexec.execSelect();
qexec.close();
for ( ; ((ResultSet) response).hasNext(); ) {   // always false
   QuerySolution soln = ((ResultSet) response).nextSolution();
   // etc
}

Maybe I missed something?


回答1:


You are closing the execution with qexec.close, then iterating over the results. Except the results are closed by the qexec.close and no longer available.

Move the qexec.close to after the loop.

Improvement:

Object response ==> ResultSet response



来源:https://stackoverflow.com/questions/15698578/jena-query-resultset-and-jena-query-querysolution-empty-iterator-after-sparql-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!