Using this will throw a "Exception in thread "main" org.apache.jena.query.QueryParseException: Line 1, column 2: Aggregate expression not legal at this point". So you may be right to use a query builder :-)
On Fri, Dec 18, 2020 at 1:29 PM Andy Seaborne <a...@apache.org> wrote: > > > On 18/12/2020 18:04, Erich Bremer wrote: > > How would one create an Expr in order to add an expression like this: > > > > SPARQLParser p = SPARQLParser.createParser(Syntax.syntaxSPARQL_11); > > Query query = new Query(); > > p.parse(query, "select ?year where {?s :Year ?year; :Fruit ?fruit} group > by > > ?year order by ?year"); > > query.addResultVar("fruits","(count(?fruit))"); > > > > > > query. addResultVar doesn't have a (String,String) but a (String,Expr) > > Expr expr = ExprUtils.parse("?x + ?y"); > > But parsing count isn't so simple - it's an aggregate function. > > Aggregator agg = AggregatorFactory.createCount(true); > Expr exprAgg = query.allocAggregate(agg) ; > query.addResultVar("hello", exprAgg); > > It may be better to use the query builder. > > Andy > > > > > SelectHandler has a private function "parseExpr" which I pulled out a > copy > > and modified it to (Query, String) to allow passing of an external Query > > rather than the SelectHandler class variable query and it worked, but, is > > there an already built in function that does the same or is there an > easier > > way to achieve the same result? - Erich > > >