@Override public SearchArgument build() { if (currentTree.size() != 0) { throw new IllegalArgumentException("Failed to end " + currentTree.size() + " operations."); } ExpressionBuilder internal = new ExpressionBuilder(); ExpressionTree normalized = internal.expression(root, leaves); return new SearchArgumentImpl(normalized, internal.getLeaves()); } }
private static List<Object> getLiteralList(ExprNodeGenericFuncDesc expr, int start) { List<Object> result = new ArrayList<Object>(); List<ExprNodeDesc> children = expr.getChildren(); // ignore the first child, since it is the variable for(ExprNodeDesc child: children.subList(start, children.size())) { if (child instanceof ExprNodeConstantDesc) { result.add(boxLiteral((ExprNodeConstantDesc) child)); } else { // if we get some non-literals, we need to punt return null; } } return result; }
/** * 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; }
addChildren(result, expr, leafCache); } else if (op == GenericUDFOPAnd.class) { result = new ExpressionTree(ExpressionTree.Operator.AND); addChildren(result, expr, leafCache); } else if (op == GenericUDFOPNot.class) { result = new ExpressionTree(ExpressionTree.Operator.NOT); addChildren(result, expr, leafCache); } else if (op == GenericUDFOPEqual.class) { result = createLeaf(PredicateLeaf.Operator.EQUALS, expr, leafCache); } else if (op == GenericUDFOPNotEqual.class) { result = negate(createLeaf(PredicateLeaf.Operator.EQUALS, expr, leafCache)); } else if (op == GenericUDFOPEqualNS.class) { result = createLeaf(PredicateLeaf.Operator.NULL_SAFE_EQUALS, expr, leafCache); } else if (op == GenericUDFOPGreaterThan.class) { result = negate(createLeaf(PredicateLeaf.Operator.LESS_THAN_EQUALS, expr, leafCache)); } else if (op == GenericUDFOPEqualOrGreaterThan.class) { result = negate(createLeaf(PredicateLeaf.Operator.LESS_THAN, expr, leafCache)); } else if (op == GenericUDFOPLessThan.class) { result = createLeaf(PredicateLeaf.Operator.LESS_THAN, expr, leafCache); } else if (op == GenericUDFOPEqualOrLessThan.class) { result = createLeaf(PredicateLeaf.Operator.LESS_THAN_EQUALS, expr, leafCache); } else if (op == GenericUDFIn.class) { result = createLeaf(PredicateLeaf.Operator.IN, expr, leafCache, 0); } else if (op == GenericUDFBetween.class) {
ExpressionTree child = flatten(root.children.get(i));
root.children.set(i, convertToCNF(root.children.get(i))); if (checkCombinationsThreshold(andList)) { root = new ExpressionTree(ExpressionTree.Operator.AND); generateAllCombinations(root.children, andList, nonAndList); } else { root = new ExpressionTree(TruthValue.YES_NO_NULL);
/** * 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; }
private static Object getLiteral(ExprNodeGenericFuncDesc expr) { Object result = null; List<ExprNodeDesc> children = expr.getChildren(); if (children.size() != 2) { return null; } for(ExprNodeDesc child: children) { if (child instanceof ExprNodeConstantDesc) { if (result != null) { return null; } result = boxLiteral((ExprNodeConstantDesc) child); } } return result; }
/** * Create a leaf expression when we aren't sure where the variable is * located. * @param operator the operator type that was found * @param expression the expression to check * @param leafCache the list of leaves * @return if the expression is a sarg, return it, otherwise null */ private ExpressionTree createLeaf(PredicateLeaf.Operator operator, ExprNodeGenericFuncDesc expression, List<PredicateLeaf> leafCache) { return createLeaf(operator, expression, leafCache, findVariable(expression)); }
/** * Builds the expression and leaf list from the original predicate. * @param expression the expression to translate * @return The normalized expression. */ ExpressionTree expression(ExprNodeGenericFuncDesc expression) { List<PredicateLeaf> leafCache = new ArrayList<PredicateLeaf>(); ExpressionTree expr = parse(expression, leafCache); return expression(expr, leafCache); }
SearchArgumentImpl(ExprNodeGenericFuncDesc expr) { if (expr == null) { leaves = new ArrayList<PredicateLeaf>(); expression = null; } else { ExpressionBuilder builder = new ExpressionBuilder(); expression = builder.expression(expr); leaves = builder.getLeaves(); } }