final Collection<BasicOperator> remainingJoins = new LinkedList<BasicOperator>();
final Iterator<BasicOperator> itp = newOrder.iterator();
while (itp.hasNext()) {
final BasicOperator first = itp.next();
if (itp.hasNext()) {
final Join newJoin = new Join();
added.add(newJoin);
first.setSucceedingOperator(new OperatorIDTuple(newJoin, 0));
final BasicOperator second = itp.next();
second.setSucceedingOperator(new OperatorIDTuple(newJoin, 1));
final HashSet<Variable> hv = new HashSet<Variable>();
hv.addAll(first.getUnionVariables());
hv.addAll(second.getUnionVariables());
newJoin.setUnionVariables(hv);
remainingJoins.add(newJoin);
} else {
remainingJoins.add(first);
}
}
while (remainingJoins.size() > 1) {
// choose best combination
final Collection<BasicOperator> co = getNextJoin(remainingJoins);
final Iterator<BasicOperator> io = co.iterator();
final BasicOperator first = io.next();
final BasicOperator second = io.next();
final Join join = new Join();
added.add(join);
join.setIntersectionVariables(new HashSet<Variable>());
join.setUnionVariables(new HashSet<Variable>());
join.getUnionVariables().addAll(first.getUnionVariables());
join.getUnionVariables().addAll(second.getUnionVariables());
first.setSucceedingOperator(new OperatorIDTuple(join, 0));
second.setSucceedingOperator(new OperatorIDTuple(join, 1));
remainingJoins.remove(first);
remainingJoins.remove(second);
remainingJoins.add(join);
}
return remainingJoins.iterator().next();