@Override
public Tuple<Collection<BasicOperator>, Collection<BasicOperator>> transformOperatorGraph(
final Map<String, BasicOperator> mso,
final BasicOperator rootOperator) {
final ReplaceLit replaceLit = (ReplaceLit) mso.get("replaceLit");
final Projection projection = (Projection) mso.get("projection");
final Object[] projectionVars = projection.getProjectedVariables()
.toArray();
final LinkedList<Variable> replaceLitLeft = replaceLit
.getSubstitutionsLiteralLeft();
final LinkedList<Literal> replaceLitRight = replaceLit
.getSubstitutionsLiteralRight();
Variable var;
Literal lit;
// Delete all not projected Tupels from ReplaceLit
for (int i = 0; i < replaceLitLeft.size(); i++) {
var = replaceLitLeft.get(i);
if (!arrayContains(projectionVars, var)) {
lit = replaceLitRight.get(i);
replaceLit.removeSubstitution(var, lit);
}
}
final LinkedList<BasicOperator> pres = (LinkedList<BasicOperator>) replaceLit
.getPrecedingOperators();
final LinkedList<OperatorIDTuple> succs = (LinkedList<OperatorIDTuple>) projection
.getSucceedingOperators();
final int index = replaceLit.getOperatorIDTuple(projection).getId();
BasicOperator pre;
// Connect the ReplaceLit precessors directly to the Projection
for (int i = pres.size() - 1; i >= 0; i--) {
pre = pres.get(i);
projection.addPrecedingOperator(pre);
pre.removeSucceedingOperator(replaceLit);
pre.addSucceedingOperator(new OperatorIDTuple(projection, index));
}
// Make ReplaceLit the successor of Projection
projection.removePrecedingOperator(replaceLit);
projection.setSucceedingOperator(new OperatorIDTuple(replaceLit, 0));
replaceLit.setPrecedingOperator(projection);
final HashSet<Variable> hsv = new HashSet<Variable>();
hsv.addAll(projection.getIntersectionVariables());
replaceLit.setIntersectionVariables(hsv);
replaceLit.setUnionVariables(hsv);
// Connect ReplaceLit to the Projections successors instead of him
replaceLit.setSucceedingOperators(succs);