/** * Iterates through the expression, finding all of the leaves. It creates * the leaves list with each unique leaf that is found in the expression. * The expression is updated with the new leaf ids for each leaf. * @param expr the expression to find the leaves in * @param leafCache the list of all of the leaves * @param lookup a map that is used to uniquify the leaves * @return The potentially modified expression */ private ExpressionTree buildLeafList(ExpressionTree expr, List<PredicateLeaf> leafCache, Map<PredicateLeaf, ExpressionTree> lookup) { if (expr.children != null) { for(int i=0; i < expr.children.size(); ++i) { expr.children.set(i, buildLeafList(expr.children.get(i), leafCache, lookup)); } } else if (expr.operator == ExpressionTree.Operator.LEAF) { PredicateLeaf leaf = leafCache.get(expr.leaf); ExpressionTree val = lookup.get(leaf); if (val == null) { val = new ExpressionTree(leaves.size()); lookup.put(leaf, val); leaves.add(leaf); } return val; } return expr; }
/** * Builds the expression and optimized leaf list from a non-normalized * expression. Sets the leaves field with the unique leaves. * @param expr non-normalized expression * @param leaves non-unique leaves * @return the normalized expression */ ExpressionTree expression(ExpressionTree expr, List<PredicateLeaf> leaves) { expr = pushDownNot(expr); expr = foldMaybe(expr); expr = flatten(expr); expr = convertToCNF(expr); expr = flatten(expr); expr = buildLeafList(expr, leaves, new HashMap<PredicateLeaf, ExpressionTree>()); return expr; }