问题
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 ....
回答1:
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));
}
}
来源:https://stackoverflow.com/questions/26065884/cant-get-any-results-from-this-sparql-query