0

is there any way to generate a SPARQL query in a dynamic way? 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

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

will return the predicate between the 2 resources and this query

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

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Hanan Mahmoud
  • 65
  • 1
  • 1
  • 10
  • Do you want multiple queries? Or you want one query that encapsulates this pattern for a given depth? Or what? It's not quite clear what you're trying to get. – Joshua Taylor Sep 05 '14 at 20:29
  • 1
    if I write 5 SPARQL queries to retrieve up to 5 relations between any two resources.. it will be a time consuming. I'm thinking some thing like that: if the two resources has one direction relation, return it, else, if there is a middle object between the two resources, return the middle object and its predicates with the two resources... and so on – Hanan Mahmoud Sep 06 '14 at 05:38
  • OK, so you want one query that returns the shortest path, so to speak. SPARQL property paths would be a nice candidate here, except that they don't provide a mechanism for extracting intermediate node, or for using variables in the path. I think you might have to use something with **union** or **optional** and writing out the different options manually. Is there any chance that you might be able to ignore the predicates? That might make the problem easier. – Joshua Taylor Sep 06 '14 at 15:01
  • Another question: do you anticipate there being paths of different lengths between r1 and r2? If there's only path, then you can get it fairly easily regardless of the length. – Joshua Taylor Sep 06 '14 at 15:07
  • both of the intermediate nodes and the predicates are very important in my work. while I was searching I found something that seems to be like what I'm looking for, except that is was written in action script I think, so I really don't get it that clear – Hanan Mahmoud Sep 08 '14 at 16:00
  • @user3293838 in [this followup question you posted](http://stackoverflow.com/questions/26065884/cant-get-any-results-from-this-sparql-query) you mention that you have found a solution to this problem. Could you post your solution here as an answer so that this question become useful for others with a similar issue? – Jeen Broekstra Sep 27 '14 at 00:56

1 Answers1

1

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);
}}
Hanan Mahmoud
  • 65
  • 1
  • 1
  • 10