/** * Called when we leave an NotExpression. * * Checks if the expression is preceded by a Not and adds the filter in that case. * @param ctx expression context */ @Override public void exitNotExpression(GDLParser.NotExpressionContext ctx) { if (!ctx.NOT().isEmpty()) { Predicate not = new Not(currentPredicates.pop()); currentPredicates.add(not); } }
Predicate expression = not.getArguments()[0]; Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) );
/** * Converts the predicate into conjunctive normal form * * @return predicate in cnf */ public CNF asCNF() { Predicate lhs = xor.getArguments()[0]; Predicate rhs = xor.getArguments()[1]; QueryPredicate wrapper = QueryPredicate.createFrom( new Or(new And(lhs, new Not(rhs)), new And(new Not(lhs), rhs)) ); return wrapper.asCNF(); }
Predicate expression = not.getArguments()[0]; Predicate[] otherArguments = expression.getArguments(); Or or = new Or( new Not(otherArguments[0]), new Not(otherArguments[0]) ); return QueryPredicate.createFrom(or).asCNF(); Predicate[] otherArguments = expression.getArguments(); And and = new And( new Not(otherArguments[0]), new Not(otherArguments[1]) ); otherArguments[1]), new And( new Not(otherArguments[0]), new Not(otherArguments[0])) );
/** * Converts the predicate into conjunctive normal form * * @return predicate in cnf */ public CNF asCNF() { Predicate lhs = xor.getArguments()[0]; Predicate rhs = xor.getArguments()[1]; QueryPredicate wrapper = QueryPredicate.createFrom( new Or(new And(lhs, new Not(rhs)), new And(new Not(lhs), rhs)) ); return wrapper.asCNF(); }
@Test public void convertNestedXorToCnfTest() { Comparison a = getComparison(); Comparison b = getComparison(); Xor xor = new Xor(a, b); NotPredicate notPredicate = new NotPredicate(new Not(xor)); CNF reference = QueryPredicate.createFrom( new Or( new And(a, b), new And( new Not(a), new Not(b) ) ) ).asCNF(); assertEquals(reference, notPredicate.asCNF()); }
@Test public void convertToCnfTest() { Comparison a = getComparison(); Comparison b = getComparison(); XorPredicate xorPredicate = new XorPredicate(new Xor(a, b)); CNF reference = new OrPredicate( new Or( new And( a, new Not(b) ), new And( new Not(a), b ) ) ).asCNF(); assertEquals(reference, xorPredicate.asCNF()); }
@Test public void convertNestedComparisonToCnfTest() { Comparison a = getComparison(); NotPredicate notPredicate = new NotPredicate(new Not(a)); CNF reference = QueryPredicate.createFrom(invert(a)).asCNF(); assertEquals(reference, notPredicate.asCNF()); }
@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()); }
@Test public void convertNestedOrToCnfTest() { Comparison a = getComparison(); Comparison b = getComparison(); Or or = new Or(a, b); NotPredicate notPredicate = new NotPredicate(new Not(or)); CNF reference = new ComparisonExpression(invert(a)).asCNF().and(new ComparisonExpression(invert(b)).asCNF()); assertEquals(reference, notPredicate.asCNF()); }