use of org.batfish.z3.expr.OrExpr in project batfish by batfish.
the class SimplifierTest method testSimplifyOr1.
/**
* Test that an OR node with a single child (other than TRUE or FALSE) simplifies to that child.
*/
@Test
public void testSimplifyOr1() {
BooleanExpr p1 = newAtom();
OrExpr or = new OrExpr(of(p1));
assertThat(simplifyBooleanExpr(or), equalTo(p1));
}
use of org.batfish.z3.expr.OrExpr in project batfish by batfish.
the class SimplifierTest method testSimplifyOrTrue.
/**
* Test that an OR node with a TRUE child simplifies to TRUE.
*/
@Test
public void testSimplifyOrTrue() {
BooleanExpr p1 = newAtom();
OrExpr or = new OrExpr(of(p1, TrueExpr.INSTANCE));
assertThat(simplifyBooleanExpr(or), equalTo(TrueExpr.INSTANCE));
}
use of org.batfish.z3.expr.OrExpr in project batfish by batfish.
the class Simplifier method visitOrExpr.
@Override
public BooleanExpr visitOrExpr(OrExpr orExpr) {
boolean changed = false;
List<BooleanExpr> oldDisjuncts = orExpr.getDisjuncts();
ImmutableList.Builder<BooleanExpr> newDisjunctsBuilder = ImmutableList.builder();
// first check for nested ORs
if (oldDisjuncts.stream().anyMatch(Predicates.instanceOf(OrExpr.class))) {
return simplifyBooleanExpr(new OrExpr(oldDisjuncts.stream().flatMap(disjunct -> disjunct instanceof OrExpr ? ((OrExpr) disjunct).getDisjuncts().stream() : Stream.of(disjunct)).collect(ImmutableList.toImmutableList())));
}
// no nested ORs, so just simplify all disjuncts
for (BooleanExpr disjunct : oldDisjuncts) {
BooleanExpr simplifiedDisjunct = simplifyBooleanExpr(disjunct);
if (disjunct != simplifiedDisjunct) {
changed = true;
}
if (simplifiedDisjunct == TrueExpr.INSTANCE) {
return TrueExpr.INSTANCE;
} else if (simplifiedDisjunct != FalseExpr.INSTANCE) {
newDisjunctsBuilder.add(simplifiedDisjunct);
} else {
changed = true;
}
}
List<BooleanExpr> newDisjuncts = newDisjunctsBuilder.build();
if (newDisjuncts.size() == 0) {
return FalseExpr.INSTANCE;
} else if (newDisjuncts.size() == 1) {
return newDisjuncts.get(0);
} else if (!changed) {
return orExpr;
} else {
return new OrExpr(newDisjuncts);
}
}
Aggregations