public Predicate simplifySubTree(Predicate predicate, boolean negated) {
        if (predicate == null) {
            return null;
        }
        if (predicate instanceof Negation) {
            return simplifySubTree(((Negation) predicate).getOperand(), !negated);
        } else if (predicate instanceof Conjunction) {
            List<Predicate> in = ((Conjunction)predicate).getOperands();
            List<Predicate> out = new ArrayList<>(in.size());
            for (Predicate operand : in) {
                operand = simplifySubTree(operand, negated);
                if (operand instanceof Conjunction) {
                    out.addAll(((Conjunction)operand).getOperands());
                } else {
                    out.add(operand);
                }
            }
            if (negated) {
                return new Disjunction(out);
            }
            ((Conjunction)predicate).setOperands(out);
        } else if (predicate instanceof Disjunction) {
            List<Predicate> in = ((Disjunction)predicate).getOperands();
            List<Predicate> out = new ArrayList<>(in.size());
            for (Predicate operand : in) {
                operand = simplifySubTree(operand, negated);
                if (operand instanceof Disjunction) {
                    out.addAll(((Disjunction)operand).getOperands());
                } else {
                    out.add(operand);
                }
            }
            if (negated) {
                return new Conjunction(out);
            }
            ((Disjunction)predicate).setOperands(out);
        } else {
            if (negated) {
                return new Negation(predicate);
            }
        }
        return predicate;
    }
Simplifies a predicate by:
 - Combining child-AND/OR nodes with direct parents of the same type
 - Pushing negations down to leaf nodes by using De Morgan's law.

@author Magnar Nedland
@author bjorncs
predicate-search-core/src/main/java/com/yahoo/search/predicate/optimization/AndOrSimplifier.java