@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); }
switch (child.getOperator()) { case NOT: return pushDownNot(child.getChildren().get(0)); case CONSTANT: return new ExpressionTree(child.getConstant().not()); root = new ExpressionTree(ExpressionTree.Operator.OR); for(ExpressionTree kid: child.getChildren()) { root.getChildren().add(pushDownNot(new ExpressionTree(ExpressionTree.Operator.NOT, kid))); root = new ExpressionTree(ExpressionTree.Operator.AND); for(ExpressionTree kid: child.getChildren()) { root.getChildren().add(pushDownNot(new ExpressionTree (ExpressionTree.Operator.NOT, kid))); root.getChildren().set(i, pushDownNot(root.getChildren().get(i)));
@Test public void testNotPushdown() throws Exception { assertEquals("leaf-1", SearchArgumentImpl.BuilderImpl.pushDownNot(leaf(1)) .toString()); assertEquals("(not leaf-1)", SearchArgumentImpl.BuilderImpl.pushDownNot(not(leaf(1))).toString()); assertEquals("leaf-1", SearchArgumentImpl.BuilderImpl.pushDownNot(not(not(leaf(1)))) .toString()); assertEquals("(not leaf-1)", SearchArgumentImpl.BuilderImpl.pushDownNot(not(not(not(leaf(1))))). toString()); assertEquals("(or leaf-1 (not leaf-2))", SearchArgumentImpl.BuilderImpl.pushDownNot(not(and(not(leaf(1)), leaf(2)))).toString()); assertEquals("(and (not leaf-1) leaf-2)", SearchArgumentImpl.BuilderImpl.pushDownNot(not(or(leaf(1), not(leaf(2))))).toString()); assertEquals("(or (or (not leaf-1) leaf-2) leaf-3)", SearchArgumentImpl.BuilderImpl.pushDownNot(or(not(and(leaf(1), not(leaf(2)))), not(not(leaf(3))))).toString()); assertEquals("NO", SearchArgumentImpl.BuilderImpl.pushDownNot( not(constant(TruthValue.YES))).toString()); assertEquals("YES", SearchArgumentImpl.BuilderImpl.pushDownNot( not(constant(TruthValue.NO))).toString()); assertEquals("NULL", SearchArgumentImpl.BuilderImpl.pushDownNot( not(constant(TruthValue.NULL))).toString()); assertEquals("YES_NO", SearchArgumentImpl.BuilderImpl.pushDownNot( not(constant(TruthValue.YES_NO))).toString());
switch (child.getOperator()) { case NOT: return pushDownNot(child.getChildren().get(0)); case CONSTANT: return new ExpressionTree(child.getConstant().not()); root = new ExpressionTree(ExpressionTree.Operator.OR); for(ExpressionTree kid: child.getChildren()) { root.getChildren().add(pushDownNot(new ExpressionTree(ExpressionTree.Operator.NOT, kid))); root = new ExpressionTree(ExpressionTree.Operator.AND); for(ExpressionTree kid: child.getChildren()) { root.getChildren().add(pushDownNot(new ExpressionTree (ExpressionTree.Operator.NOT, kid))); root.getChildren().set(i, pushDownNot(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); }