Search in sources :

Example 1 with IfExpr

use of org.batfish.z3.expr.IfExpr in project batfish by batfish.

the class SimplifierTest method testSimplifyIfAntecedentStaticallyConsequent.

/**
 * Test that an IF with antecedent statically determinable to be TRUE simplifies to the
 * consequent.
 */
@Test
public void testSimplifyIfAntecedentStaticallyConsequent() {
    BooleanExpr p1 = newAtom();
    assertThat(simplifyBooleanExpr(new IfExpr(TrueExpr.INSTANCE, p1)), equalTo(p1));
    assertThat(simplifyBooleanExpr(new IfExpr(new NotExpr(new NotExpr(TrueExpr.INSTANCE)), p1)), equalTo(p1));
}
Also used : IfExpr(org.batfish.z3.expr.IfExpr) NotExpr(org.batfish.z3.expr.NotExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 2 with IfExpr

use of org.batfish.z3.expr.IfExpr in project batfish by batfish.

the class BoolExprTransformerTest method testVisitIfExpr.

@Test
public void testVisitIfExpr() {
    BooleanExpr p1Batfish = newBooleanAtom();
    BooleanExpr p2Batfish = newBooleanAtom();
    BoolExpr p1Z3 = toBoolExpr(p1Batfish, _input, _nodContext);
    BoolExpr p2Z3 = toBoolExpr(p2Batfish, _input, _nodContext);
    assertThat(toBoolExpr(new IfExpr(p1Batfish, p2Batfish), _input, _nodContext), equalTo(_ctx.mkImplies(p1Z3, p2Z3)));
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) BoolExprTransformer.toBoolExpr(org.batfish.z3.expr.visitors.BoolExprTransformer.toBoolExpr) IfExpr(org.batfish.z3.expr.IfExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Test(org.junit.Test)

Example 3 with IfExpr

use of org.batfish.z3.expr.IfExpr in project batfish by batfish.

the class SimplifierTest method testSimplifyIfStaticallyTrue.

/**
 * Test that an IF with antecedent statically FALSE, or consequent statically TRUE, or antecedent
 * statically equal to consequent simplifies to TRUE.
 */
@Test
public void testSimplifyIfStaticallyTrue() {
    BooleanExpr p1 = newAtom();
    // Antecedent is false
    assertThat(simplifyBooleanExpr(new IfExpr(FalseExpr.INSTANCE, newAtom())), equalTo(TrueExpr.INSTANCE));
    assertThat(simplifyBooleanExpr(new IfExpr(new NotExpr(new NotExpr(FalseExpr.INSTANCE)), newAtom())), equalTo(TrueExpr.INSTANCE));
    // Consequent is true
    assertThat(simplifyBooleanExpr(new IfExpr(newAtom(), TrueExpr.INSTANCE)), equalTo(TrueExpr.INSTANCE));
    assertThat(simplifyBooleanExpr(new IfExpr(newAtom(), new NotExpr(FalseExpr.INSTANCE))), equalTo(TrueExpr.INSTANCE));
    // Antecedent == Consequent
    assertThat(simplifyBooleanExpr(new IfExpr(p1, p1)), equalTo(TrueExpr.INSTANCE));
    assertThat(simplifyBooleanExpr(new IfExpr(new NotExpr(new NotExpr(p1)), p1)), equalTo(TrueExpr.INSTANCE));
}
Also used : IfExpr(org.batfish.z3.expr.IfExpr) NotExpr(org.batfish.z3.expr.NotExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 4 with IfExpr

use of org.batfish.z3.expr.IfExpr in project batfish by batfish.

the class SimplifierTest method testSimplifyIfUnchanged.

/**
 * Test that an IF with unsimplifiable antecedent and consequent is unchanged by simplification.
 */
@Test
public void testSimplifyIfUnchanged() {
    BooleanExpr p1 = newAtom();
    BooleanExpr p2 = newAtom();
    BooleanExpr ifExpr = new IfExpr(p1, p2);
    assertThat(simplifyBooleanExpr(ifExpr), sameInstance(ifExpr));
}
Also used : IfExpr(org.batfish.z3.expr.IfExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 5 with IfExpr

use of org.batfish.z3.expr.IfExpr in project batfish by batfish.

the class Simplifier method visitIfExpr.

@Override
public BooleanExpr visitIfExpr(IfExpr ifExpr) {
    BooleanExpr oldAntecedent = ifExpr.getAntecedent();
    BooleanExpr oldConsequent = ifExpr.getConsequent();
    BooleanExpr newAntecedent = simplifyBooleanExpr(oldAntecedent);
    BooleanExpr newConsequent = simplifyBooleanExpr(oldConsequent);
    if (newAntecedent == FalseExpr.INSTANCE || newConsequent == TrueExpr.INSTANCE || newAntecedent.equals(newConsequent)) {
        return TrueExpr.INSTANCE;
    } else if (newAntecedent == TrueExpr.INSTANCE) {
        return newConsequent;
    } else if (newAntecedent != oldAntecedent || newConsequent != oldConsequent) {
        return new IfExpr(newAntecedent, newConsequent);
    } else {
        return ifExpr;
    }
}
Also used : IfExpr(org.batfish.z3.expr.IfExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Aggregations

BooleanExpr (org.batfish.z3.expr.BooleanExpr)5 IfExpr (org.batfish.z3.expr.IfExpr)5 Test (org.junit.Test)4 Simplifier.simplifyBooleanExpr (org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr)3 NotExpr (org.batfish.z3.expr.NotExpr)2 BoolExpr (com.microsoft.z3.BoolExpr)1 BoolExprTransformer.toBoolExpr (org.batfish.z3.expr.visitors.BoolExprTransformer.toBoolExpr)1