private static boolean checkCombinationsThreshold(List<ExpressionTree> andList) { int numComb = 1; for (ExpressionTree tree : andList) { numComb *= tree.getChildren().size(); if (numComb > CNF_COMBINATIONS_THRESHOLD) { return false; } } return true; }
@Override public Builder end() { ExpressionTree current = currentTree.removeFirst(); if (current.getChildren().size() == 0) { throw new IllegalArgumentException("Can't create expression " + root + " with no children."); } if (current.getOperator() == ExpressionTree.Operator.NOT && current.getChildren().size() != 1) { throw new IllegalArgumentException("Can't create not expression " + current + " with more than 1 child."); } return this; }
@Override public Builder startAnd() { ExpressionTree node = new ExpressionTree(ExpressionTree.Operator.AND); currentTree.getFirst().getChildren().add(node); currentTree.addFirst(node); return this; }
@Override public Builder startOr() { ExpressionTree node = new ExpressionTree(ExpressionTree.Operator.OR); currentTree.getFirst().getChildren().add(node); currentTree.addFirst(node); return this; }
@Override public Builder startNot() { ExpressionTree node = new ExpressionTree(ExpressionTree.Operator.NOT); currentTree.getFirst().getChildren().add(node); currentTree.addFirst(node); return this; }
@Override public Builder literal(TruthValue truth) { ExpressionTree parent = currentTree.getFirst(); parent.getChildren().add(new ExpressionTree(truth)); return this; }
if (expr.getChildren() != null) { for(int i=0; i < expr.getChildren().size(); ++i) { ExpressionTree child = foldMaybe(expr.getChildren().get(i)); if (child.getConstant() == TruthValue.YES_NO_NULL) { switch (expr.getOperator()) { case AND: expr.getChildren().remove(i); i -= 1; break; expr.getChildren().set(i, child); if (expr.getChildren().isEmpty()) { return new ExpressionTree(TruthValue.YES_NO_NULL);
@Override public Builder isNull(String column, PredicateLeaf.Type type) { ExpressionTree parent = currentTree.getFirst(); if (column == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.IS_NULL, type, column, null, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
if (root.getChildren() != null) { for(int i=0; i < root.getChildren().size(); ++i) { ExpressionTree child = flatten(root.getChildren().get(i)); for(ExpressionTree grandkid: child.getChildren()) { root.getChildren().set(i, grandkid); } else { root.getChildren().add(++i, grandkid); root.getChildren().set(i, child); root.getChildren().size() == 1) { return root.getChildren().get(0);
@Override public Builder nullSafeEquals(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.NULL_SAFE_EQUALS, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder lessThan(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.LESS_THAN, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder lessThanEquals(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.LESS_THAN_EQUALS, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder equals(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.EQUALS, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder between(String column, PredicateLeaf.Type type, Object lower, Object upper) { ExpressionTree parent = currentTree.getFirst(); if (column == null || lower == null || upper == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { List<Object> argList = new ArrayList<Object>(); argList.add(lower); argList.add(upper); PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.BETWEEN, type, column, null, argList, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder in(String column, PredicateLeaf.Type type, Object... literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { if (literal.length == 0) { throw new IllegalArgumentException("Can't create in expression with " + "no arguments"); } List<Object> argList = new ArrayList<Object>(); argList.addAll(Arrays.asList(literal)); PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.IN, type, column, null, argList, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
/** * Rewrite expression tree to update the leaves. * @param root the root of the tree to fix * @param leafReorder a map from old leaf ids to new leaf ids * @return the fixed root */ static ExpressionTree rewriteLeaves(ExpressionTree root, int[] leafReorder) { // The leaves could be shared in the tree. Use Set to remove the duplicates. Set<ExpressionTree> leaves = new HashSet<ExpressionTree>(); Queue<ExpressionTree> nodes = new LinkedList<ExpressionTree>(); nodes.add(root); while(!nodes.isEmpty()) { ExpressionTree node = nodes.remove(); if (node.getOperator() == ExpressionTree.Operator.LEAF) { leaves.add(node); } else { if (node.getChildren() != null){ nodes.addAll(node.getChildren()); } } } // Update the leaf in place for(ExpressionTree leaf : leaves) { leaf.setLeaf(leafReorder[leaf.getLeaf()]); } return root; }
List<ExpressionTree> nonAndList ) { List<ExpressionTree> kids = andList.get(0).getChildren(); if (result.isEmpty()) { for(ExpressionTree kid: kids) { result.add(or); for(ExpressionTree node: nonAndList) { or.getChildren().add(new ExpressionTree(node)); or.getChildren().add(kid); for(ExpressionTree or: work) { ExpressionTree copy = new ExpressionTree(or); copy.getChildren().add(kid); result.add(copy);
/** * Recursively explore the tree to find the leaves that are still reachable * after optimizations. * @param tree the node to check next * @param next the next available leaf id * @param leafReorder * @return the next available leaf id */ static int compactLeaves(ExpressionTree tree, int next, int[] leafReorder) { if (tree.getOperator() == ExpressionTree.Operator.LEAF) { int oldLeaf = tree.getLeaf(); if (leafReorder[oldLeaf] == -1) { leafReorder[oldLeaf] = next++; } } else if (tree.getChildren() != null){ for(ExpressionTree child: tree.getChildren()) { next = compactLeaves(child, next, leafReorder); } } return next; }
private static void assertNoSharedNodes(ExpressionTree tree, Set<ExpressionTree> seen ) throws Exception { if (seen.contains(tree) && tree.getOperator() != ExpressionTree.Operator.LEAF) { assertTrue("repeated node in expression " + tree, false); } seen.add(tree); if (tree.getChildren() != null) { for (ExpressionTree child : tree.getChildren()) { assertNoSharedNodes(child, seen); } } }
private static void assertNoSharedNodes(ExpressionTree tree, Set<ExpressionTree> seen ) throws Exception { if (seen.contains(tree) && tree.getOperator() != ExpressionTree.Operator.LEAF) { assertTrue("repeated node in expression " + tree, false); } seen.add(tree); if (tree.getChildren() != null) { for (ExpressionTree child : tree.getChildren()) { assertNoSharedNodes(child, seen); } } }