无法从此SPARQL查询中获得任何结果 [英] Can't get any results from this SPARQL query

查看:125
本文介绍了无法从此SPARQL查询中获得任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问过一个问题

生成增量SPARQL查询

感谢上帝,我设法解决了这个问题.

and thank god I manage to solve it.

这是我的解决方案:

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);}

到目前为止效果很好

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

现在,我正在尝试执行这些查询,但是我不知道该怎么做 如果这是一个简单的查询,则类似于(选择?abstract where ...)的执行例如:

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:

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

推荐答案

仅靠SPARQL不能解决这个问题-您将需要使用API​​工具箱的功能来处理查询和结果.由于使用的是Jena,因此应该查看Jena的

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. Since you are using Jena, you should have a look at the documentation for Jena's ResultSet object. It has a method getResultVars() which gives you a list with all variable names in the result. You can then use this to iterate over the result and get the variable-bindings in each solution, e.g. 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));
       }
 }

这篇关于无法从此SPARQL查询中获得任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆