简体   繁体   English

生成增量SPARQL查询

[英]Generate an incremental SPARQL query

is there any way to generate a SPARQL query in a dynamic way? 有什么方法可以动态生成SPARQL查询吗? what I'm trying to do is some thing like this: if I have a DBpedia resource r1 and another DBpedia resource r2, this query 我想做的是这样的:如果我有一个DBpedia资源r1和另一个DBpedia资源r2,则此查询

SELECT * WHERE { <r1> ?pre <r2> }

will return the predicate between the 2 resources and this query 将返回2个资源与该查询之间的谓词

SELECT * WHERE { <r1> ?pre1 ?obj1 . ?obj1 ?pre2 <r2> }

will return all the predicates and object between these two resources ( in two steps) and so on I'm trying to build this query in such a way that it will automatically increase the number of objects and predicates between the two resources (for example in 4 steps)? 会在两个步骤中返回这两个资源之间的所有谓词和对象,依此类推,我试图以这种方式构建此查询,该查询将自动增加两个资源之间的对象和谓词的数量(例如4个步骤)?

I figure out how to solve this problem.. 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);
}}

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

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