@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); }
ExpressionTree child = flatten(root.getChildren().get(i));
@Test public void testFlatten() throws Exception { assertEquals("leaf-1", SearchArgumentImpl.BuilderImpl.flatten(leaf(1)).toString()); assertEquals("NO", SearchArgumentImpl.BuilderImpl.flatten(constant(TruthValue.NO)).toString()); assertEquals("(not (not leaf-1))", SearchArgumentImpl.BuilderImpl.flatten(not(not(leaf(1)))).toString()); assertEquals("(and leaf-1 leaf-2)", SearchArgumentImpl.BuilderImpl.flatten(and(leaf(1), leaf(2))).toString()); assertEquals("(and (or leaf-1 leaf-2) leaf-3)", SearchArgumentImpl.BuilderImpl.flatten(and(or(leaf(1), leaf(2)), leaf(3)) ).toString()); assertEquals("(and leaf-1 leaf-2 leaf-3 leaf-4)", SearchArgumentImpl.BuilderImpl.flatten(and(and(leaf(1), leaf(2)), and(leaf(3), leaf(4)))).toString()); assertEquals("(or leaf-1 leaf-2 leaf-3 leaf-4)", SearchArgumentImpl.BuilderImpl.flatten(or(leaf(1), or(leaf(2), or(leaf(3), leaf(4))))).toString()); assertEquals("(or leaf-1 leaf-2 leaf-3 leaf-4)", SearchArgumentImpl.BuilderImpl.flatten(or(or(or(leaf(1), leaf(2)), leaf(3)), leaf(4))).toString()); assertEquals("(or leaf-1 leaf-2 leaf-3 leaf-4 leaf-5 leaf-6)", SearchArgumentImpl.BuilderImpl.flatten(or(or(leaf(1), or(leaf(2), leaf(3))), or(or(leaf(4), leaf(5)), leaf(6)))).toString()); assertEquals("(and (not leaf-1) leaf-2 (not leaf-3) leaf-4 (not leaf-5) leaf-6)", SearchArgumentImpl.BuilderImpl.flatten(and(and(not(leaf(1)), and(leaf(2), not(leaf(3)))), and(and(leaf(4), not(leaf(5))), leaf(6))) ).toString()); assertEquals("(not (and leaf-1 leaf-2 leaf-3))", SearchArgumentImpl.BuilderImpl.flatten(not(and(leaf(1), and(leaf(2), leaf(3))))
ExpressionTree child = flatten(root.getChildren().get(i));
@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); }