Search code examples
javasparqlsesame

How to print the retrieved statements in SPARQL after executing a query


I am quite new to Java Sesame and SPARQL. I have added statements into the Sesame repository and am now trying to retrieve some statements and simply print them. From what I understood that I need to parse the query in order to print the retrieved statements. The code below shows where I have reached so far:

String queryString = "SELECT ?subject ?object WHERE { ?subject  <http://example.org/is> ?object . } LIMIT 1";
...
SPARQLParser parser = new SPARQLParser();
ParsedQuery query = parser.parseQuery(queryString, null);

StatementPatternCollector collector = new StatementPatternCollector();
query.getTupleExpr().visit(collector);

List<StatementPattern> patterns = collector.getStatementPatterns();
// To print the first statement only for example. 
System.out.println(patterns.get(0));

Here is the output:

StatementPattern
Var (name=name)
Var (name=-const-1, value=http://example.org/is, anonymous)
Var (name=object)

According to the output, it does not show me the subject and object. My questions are: How can I print the results of the query as shown above. Is this code the right way to parse the query for printing the statements later. Your help would be very much appreciated.


Solution

  • You get the results of a query by evaluating the query, not by parsing it. What your code does is inspect the query's algebra model and retrieve patterns from that algebra. This approach will not give you query results.

    Evaluating a query in Sesame is far simpler than this:

     // open a connection to the Sesame repository containing your statements
     RepositoryConnection conn = repository.getConnection();
     try {
         // create a prepared query object from your query string
         TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    
         // evaluate the query on the repository
         TupleQueryResult result = query.evaluate(); 
    
         // iterate over the results and do something with each result
         while (result.hasNext()) {
              BindingSet s = result.next();
              Value subject = s.getValue("subject");
              Value object = s.getValue("object");
    
              System.out.println("value of ?subject: " + subject);
              System.out.println("value of ?object: " + object);
         }
     } finally {
           conn.close();
     }
    

    As Joshua pointed out, your query retrieves variable bindings (for the variables mentioned in your SELECT clause), not RDF statements. You can of course re-create the RDF statement from those variable bindings in Java, but if you really want full statements rather than just the subject and object, it's probably easier to switch to using a SPARQL CONSTRUCT query instead of a SELECT query.

    For more information on how to evaluate different kinds of queries and work with the results in Sesame, see the Sesame user documentation (section 6.5 in particular) and the API Javadoc.