@Override public SearchArgument build() { if (currentTree.size() != 1) { throw new IllegalArgumentException("Failed to end " + currentTree.size() + " operations."); } ExpressionTree optimized = pushDownNot(root); optimized = foldMaybe(optimized); optimized = flatten(optimized); optimized = convertToCNF(optimized); optimized = flatten(optimized); int leafReorder[] = new int[leaves.size()]; Arrays.fill(leafReorder, -1); int newLeafCount = compactLeaves(optimized, 0, leafReorder); optimized = rewriteLeaves(optimized, leafReorder); ArrayList<PredicateLeaf> leafList = new ArrayList<>(newLeafCount); // expand list to correct size for(int i=0; i < newLeafCount; ++i) { leafList.add(null); } // build the new list for(Map.Entry<PredicateLeaf, Integer> elem: leaves.entrySet()) { int newLoc = leafReorder[elem.getValue()]; if (newLoc != -1) { leafList.set(newLoc, elem.getKey()); } } return new SearchArgumentImpl(optimized, leafList); }
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()) {
@Test public void testFoldMaybe() throws Exception { assertEquals("(and leaf-1)", SearchArgumentImpl.BuilderImpl.foldMaybe(and(leaf(1), constant(TruthValue.YES_NO_NULL))).toString()); assertEquals("(and leaf-1 leaf-2)", SearchArgumentImpl.BuilderImpl.foldMaybe(and(leaf(1), constant(TruthValue.YES_NO_NULL), leaf(2))).toString()); assertEquals("(and leaf-1 leaf-2)", SearchArgumentImpl.BuilderImpl. foldMaybe(and(constant(TruthValue.YES_NO_NULL), leaf(1), leaf(2), constant(TruthValue.YES_NO_NULL))).toString()); assertEquals("YES_NO_NULL", SearchArgumentImpl.BuilderImpl. foldMaybe(and(constant(TruthValue.YES_NO_NULL), constant(TruthValue.YES_NO_NULL))).toString()); assertEquals("YES_NO_NULL", SearchArgumentImpl.BuilderImpl. foldMaybe(or(leaf(1), constant(TruthValue.YES_NO_NULL))).toString()); assertEquals("(or leaf-1 (and leaf-2))", SearchArgumentImpl.BuilderImpl.foldMaybe(or(leaf(1), and(leaf(2), constant(TruthValue.YES_NO_NULL)))).toString()); assertEquals("(and leaf-1)", SearchArgumentImpl.BuilderImpl.foldMaybe(and(or(leaf(2), constant(TruthValue.YES_NO_NULL)), leaf(1))).toString()); assertEquals("(and leaf-100)", SearchArgumentImpl.BuilderImpl.foldMaybe( SearchArgumentImpl.BuilderImpl.convertToCNF(and(leaf(100), or(and(leaf(0), leaf(1)),
@Override public SearchArgument build() { if (currentTree.size() != 1) { throw new IllegalArgumentException("Failed to end " + currentTree.size() + " operations."); } ExpressionTree optimized = pushDownNot(root); optimized = foldMaybe(optimized); optimized = flatten(optimized); optimized = convertToCNF(optimized); optimized = flatten(optimized); int leafReorder[] = new int[leaves.size()]; Arrays.fill(leafReorder, -1); int newLeafCount = compactLeaves(optimized, 0, leafReorder); optimized = rewriteLeaves(optimized, leafReorder); ArrayList<PredicateLeaf> leafList = new ArrayList<>(newLeafCount); // expand list to correct size for(int i=0; i < newLeafCount; ++i) { leafList.add(null); } // build the new list for(Map.Entry<PredicateLeaf, Integer> elem: leaves.entrySet()) { int newLoc = leafReorder[elem.getValue()]; if (newLoc != -1) { leafList.set(newLoc, elem.getKey()); } } return new SearchArgumentImpl(optimized, leafList); }
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()) {