5

I have a problem in building the query from scratch syntactically or in algebra, based on https://jena.apache.org/documentation/query/manipulating_sparql_using_arq.html

For example I have the below query

 SELECT  (count(?instance) AS ?count)
 WHERE
 { ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
  <http://data.linkedmdb.org/resource/movie/film> }


(project (?count)
  (extend ((?count ?.0))
    (group () ((?.0 (count ?instance)))
      (bgp (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>))))) 

Can any one direct me with a sample code of how to build the above query from scratch? I have tried to build it syntactically but failing to know about how to alias the aggregation above.

If anybody can at least guide me in including aggregation with its aliasing name in projection it will be very great.

TylerH
  • 19,065
  • 49
  • 65
  • 86
Ajshk
  • 51
  • 1
  • I'm not sure what you're asking. "Query" in the SPARQL context usually means "SPARQL query", but since you gave a query, why would you need code to build it? And by "the above query", do you mean the second form? – Ben Companjen Mar 28 '13 at 21:44

2 Answers2

3

I don't typically construct queries through code, since I can just parse a query string, or use a parameterized SPARQL query, but here's a reconstruction of your query using the API. Most of the methods I used here I found by exploring the autocomplete options in Eclipse, and by looking at the Javadoc.

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.sparql.core.Var;
import com.hp.hpl.jena.sparql.expr.ExprAggregator;
import com.hp.hpl.jena.sparql.expr.ExprVar;
import com.hp.hpl.jena.sparql.expr.aggregate.AggCountVar;
import com.hp.hpl.jena.sparql.syntax.ElementTriplesBlock;
import com.hp.hpl.jena.vocabulary.RDF;

public class QueryBuilding {
    public static void main(String[] args) {
        // Create the query and make it a SELECT query.
        final Query query = QueryFactory.create();
        query.setQuerySelectType();

        // Set the projection expression.
        final ExprVar instance = new ExprVar( "instance" );
        query.getProject().add( Var.alloc( "count" ), new ExprAggregator( instance.asVar(), new AggCountVar( instance )));

        // Construct the triples pattern and add it.
        final ElementTriplesBlock triples = new ElementTriplesBlock();
        final Node film = Node.createURI( "http://data.linkedmdb.org/resource/movie/film" );
        triples.addTriple( new Triple( instance.getAsNode(), RDF.type.asNode(), film ));
        query.setQueryPattern( triples );

        // Show the query 
        System.out.println( query );
    }
}

The output (i.e., the printed query) follows. It's the same as your query, modulo some whitespace location and newlines.

SELECT  (count(?instance) AS ?count)
WHERE
  { ?instance  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>  <http://data.linkedmdb.org/resource/movie/film> .}
Joshua Taylor
  • 80,876
  • 9
  • 135
  • 306
0

Although the solution proposed by Joshua was very helpful to me and produces the correct String output, I have found that it contains a problem; the line :

query.getProject().add( Var.alloc( "count" ), new ExprAggregator( instance.asVar(), new AggCountVar( instance )));

Should be replaced by :

query.getProject().add( Var.alloc( "count" ), query.allocAggregate( new AggCountVar( instance ) ));

Otherwise if you execute the query against a Model you will get an exception "NotAVariableException: Node_variable (not a Var) found"

ThomasFrancart
  • 406
  • 3
  • 12
  • 1
    A comment on an existing solution should be posted as a comment on that answer rather than as a new answer. – Rob Hall Aug 04 '14 at 17:07
  • @ThomasFrancart Yes, had this been posted as a comment, I would have received notification, and could have updated my answer. I'll check this, and make the update accordingly. – Joshua Taylor Aug 04 '14 at 17:21
  • @Thomas Can you show an example where this doesn't work? I'm running the query that I produced against a Model, but I don't get the error you describe. Your alternative looks like it works, too, though. – Joshua Taylor Aug 04 '14 at 17:28