Search in sources :

Example 6 with AndExpr

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));
}
Also used : AndExpr(org.batfish.z3.expr.AndExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 7 with AndExpr

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))));
}
Also used : AndExpr(org.batfish.z3.expr.AndExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 8 with AndExpr

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);
    }
}
Also used : AndExpr(org.batfish.z3.expr.AndExpr) Statement(org.batfish.z3.expr.Statement) PrefixMatchExpr(org.batfish.z3.expr.PrefixMatchExpr) OrExpr(org.batfish.z3.expr.OrExpr) TrueExpr(org.batfish.z3.expr.TrueExpr) IntExpr(org.batfish.z3.expr.IntExpr) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) GenericStatementVisitor(org.batfish.z3.expr.GenericStatementVisitor) ImmutableList(com.google.common.collect.ImmutableList) ExtractExpr(org.batfish.z3.expr.ExtractExpr) Predicates(com.google.common.base.Predicates) StateExpr(org.batfish.z3.expr.StateExpr) HeaderSpaceMatchExpr(org.batfish.z3.expr.HeaderSpaceMatchExpr) IfExpr(org.batfish.z3.expr.IfExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Expr(org.batfish.z3.expr.Expr) BitVecExpr(org.batfish.z3.expr.BitVecExpr) FalseExpr(org.batfish.z3.expr.FalseExpr) IpSpaceMatchExpr(org.batfish.z3.expr.IpSpaceMatchExpr) IfThenElse(org.batfish.z3.expr.IfThenElse) NotExpr(org.batfish.z3.expr.NotExpr) VarIntExpr(org.batfish.z3.expr.VarIntExpr) Comment(org.batfish.z3.expr.Comment) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) SaneExpr(org.batfish.z3.expr.SaneExpr) AndExpr(org.batfish.z3.expr.AndExpr) List(java.util.List) Stream(java.util.stream.Stream) LitIntExpr(org.batfish.z3.expr.LitIntExpr) CurrentIsOriginalExpr(org.batfish.z3.expr.CurrentIsOriginalExpr) EqExpr(org.batfish.z3.expr.EqExpr) QueryStatement(org.batfish.z3.expr.QueryStatement) RangeMatchExpr(org.batfish.z3.expr.RangeMatchExpr) IdExpr(org.batfish.z3.expr.IdExpr) ListExpr(org.batfish.z3.expr.ListExpr) ImmutableList(com.google.common.collect.ImmutableList) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Example 9 with AndExpr

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))));
}
Also used : IpWildcard(org.batfish.datamodel.IpWildcard) AndExpr(org.batfish.z3.expr.AndExpr) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) NotExpr(org.batfish.z3.expr.NotExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Test(org.junit.Test)

Example 10 with AndExpr

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));
}
Also used : AndExpr(org.batfish.z3.expr.AndExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Aggregations

AndExpr (org.batfish.z3.expr.AndExpr)15 BooleanExpr (org.batfish.z3.expr.BooleanExpr)13 Test (org.junit.Test)8 NotExpr (org.batfish.z3.expr.NotExpr)6 ImmutableList (com.google.common.collect.ImmutableList)5 BasicRuleStatement (org.batfish.z3.expr.BasicRuleStatement)5 QueryStatement (org.batfish.z3.expr.QueryStatement)5 Simplifier.simplifyBooleanExpr (org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr)5 EqExpr (org.batfish.z3.expr.EqExpr)3 HeaderSpaceMatchExpr (org.batfish.z3.expr.HeaderSpaceMatchExpr)3 LitIntExpr (org.batfish.z3.expr.LitIntExpr)3 RuleStatement (org.batfish.z3.expr.RuleStatement)3 SaneExpr (org.batfish.z3.expr.SaneExpr)3 StateExpr (org.batfish.z3.expr.StateExpr)3 VarIntExpr (org.batfish.z3.expr.VarIntExpr)3 List (java.util.List)2 BitVecExpr (org.batfish.z3.expr.BitVecExpr)2 CurrentIsOriginalExpr (org.batfish.z3.expr.CurrentIsOriginalExpr)2 Expr (org.batfish.z3.expr.Expr)2 ExtractExpr (org.batfish.z3.expr.ExtractExpr)2