}
private static void processAggregationFunction(final QueryResult queryResult, final lupos.sparql1_1.Node n, final HashMap<lupos.sparql1_1.Node, Object> resultsOfAggregationFunctions, final EvaluationVisitor<Map<Node, Object>, Object> evaluationVisitor) {
final boolean childAdded = false;
if (isAggregationFunction(n)) {
final ASTAggregation aggregation = (ASTAggregation) n;
Object result = null;
if (n.jjtGetNumChildren()>0 && isConstant(n.jjtGetChild(0))) {
try {
final lupos.sparql1_1.Node node=n.jjtGetChild(0);
final Object operand = Filter.staticEvalTree(null, node,
resultsOfAggregationFunctions, evaluationVisitor);
if (childAdded) {
n.clearChildren();
}
final Iterator<Object> values = new ImmutableIterator<Object>() {
Object next = operand;
@Override
public boolean hasNext() {
return this.next != null;
}
@Override
public Object next() {
final Object znext = this.next;
this.next = null;
return znext;
}
};
result = aggregation.applyAggregation(
evaluationVisitor, values);
} catch (final NotBoundException e) {
System.err.println(e);
e.printStackTrace();
} catch (final TypeErrorException e) {
System.err.println(e);
e.printStackTrace();
}
} else {
Iterator<? extends Object> values = (n.jjtGetNumChildren()==0)?queryResult.iterator():new ImmutableIterator<Object>() {
final lupos.sparql1_1.Node node=n.jjtGetChild(0);
Iterator<Bindings> iterator = queryResult.iterator();
Object next = null;
@Override
public boolean hasNext() {
if (this.next != null) {
return true;
}
this.next = this.next();
return (this.next != null);
}
@Override
public Object next() {
if (this.next != null) {
final Object znext = this.next;
this.next = null;
return znext;
}
while (this.iterator.hasNext()) {
final Bindings b = this.iterator.next();
try {
return Filter.staticEvalTree(b, this.node,
resultsOfAggregationFunctions, evaluationVisitor);
} catch (final Exception e) {
// just ignore bindings with error!
}
}
return null;
}
};
if (aggregation.isDistinct()) {
// first just implement an in-memory distinct
// TODO implement also disk-based duplicate elimination
// (just like physical operators for DISTINCT)
final Iterator<? extends Object> oldIterator = values;
values = new ImmutableIterator<Object>() {
HashSet<Object> alreadyUsedObjects = new HashSet<Object>();
Object next = null;
@Override
public boolean hasNext() {
if (this.next != null) {
return true;
}
this.next = this.next();
return (this.next != null);
}
@Override
public Object next() {
if (this.next != null) {
final Object znext = this.next;
this.next = null;
return znext;
}
while (oldIterator.hasNext()) {
final Object o = oldIterator.next();
if (!this.alreadyUsedObjects.contains(o)) {
this.alreadyUsedObjects.add(o);
return o;
}
}
return null;
}
};
}
result = aggregation.applyAggregation(evaluationVisitor,
values);
}
if (result != null) {
resultsOfAggregationFunctions.put(n, result);