@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; }
/** * 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; }
ExpressionTree child = flatten(root.getChildren().get(i)); if (child.getOperator() == root.getOperator() && child.getOperator() != ExpressionTree.Operator.NOT) { boolean first = true; for(ExpressionTree grandkid: child.getChildren()) { if ((root.getOperator() == ExpressionTree.Operator.OR || root.getOperator() == ExpressionTree.Operator.AND) && root.getChildren().size() == 1) { return root.getChildren().get(0);
root.getChildren().set(i, convertToCNF(root.getChildren().get(i))); if (root.getOperator() == ExpressionTree.Operator.OR) { if (child.getOperator() == ExpressionTree.Operator.AND) { andList.add(child); } else if (child.getOperator() == ExpressionTree.Operator.OR) {
ExpressionTree child = foldMaybe(expr.getChildren().get(i)); if (child.getConstant() == TruthValue.YES_NO_NULL) { switch (expr.getOperator()) { case AND: expr.getChildren().remove(i);
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); } } }
/** * 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; }
if (root.getOperator() == ExpressionTree.Operator.NOT) { ExpressionTree child = root.getChildren().get(0); switch (child.getOperator()) { case NOT: return pushDownNot(child.getChildren().get(0));
MessageType schema) throws Exception { FilterPredicate p = null; switch (root.getOperator()) { case OR: for(ExpressionTree child: root.getChildren()) { default: throw new IllegalStateException("Unknown operator: " + root.getOperator());
if (expr.getOperator() == Operator.LEAF) { PredicateLeaf l = leaves.get(expr.getLeaf()); if (!addLiteral(literals, l)) { return; } else if (expr.getOperator() == Operator.AND) { boolean found = false; for (ExpressionTree subExpr : expr.getChildren()) { if (subExpr.getOperator() != Operator.LEAF) { return; return; } else if (expr.getOperator() == Operator.OR) { for (ExpressionTree subExpr : expr.getChildren()) { if (subExpr.getOperator() != Operator.LEAF) { return;
if (expr.getOperator() == Operator.LEAF) { PredicateLeaf l = leaves.get(expr.getLeaf()); if (!addLiteral(literals, l)) { return; } else if (expr.getOperator() == Operator.AND) { boolean found = false; for (ExpressionTree subExpr : expr.getChildren()) { if (subExpr.getOperator() != Operator.LEAF) { return; return; } else if (expr.getOperator() == Operator.OR) { for (ExpressionTree subExpr : expr.getChildren()) { if (subExpr.getOperator() != Operator.LEAF) { return;
MessageType schema) throws Exception { FilterPredicate p = null; switch (root.getOperator()) { case OR: for(ExpressionTree child: root.getChildren()) { default: throw new IllegalStateException("Unknown operator: " + root.getOperator());
@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; }
/** * 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; }
ExpressionTree child = flatten(root.getChildren().get(i)); if (child.getOperator() == root.getOperator() && child.getOperator() != ExpressionTree.Operator.NOT) { boolean first = true; for(ExpressionTree grandkid: child.getChildren()) { if ((root.getOperator() == ExpressionTree.Operator.OR || root.getOperator() == ExpressionTree.Operator.AND) && root.getChildren().size() == 1) { return root.getChildren().get(0);
root.getChildren().set(i, convertToCNF(root.getChildren().get(i))); if (root.getOperator() == ExpressionTree.Operator.OR) { if (child.getOperator() == ExpressionTree.Operator.AND) { andList.add(child); } else if (child.getOperator() == ExpressionTree.Operator.OR) {
ExpressionTree child = foldMaybe(expr.getChildren().get(i)); if (child.getConstant() == TruthValue.YES_NO_NULL) { switch (expr.getOperator()) { case AND: expr.getChildren().remove(i);
/** * 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; }
if (root.getOperator() == ExpressionTree.Operator.NOT) { ExpressionTree child = root.getChildren().get(0); switch (child.getOperator()) { case NOT: return pushDownNot(child.getChildren().get(0));