@Test public void convertNestedAndToCnfTest() { Comparison a = getComparison(); Comparison b = getComparison(); And and = new And(a, b); NotPredicate notPredicate = new NotPredicate(new Not(and)); CNF reference = new ComparisonExpression(invert(a)).asCNF().or(new ComparisonExpression(invert(b)).asCNF()); assertEquals(reference, notPredicate.asCNF()); }
@Override public CNF asCNF() { Predicate expression = not.getArguments()[0]; if (expression.getClass() == Comparison.class) { CNF cnf = new CNF(); CNFElement cnfElement = new CNFElement(); cnfElement.addPredicate(new ComparisonExpression(invertComparison((Comparison) expression))); cnf.addPredicate(cnfElement); return cnf; } else if (expression.getClass() == Not.class) { return QueryPredicate.createFrom(expression.getArguments()[0]).asCNF(); } else if (expression.getClass() == And.class) { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); } else if (expression.getClass() == Or.class) { Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); return QueryPredicate.createFrom(and).asCNF(); } else { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new And( otherArguments[0], otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) ); return QueryPredicate.createFrom(or).asCNF(); } }
NotPredicate extends QueryPredicate { @Override public CNF asCNF() { Predicate expression = not.getArguments()[0]; if (expression.getClass() == Comparison.class) { CNF cnf = new CNF(); CNFElement cnfElement = new CNFElement(); cnfElement.addPredicate(new ComparisonExpression(invertComparison((Comparison) expression))); cnf.addPredicate(cnfElement); return cnf; } else if (expression.getClass() == Not.class) { return QueryPredicate.createFrom(expression.getArguments()[0]).asCNF(); } else if (expression.getClass() == And.class) { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); } else if (expression.getClass() == Or.class) { Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); return QueryPredicate.createFrom(and).asCNF(); } else { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new And( otherArguments[0], otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) ); return QueryPredicate.createFrom(or).asCNF(); } } }
NotPredicate extends QueryPredicate { @Override public CNF asCNF() { Predicate expression = not.getArguments()[0]; if (expression.getClass() == Comparison.class) { CNF cnf = new CNF(); CNFElement cnfElement = new CNFElement(); cnfElement.addPredicate(new ComparisonExpression(invertComparison((Comparison) expression))); cnf.addPredicate(cnfElement); return cnf; } else if (expression.getClass() == Not.class) { return QueryPredicate.createFrom(expression.getArguments()[0]).asCNF(); } else if (expression.getClass() == And.class) { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); } else if (expression.getClass() == Or.class) { Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); return QueryPredicate.createFrom(and).asCNF(); } else { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new And( otherArguments[0], otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) ); return QueryPredicate.createFrom(or).asCNF(); } } NotPredicate(Not not); }
NotPredicate extends QueryPredicate { @Override public CNF asCNF() { Predicate expression = not.getArguments()[0]; if (expression.getClass() == Comparison.class) { CNF cnf = new CNF(); CNFElement cnfElement = new CNFElement(); cnfElement.addPredicate(new ComparisonExpression(invertComparison((Comparison) expression))); cnf.addPredicate(cnfElement); return cnf; } else if (expression.getClass() == Not.class) { return QueryPredicate.createFrom(expression.getArguments()[0]).asCNF(); } else if (expression.getClass() == And.class) { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); } else if (expression.getClass() == Or.class) { Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); return QueryPredicate.createFrom(and).asCNF(); } else { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new And( otherArguments[0], otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) ); return QueryPredicate.createFrom(or).asCNF(); } } NotPredicate(Not not); @Override CNF asCNF(); @Override boolean equals(Object o); @Override int hashCode(); }
NotPredicate extends QueryPredicate { @Override public CNF asCNF() { Predicate expression = not.getArguments()[0]; if (expression.getClass() == Comparison.class) { CNF cnf = new CNF(); CNFElement cnfElement = new CNFElement(); cnfElement.addPredicate(new ComparisonExpression(invertComparison((Comparison) expression))); cnf.addPredicate(cnfElement); return cnf; } else if (expression.getClass() == Not.class) { return QueryPredicate.createFrom(expression.getArguments()[0]).asCNF(); } else if (expression.getClass() == And.class) { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); } else if (expression.getClass() == Or.class) { Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); return QueryPredicate.createFrom(and).asCNF(); } else { Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new And( otherArguments[0], otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) ); return QueryPredicate.createFrom(or).asCNF(); } } NotPredicate(Not not); @Override CNF asCNF(); @Override boolean equals(Object o); @Override int hashCode();  }