final Collection<BasicOperator> added = new LinkedList<BasicOperator>();
final Filter filter = (Filter) mso.get("filter");
// delete the filter operator
final BasicOperator aboveFilter = filter.getPrecedingOperators().get(0);
if (eliminateOnlyFilter) {
deleted.add(filter);
final List<OperatorIDTuple> list = new LinkedList<OperatorIDTuple>();
list.addAll(filter.getSucceedingOperators());
for (final OperatorIDTuple oid : list) {
oid.getOperator().removePrecedingOperator(filter);
oid.getOperator().getPrecedingOperators().addAll(
filter.getPrecedingOperators());
for (final BasicOperator precFilter : filter
.getPrecedingOperators()) {
precFilter.removeSucceedingOperator(filter);
precFilter.addSucceedingOperator(new OperatorIDTuple(oid
.getOperator(), oid.getId()));
}
}
} else {
filter.visit(new SimpleOperatorGraphVisitor() {
boolean ignore = false;
public Object visit(final BasicOperator basicOperator) {
if (ignore || (basicOperator instanceof PatternMatcher)
|| (basicOperator instanceof Union))
ignore = true;
else {
deleted.add(basicOperator);
if (basicOperator instanceof Join) {
if (basicOperator.getPrecedingOperators().size() <= 2) {
for (BasicOperator prec : basicOperator
.getPrecedingOperators()) {
if (!deleted.contains(prec)
&& prec.getSucceedingOperators()
.size() == 1) {
deleted.add(prec);
while (!(prec instanceof PatternMatcher)
&& prec.getPrecedingOperators()
.size() == 1) {
prec = prec.getPrecedingOperators()
.get(0);
if (prec.getSucceedingOperators()
.size() > 1)
break;
deleted.add(prec);
}
}
}
}
}
}
return null;
}
});
BasicOperator parent = aboveFilter;
while (parent.getSucceedingOperators().size() <= 1
&& parent.getPrecedingOperators().size() == 1) {
deleted.add(parent);
parent = parent.getPrecedingOperators().get(0);
}
for (final BasicOperator basicOperator : deleted) {
final List<BasicOperator> list = new LinkedList<BasicOperator>();
list.addAll(basicOperator.getPrecedingOperators());