[ 
https://issues.apache.org/jira/browse/JENA-803?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14259704#comment-14259704
 ] 

Andy Seaborne commented on JENA-803:
------------------------------------

An example:
{noformat}
    // Functionality is defined by an accumulator, 
    // An accumulator is applied to all the elements in a group
    static AccumulatorFactory f = new AccumulatorFactory() {
            @Override
            public Accumulator createAccumulator(AggCustom agg) {
                return new Accumulator() {
                    int count = 0 ;
                    
                    @Override
                    public void accumulate(Binding binding, FunctionEnv 
functionEnv) {
                        
                        ExprList exprList = agg.getExprList() ;
                        for(Expr expr: exprList) {
                            try {
                                NodeValue nv = expr.eval(binding, functionEnv) ;
                                if ( nv.isLiteral() )
                                    count ++ ;
                            } catch (ExprEvalException ex) {}
                        }
                    }

                    @Override
                    public NodeValue getValue() {
                        return NodeValue.makeInteger(count) ;
                    }} ;
            }
        } ;
{noformat}
Custom aggregates must be registered by URI before use:
{noformat}
        public static void main(String[] args) {
        String aggUri = "http://example/countLiterals"; ;

        Graph g = SSE.parseGraph("(graph (:s :p :o) (:s :p 1))") ;
        AggregateRegistry.register(aggUri, f);

        String qs = "SELECT (AGG <http://example/countLiterals>(?s,?o) AS 
?X)\n{ ?s ?p ?o }" ;
        Query q = QueryFactory.create(qs, Syntax.syntaxARQ) ;
        try ( QueryExecution qexec = QueryExecutionFactory.create(q, 
ModelFactory.createModelForGraph(g)) ) {
            ResultSet rs = qexec.execSelect() ;
            ResultSetFormatter.out(rs);
        }
        
        System.out.println(q) ;
    }
{noformat}

Currently, parsing requires the {{AGG}} trigger.  This is temporary until 
further parser changes are made.

> Custom aggregates in SPARQL.
> ----------------------------
>
>                 Key: JENA-803
>                 URL: https://issues.apache.org/jira/browse/JENA-803
>             Project: Apache Jena
>          Issue Type: New Feature
>          Components: ARQ
>    Affects Versions: Jena 2.12.1
>            Reporter: Andy Seaborne
>            Assignee: Andy Seaborne
>
> Support SPARQL custom aggregates.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to