[英]Can't get any results from this SPARQL query
I've asked a question before here 我在这里问过一个问题
Generate an incremental SPARQL query 生成增量SPARQL查询
and thank god I manage to solve it. 感谢上帝,我设法解决了这个问题。
Here is my solution: 这是我的解决方案:
private String completeQuery(String coreQuery){
String completeQuery = "";
completeQuery += "SELECT * WHERE {"+ "\n";
completeQuery += coreQuery + "\n";
completeQuery =completeQuery + "}" + "\n" +"limit 5" ;
return completeQuery;
}
public String link(String object1, String object2, int distance){
if(distance == 1){
String Quer = "<http://dbpedia.org/resource/"+object1+">" + " ?pre1 " +"<http://dbpedia.org/resource/"+object2+">";
return completeQuery(Quer);
}
else {
String query = "<http://dbpedia.org/resource/"+object1+">" + " ?pre1 ?obj1 " + ".\n";
for(int i = 1; i < distance-1; i++){
query += "?obj" + i + " ?pre" + (i+1) + " ?obj" + (i+1) + ".\n" ;
}
query += "?obj" + (distance-1) + " ?pre" + distance + " " + "<http://dbpedia.org/resource/"+object2+">";
return completeQuery(query);
}
}
and from the main I just try to print the query string to figure out if it is right or not: 从头开始,我只是尝试打印查询字符串以查明是否正确:
String queryString = "";
int maxDistance =2;
PathFinder pf = new PathFinder();
for (int distance = 1; distance<=maxDistance ; distance ++)
{
System.out.println("\nQueries for distance: "+distance);
queryString = pf.link("Lionel_Messi","Spanish_language",distance);
System.out.println(queryString);}
and the result is good so far 到目前为止效果很好
Queries for distance1: SELECT * WHERE {<http://dbpedia.org/resource/Lionel_Messi> ?pre1 <http://dbpedia.org/resource/Spanish_language>}limit 5
Queries for distance2: SELECT * WHERE { <http://dbpedia.org/resource/Lionel_Messi> ?pre1 ?obj1 . ?obj1 ?pre2 <http://dbpedia.org/resource/Spanish_language> }limit 5
now, I'm trying to execute these queries but I don't know how to do it if it was a simple query , something like (select ?abstract where ...) the execution will be for example: 现在,我正在尝试执行这些查询,但是如果这是一个简单的查询,我不知道如何执行,例如(选择?abstract where ...),执行将例如:
QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);
ResultSet results = qe.execSelect();
for (; results.hasNext();){
QuerySolution sol = (QuerySolution) results.next();
System.out.println(sol.get("?abstract"));
}
but if the query is like (select * where) how can I get the results without knowing the specific elements I want to print? 但是如果查询类似于(选择*位置),如何在不知道要打印的特定元素的情况下获得结果? it's all depend on my distance and what queries it will return ....
这一切都取决于我的距离以及它将返回什么查询...。
This is not something you can solve just by means of SPARQL alone - you will need to use the capabilities of the API toolkit you're using to handle queries and results. 仅靠SPARQL不能解决这个问题-您将需要使用用于处理查询和结果的API工具箱的功能。 Since you are using Jena, you should have a look at the documentation for Jena's ResultSet object.
由于您正在使用Jena,因此应该查看Jena的ResultSet对象的文档。 It has a method
getResultVars()
which gives you a list with all variable names in the result. 它具有方法
getResultVars()
,该方法为您提供一个列表,其中包含结果中的所有变量名称。 You can then use this to iterate over the result and get the variable-bindings in each solution, eg to simply print them: 然后,您可以使用它遍历结果并获得每个解决方案中的变量绑定,例如,简单地打印它们:
List<String> varNames = results.getResultVars();
while(results.hasNext()) {
QuerySolution sol = (QuerySolution) results.next();
for (String var: varNames) {
System.out.println("value of " + var + ": " + sol.get(var));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.