-1

I've asked a question before here

Generate an incremental SPARQL query

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:

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

Community
  • 1
  • 1
Hanan Mahmoud
  • 65
  • 1
  • 1
  • 10
  • 1
    Could you post that solution you found as an actual answer to your earlier question? That way, other people with a similar problem will more easily find it. – Jeen Broekstra Sep 26 '14 at 20:25
  • Also, "it's not working" is not much to go on. What happens when you try: do you get an error? If so, what is the error message and where exactly in your code does it occur? If you get no error but an unexpected result: what result did you expect, and what did you actually get? Please [edit your question](http://stackoverflow.com/posts/26065884/edit) to provide these necessary details. See [how to ask](http://stackoverflow.com/help/how-to-ask) for more details on how to write answerable questions. – Jeen Broekstra Sep 26 '14 at 20:29
  • 2
    sol.get("?abstract") is going to be null - there is no ?abstract in the query string. – AndyS Sep 26 '14 at 22:20
  • @AndyS I spotted that too, but it's not clear from the question whether that is actually the problem the OP is encountering. – Jeen Broekstra Sep 27 '14 at 00:16
  • If you managed to find a solution to the earlier question, you should post an answer to it, or delete the question. Part of the benefit of Stack Overflow is that *everyone* benefits from answers to question. Will anyone else be motivated to answer your questions if you're not willing to? – Joshua Taylor Sep 27 '14 at 04:11
  • 1
    morning to all of you, and thanks for your replay, I know there is no ?abstract in any of my queries, that why I said (for example), if my select query goes like: select ?abstract, it will be easy to use sol.get("?abstract"), but here I use select *..., how can I manage to use get.sol here... and I will answer the earlier Q right now.. thanks for mention that – Hanan Mahmoud Sep 27 '14 at 04:19

1 Answers1

3

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));
       }
 }
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Thanks for your answer I appreciate it. but I've this error:( Can't find symbol: method getValue(String)) – Hanan Mahmoud Oct 01 '14 at 14:16
  • I replace getValue with getResource and it was perfect!! thanx again for ur help – Hanan Mahmoud Oct 01 '14 at 14:36
  • 1
    Ah, that's my bias showing through: `getValue` is the method to get use when working with the Sesame API, rather than Jena. Fixed now. You should probably use `get()` instead of `getResource()` though: not every variable binding is necessarily a resource (some may be literals), in which using `getResource` will probably cause an exception. – Jeen Broekstra Oct 01 '14 at 20:58