/** * Adds a list of predicates to the current predicates using AND conjunctions * * @param newPredicates predicates to be added */ private void addPredicates(List<Predicate> newPredicates) { for(Predicate newPredicate : newPredicates) { if(this.predicates == null) { this.predicates = newPredicate; } else { this.predicates = new And(this.predicates, newPredicate); } } }
/** * Retruns the wrapped right hand side predicate * @return the right hand side */ public QueryPredicate getRhs() { return QueryPredicate.createFrom(and.getArguments()[1]); }
/** * Processes a conjuctive expression (AND, OR, XOR) and connects the filter with the corresponding operator * * @param conjunctions list of conjunction operators */ private void processConjunctionExpression(List<TerminalNode> conjunctions) { Predicate conjunctionReuse; for (int i = conjunctions.size() - 1; i >= 0; i--) { Predicate rhs = currentPredicates.removeLast(); Predicate lhs = currentPredicates.removeLast(); switch (conjunctions.get(i).getText().toLowerCase()) { case "and": conjunctionReuse = new And(lhs, rhs); break; case "or": conjunctionReuse = new Or(lhs, rhs); break; default: conjunctionReuse = new Xor(lhs, rhs); break; } currentPredicates.add(conjunctionReuse); } }
/** * Retruns the wrapped left hand side predicate * @return the left hand side */ public QueryPredicate getLhs() { return QueryPredicate.createFrom(and.getArguments()[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(); }
/** * Retruns the wrapped right hand side predicate * @return the right hand side */ public QueryPredicate getRhs() { return QueryPredicate.createFrom(and.getArguments()[1]); }
/** * 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(); }
/** * Retruns the wrapped left hand side predicate * @return the left hand side */ public QueryPredicate getLhs() { return QueryPredicate.createFrom(and.getArguments()[0]); }
@Test public void testGetRhs() { Comparison a = getComparison(); Comparison b = getComparison(); AndPredicate andPredicate = new AndPredicate(new And(a, b)); assertEquals(new ComparisonExpression(b), andPredicate.getRhs()); }
@Test public void testGetLhs() { Comparison a = getComparison(); Comparison b = getComparison(); AndPredicate andPredicate = new AndPredicate(new And(a, b)); assertEquals(new ComparisonExpression(a), andPredicate.getLhs()); }
@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 convertToCnfTest() { Comparison a = getComparison(); Comparison b = getComparison(); AndPredicate andPredicate = new AndPredicate(new And(a, b)); CNF reference = andPredicate.getLhs().asCNF().and(andPredicate.getRhs().asCNF()); assertEquals(reference, andPredicate.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 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()); }