@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))))
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); } } }
@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)), and(leaf(2), leaf(3)),
@Test public void testCNF() throws Exception { assertEquals("leaf-1", SearchArgumentImpl.BuilderImpl.convertToCNF(leaf(1)). toString()); assertEquals("NO", SearchArgumentImpl.BuilderImpl.convertToCNF( constant(TruthValue.NO)).toString()); assertEquals("(not leaf-1)", SearchArgumentImpl.BuilderImpl.convertToCNF( not(leaf(1))).toString()); assertEquals("(and leaf-1 leaf-2)", SearchArgumentImpl.BuilderImpl. convertToCNF( and(leaf(1), leaf(2))).toString()); assertEquals("(or (not leaf-1) leaf-2)", SearchArgumentImpl.BuilderImpl. convertToCNF( or(not(leaf(1)), leaf(2))).toString()); assertEquals("(and (or leaf-1 leaf-2) (not leaf-3))", SearchArgumentImpl.BuilderImpl.convertToCNF( and(or(leaf(1), leaf(2)), not(leaf(3)))).toString()); assertEquals("(and (or leaf-1 leaf-3) (or leaf-2 leaf-3)" + " (or leaf-1 leaf-4) (or leaf-2 leaf-4))", SearchArgumentImpl.BuilderImpl.convertToCNF( or(and(leaf(1), leaf(2)), and(leaf(3), leaf(4)))).toString()); assertEquals("(and" + " (or leaf-1 leaf-5) (or leaf-2 leaf-5)" + " (or leaf-3 leaf-6) (or leaf-4 leaf-6))", SearchArgumentImpl.BuilderImpl.convertToCNF( or(and(leaf(1), leaf(2), leaf(3), leaf(4)), and(leaf(5), leaf(6)))).toString()); assertEquals("(and" + " (or leaf-5 leaf-6 (not leaf-7) leaf-1 leaf-3)" +
@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());