final Map<String, BasicOperator> mso,
final BasicOperator rootOperator) {
final Collection<BasicOperator> deleted = new LinkedList<BasicOperator>();
final Collection<BasicOperator> added = new LinkedList<BasicOperator>();
BasicOperator operator = mso.get("operator");
final Filter filter = (Filter) mso.get("filter");
while (operator instanceof Filter)
operator = operator.getPrecedingOperators().get(0);
boolean deleteFilter = true;
boolean change = true;
while (change) {
deleteFilter = true;
change = false;
if (operator.getPrecedingOperators() != null) {
for (final BasicOperator o : operator.getPrecedingOperators()) {
if (!(o instanceof Filter && filter
.equalFilterExpression((Filter) o))) {
if (o.getUnionVariables().containsAll(
filter.getUsedVariables())) {
// System.out.println("TTTTTTTTTTTTTbefore:"+o.
// graphString
// ());
final Filter filter2 = (Filter) filter.clone();
added.add(filter2);
final OperatorIDTuple oit = o
.getOperatorIDTuple(operator);
filter2.setSucceedingOperator(new OperatorIDTuple(
operator, oit.getId()));
final HashSet<Variable> hsv = new HashSet<Variable>();
hsv.addAll(o.getIntersectionVariables());
filter2.setIntersectionVariables(hsv);
filter2.setUnionVariables(hsv);
// System.out.println("TTTTTTTTTTTTToit:"+oit);
o.replaceOperatorIDTuple(oit, new OperatorIDTuple(
filter2, 0));
operator
.setPrecedingOperators(new LinkedList<BasicOperator>());
//System.out.println("TTTTTTTTTTTTT"+o.graphString()
// );
rootOperator.deleteParents();
rootOperator.setParents();
rootOperator.detectCycles();
// should have been done manually:
// rootOperator.sendMessage(new
// BoundVariablesMessage());
change = true;
break;
} else {
if (deleteFilter) {
for (final Variable v : o.getUnionVariables())
if (filter.getUsedVariables().contains(v)) {
deleteFilter = false;
break;
}
}
}
}
}
}
}
if (deleteFilter || operator instanceof Join) {
final BasicOperator op2 = filter.getPrecedingOperators().get(0);
op2.setSucceedingOperators(filter.getSucceedingOperators());
rootOperator.deleteParents();
rootOperator.setParents();
rootOperator.sendMessage(new BoundVariablesMessage());
deleted.add(filter);
}