private IBindingSet aggregate(final Iterable<IBindingSet> solutions){
  final IBindingSet aggregates=new ContextBindingSet(context,new ListBindingSet());
  if (groupBy != null) {
    final IBindingSet aSolution=solutions.iterator().next();
    for (    IValueExpression<?> expr : groupBy) {
      if (expr instanceof IVariable<?>) {
        final IVariable<?> var=(IVariable<?>)expr;
        final Object varValue=var.get(aSolution);
        final Constant<?> val;
        if (varValue == null) {
          val=Constant.errorValue();
        }
 else {
          val=new Constant(varValue.getClass().cast(varValue));
        }
        ;
        aggregates.set(var,val);
      }
 else       if (expr instanceof IBind<?>) {
        final IBind<?> bindExpr=(IBind<?>)expr;
        final Constant<?> val;
        final Object exprValue=bindExpr.get(aSolution);
        if (exprValue == null) {
          val=Constant.errorValue();
        }
 else {
          val=new Constant(exprValue.getClass().cast(exprValue));
        }
        final IVariable<?> ovar=((IBind<?>)expr).getVar();
        aggregates.set(ovar,val);
      }
    }
  }
{
    final boolean nestedAggregates=groupByState.isNestedAggregates();
    final Iterator<Map.Entry<IAggregate<?>,IVariable<?>>> itr=rewrite.getAggExpr().entrySet().iterator();
    while (itr.hasNext()) {
      final Map.Entry<IAggregate<?>,IVariable<?>> e=itr.next();
      doAggregate(e.getKey(),e.getValue(),nestedAggregates,aggregates,solutions,stats);
    }
    if (log.isTraceEnabled())     log.trace("aggregates: " + aggregates);
  }
  for (  IValueExpression<?> expr : rewrite.getSelect2()) {
    try {
      expr.get(aggregates);
    }
 catch (    SparqlTypeErrorException ex) {
      TypeErrorLog.handleTypeError(ex,expr,stats);
      continue;
    }
catch (    IllegalArgumentException ex) {
      TypeErrorLog.handleTypeError(ex,expr,stats);
      continue;
    }
  }
{
    final boolean drop;
    final IConstraint[] having2=rewrite.getHaving2();
    if (having2 != null && !BOpUtility.isConsistent(having2,aggregates)) {
      drop=true;
    }
 else {
      drop=false;
    }
    if (log.isInfoEnabled())     log.info((drop ? "drop" : "keep") + " : " + aggregates);
    if (drop) {
      return null;
    }
  }
  final IBindingSet out;
  if (groupBy == null) {
    assert !aggregates.containsErrorValues();
    out=aggregates.copy(groupByState.getSelectVars().toArray(new IVariable[0]));
  }
 else {
    out=aggregates.copyMinusErrors(groupByState.getSelectVars().toArray(new IVariable[0]));
  }
  return out;
}

private IBindingSet aggregate ( final Iterable < IBindingSet > solutions ) { final IBindingSet aggregates = new ContextBindingSet ( context , new ListBindingSet ( ) ) ; if ( groupBy != null ) { final IBindingSet aSolution = solutions . iterator ( ) . next ( ) ; for ( IValueExpression < ? > expr : groupBy ) { if ( expr instanceof IVariable < ? > ) { final IVariable < ? > var = ( IVariable < ? > ) expr ; final Object varValue = var . get ( aSolution ) ; final Constant < ? > val ; if ( varValue == null ) { val = Constant . errorValue ( ) ; } else { val = new Constant ( varValue . getClass ( ) . cast ( varValue ) ) ; } ; aggregates . set ( var , val ) ; } else if ( expr instanceof IBind < ? > ) { final IBind < ? > bindExpr = ( IBind < ? > ) expr ; final Constant < ? > val ; final Object exprValue = bindExpr . get ( aSolution ) ; if ( exprValue == null ) { val = Constant . errorValue ( ) ; } else { val = new Constant ( exprValue . getClass ( ) . cast ( exprValue ) ) ; } final IVariable < ? > ovar = ( ( IBind < ? > ) expr ) . getVar ( ) ; aggregates . set ( ovar , val ) ; } } } { final boolean nestedAggregates = groupByState . isNestedAggregates ( ) ; final Iterator < Map . Entry < IAggregate < ? > , IVariable < ? > > > itr = rewrite . getAggExpr ( ) . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { final Map . Entry < IAggregate < ? > , IVariable < ? > > e = itr . next ( ) ; doAggregate ( e . getKey ( ) , e . getValue ( ) , nestedAggregates , aggregates , solutions , stats ) ; } if ( log . isTraceEnabled ( ) ) log . trace ( "aggregates: " + aggregates ) ; } for ( IValueExpression < ? > expr : rewrite . getSelect2 ( ) ) { try { expr . get ( aggregates ) ; } catch ( SparqlTypeErrorException ex ) { TypeErrorLog . handleTypeError ( ex , expr , stats ) ; continue ; } catch ( IllegalArgumentException ex ) { TypeErrorLog . handleTypeError ( ex , expr , stats ) ; continue ; } } { final boolean drop ; final IConstraint [ ] having2 = rewrite . getHaving2 ( ) ; if ( having2 != null && ! BOpUtility . isConsistent ( having2 , aggregates ) ) { drop = true ; } else { drop = false ; } if ( log . isInfoEnabled ( ) ) log . info ( ( drop ? "drop" : "keep" ) + " : " + aggregates ) ; if ( drop ) { return null ; } } final IBindingSet out ; if ( groupBy == null ) { assert ! aggregates . containsErrorValues ( ) ; out = aggregates . copy ( groupByState . getSelectVars ( ) . toArray ( new IVariable [ 0 ] ) ) ; } else { out = aggregates . copyMinusErrors ( groupByState . getSelectVars ( ) . toArray ( new IVariable [ 0 ] ) ) ; } return out ; }
compute the aggregate solution for a solution multiset ( aka a group ) .
