use of org.batfish.z3.expr.AndExpr in project batfish by batfish.
the class SimplifierTest method testSimplifyAnd1.
/**
* Test that an AND node with a single child (other than TRUE or FALSE) simplifies to that child.
*/
@Test
public void testSimplifyAnd1() {
BooleanExpr p1 = newAtom();
AndExpr and = new AndExpr(of(p1));
assertThat(simplifyBooleanExpr(and), equalTo(p1));
}
use of org.batfish.z3.expr.AndExpr in project batfish by batfish.
the class SimplifierTest method testSimplifyAndTrue.
/**
* Test that any TRUE children of an AND node are removed.
*/
@Test
public void testSimplifyAndTrue() {
BooleanExpr p1 = newAtom();
BooleanExpr p2 = newAtom();
AndExpr and = new AndExpr(of(TrueExpr.INSTANCE, p1, TrueExpr.INSTANCE, p2));
assertThat(simplifyBooleanExpr(and), equalTo(new AndExpr(of(p1, p2))));
}
use of org.batfish.z3.expr.AndExpr in project batfish by batfish.
the class Simplifier method visitAndExpr.
@Override
public BooleanExpr visitAndExpr(AndExpr andExpr) {
boolean changed = false;
List<BooleanExpr> oldConjuncts = andExpr.getConjuncts();
ImmutableList.Builder<BooleanExpr> newConjunctsBuilder = ImmutableList.builder();
// first check for nested ANDs
if (oldConjuncts.stream().anyMatch(Predicates.instanceOf(AndExpr.class))) {
return simplifyBooleanExpr(new AndExpr(oldConjuncts.stream().flatMap(conjunct -> conjunct instanceof AndExpr ? ((AndExpr) conjunct).getConjuncts().stream() : Stream.of(conjunct)).collect(ImmutableList.toImmutableList())));
}
// no nested ANDs, so just simplify all conjuncts
for (BooleanExpr conjunct : oldConjuncts) {
BooleanExpr simplifiedConjunct = simplifyBooleanExpr(conjunct);
if (conjunct != simplifiedConjunct) {
changed = true;
}
if (simplifiedConjunct == FalseExpr.INSTANCE) {
return FalseExpr.INSTANCE;
} else if (simplifiedConjunct != TrueExpr.INSTANCE) {
newConjunctsBuilder.add(simplifiedConjunct);
} else {
changed = true;
}
}
List<BooleanExpr> newConjuncts = newConjunctsBuilder.build();
if (newConjuncts.size() == 0) {
return TrueExpr.INSTANCE;
} else if (newConjuncts.size() == 1) {
return newConjuncts.get(0);
} else if (!changed) {
return andExpr;
} else {
return new AndExpr(newConjuncts);
}
}
use of org.batfish.z3.expr.AndExpr in project batfish by batfish.
the class IpSpaceBooleanExprTransformerTest method testVisitIpWildcardSetIpSpace.
@Test
public void testVisitIpWildcardSetIpSpace() {
IpWildcard includeWildcard = new IpWildcard("1.1.1.1");
IpWildcard excludeWildcard = new IpWildcard("2.2.2.2");
IpWildcardSetIpSpace ipSpace = IpWildcardSetIpSpace.builder().including(includeWildcard).excluding(excludeWildcard).build();
BooleanExpr expr = ipSpace.accept(SRC_IP_SPACE_BOOLEAN_EXPR_TRANSFORMER);
BooleanExpr includeExpr = includeWildcard.accept(SRC_IP_SPACE_BOOLEAN_EXPR_TRANSFORMER);
BooleanExpr excludeExpr = excludeWildcard.accept(SRC_IP_SPACE_BOOLEAN_EXPR_TRANSFORMER);
assertThat(expr, equalTo(new AndExpr(ImmutableList.of(new NotExpr(excludeExpr), includeExpr))));
}
use of org.batfish.z3.expr.AndExpr in project batfish by batfish.
the class SimplifierTest method testSimplifyAndExprFalseConjunct.
/**
* Test simplifications: false AND E --> false E AND false --> false.
*/
@Test
public void testSimplifyAndExprFalseConjunct() {
BooleanExpr p1 = newAtom();
AndExpr leftFalse = new AndExpr(of(FalseExpr.INSTANCE, p1));
AndExpr rightFalse = new AndExpr(of(p1, FalseExpr.INSTANCE));
assertThat(simplifyBooleanExpr(leftFalse), equalTo(FalseExpr.INSTANCE));
assertThat(simplifyBooleanExpr(rightFalse), equalTo(FalseExpr.INSTANCE));
}
Aggregations