Search in sources :

Example 36 with BooleanExpr

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

the class IpSpaceBooleanExprTransformer method visitAclIpSpace.

@Override
public BooleanExpr visitAclIpSpace(AclIpSpace aclIpSpace) {
    // right fold
    BooleanExpr expr = FalseExpr.INSTANCE;
    for (int i = aclIpSpace.getLines().size() - 1; i >= 0; i--) {
        AclIpSpaceLine line = aclIpSpace.getLines().get(i);
        expr = new IfThenElse(line.getIpSpace().accept(this), line.getAction() == LineAction.ACCEPT ? TrueExpr.INSTANCE : FalseExpr.INSTANCE, expr);
    }
    return expr;
}
Also used : AclIpSpaceLine(org.batfish.datamodel.AclIpSpaceLine) IfThenElse(org.batfish.z3.expr.IfThenElse) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Example 37 with BooleanExpr

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

the class IpSpaceBooleanExprTransformer method visitIpWildcardSetIpSpace.

@Override
public BooleanExpr visitIpWildcardSetIpSpace(IpWildcardSetIpSpace ipWildcardSetIpSpace) {
    BooleanExpr matchBlacklist = HeaderSpaceMatchExpr.matchIp(ipWildcardSetIpSpace.getBlacklist(), _useSrc, _useDst);
    BooleanExpr matchWhitelist = HeaderSpaceMatchExpr.matchIp(ipWildcardSetIpSpace.getWhitelist(), _useSrc, _useDst);
    return new AndExpr(ImmutableList.of(new NotExpr(matchBlacklist), matchWhitelist));
}
Also used : AndExpr(org.batfish.z3.expr.AndExpr) NotExpr(org.batfish.z3.expr.NotExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Example 38 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr 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);
    }
}
Also used : 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) OrExpr(org.batfish.z3.expr.OrExpr)

Example 39 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr 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)

Example 40 with BooleanExpr

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

the class Simplifier method visitBasicRuleStatement.

@Override
public Statement visitBasicRuleStatement(BasicRuleStatement basicRuleStatement) {
    /**
     * TODO: something smarter
     */
    BooleanExpr originalPreconditionStateIndependentConstraints = basicRuleStatement.getPreconditionStateIndependentConstraints();
    BooleanExpr simplifiedPreconditionStateIndependentConstraints = simplifyBooleanExpr(originalPreconditionStateIndependentConstraints);
    if (originalPreconditionStateIndependentConstraints != simplifiedPreconditionStateIndependentConstraints) {
        return simplifyStatement(new BasicRuleStatement(simplifiedPreconditionStateIndependentConstraints, basicRuleStatement.getPreconditionStates(), basicRuleStatement.getPostconditionState()));
    } else if (simplifiedPreconditionStateIndependentConstraints == FalseExpr.INSTANCE) {
        return VACUOUS_RULE;
    } else {
        return basicRuleStatement;
    }
}
Also used : BooleanExpr(org.batfish.z3.expr.BooleanExpr) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement)

Aggregations

BooleanExpr (org.batfish.z3.expr.BooleanExpr)43 Test (org.junit.Test)27 AndExpr (org.batfish.z3.expr.AndExpr)14 NotExpr (org.batfish.z3.expr.NotExpr)13 Simplifier.simplifyBooleanExpr (org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr)13 BasicRuleStatement (org.batfish.z3.expr.BasicRuleStatement)10 ImmutableList (com.google.common.collect.ImmutableList)9 HeaderSpaceMatchExpr (org.batfish.z3.expr.HeaderSpaceMatchExpr)8 BoolExpr (com.microsoft.z3.BoolExpr)7 IfExpr (org.batfish.z3.expr.IfExpr)7 VarIntExpr (org.batfish.z3.expr.VarIntExpr)7 BoolExprTransformer.toBoolExpr (org.batfish.z3.expr.visitors.BoolExprTransformer.toBoolExpr)7 List (java.util.List)6 IpWildcard (org.batfish.datamodel.IpWildcard)6 EqExpr (org.batfish.z3.expr.EqExpr)6 OrExpr (org.batfish.z3.expr.OrExpr)6 QueryStatement (org.batfish.z3.expr.QueryStatement)6 RuleStatement (org.batfish.z3.expr.RuleStatement)6 TransformationRuleStatement (org.batfish.z3.expr.TransformationRuleStatement)6 Ip (org.batfish.datamodel.Ip)5