Search in sources :

Example 16 with BooleanExpr

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

the class SimplifierTest method testSimplifyOrFalse.

/**
 * Test that any FALSE children of an OR node are removed.
 */
@Test
public void testSimplifyOrFalse() {
    BooleanExpr p1 = newAtom();
    BooleanExpr p2 = newAtom();
    OrExpr and = new OrExpr(of(FalseExpr.INSTANCE, p1, FalseExpr.INSTANCE, p2));
    assertThat(simplifyBooleanExpr(and), equalTo(new OrExpr(of(p1, p2))));
}
Also used : BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) OrExpr(org.batfish.z3.expr.OrExpr) Test(org.junit.Test)

Example 17 with BooleanExpr

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

the class SimplifierTest method testSimplfyNotDoubleNegation.

/**
 * Test that NOT NOT P == P.
 */
@Test
public void testSimplfyNotDoubleNegation() {
    BooleanExpr p1 = newAtom();
    assertThat(simplifyBooleanExpr(new NotExpr(new NotExpr(p1))), equalTo(p1));
}
Also used : 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 18 with BooleanExpr

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

the class SimplifierTest method testSimplifyWrappers.

/**
 * Test that wrapper expressions are changed by simplification
 */
@Test
public void testSimplifyWrappers() {
    BooleanExpr headerSpaceMatchExpr = new HeaderSpaceMatchExpr(IpAccessListLine.builder().build());
    BooleanExpr prefixMatchExpr = new PrefixMatchExpr(BasicHeaderField.DST_IP, Prefix.ZERO);
    BooleanExpr rangeMatchExpr = RangeMatchExpr.greaterThanOrEqualTo(BasicHeaderField.DST_IP, 123456L, 10);
    assertThat(simplifyBooleanExpr(CurrentIsOriginalExpr.INSTANCE), not(equalTo(CurrentIsOriginalExpr.INSTANCE)));
    assertThat(simplifyBooleanExpr(headerSpaceMatchExpr), not(equalTo(headerSpaceMatchExpr)));
    assertThat(simplifyBooleanExpr(prefixMatchExpr), not(equalTo(prefixMatchExpr)));
    assertThat(simplifyBooleanExpr(rangeMatchExpr), not(equalTo(rangeMatchExpr)));
    assertThat(simplifyBooleanExpr(SaneExpr.INSTANCE), not(equalTo(SaneExpr.INSTANCE)));
}
Also used : PrefixMatchExpr(org.batfish.z3.expr.PrefixMatchExpr) HeaderSpaceMatchExpr(org.batfish.z3.expr.HeaderSpaceMatchExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 19 with BooleanExpr

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

the class DefaultTransitionGenerator method visitPreOutEdgePostNat_generateNoMatchSourceNatRules.

private void visitPreOutEdgePostNat_generateNoMatchSourceNatRules(String node1, String iface1, String node2, String iface2) {
    List<Entry<AclPermit, BooleanExpr>> sourceNats = _input.getSourceNats().getOrDefault(node1, ImmutableMap.of()).getOrDefault(iface1, ImmutableList.of());
    ImmutableSet.Builder<StateExpr> preStates = ImmutableSet.builder();
    preStates.add(new PreOutEdge(node1, iface1, node2, iface2));
    sourceNats.stream().map(Entry::getKey).map(aclPermit -> new AclDeny(aclPermit.getHostname(), aclPermit.getAcl())).forEach(preStates::add);
    _rules.add(new TransformationRuleStatement(new EqExpr(new VarIntExpr(TransformationHeaderField.NEW_SRC_IP), new VarIntExpr(TransformationHeaderField.NEW_SRC_IP.getCurrent())), preStates.build(), ImmutableSet.of(), new PreOutEdgePostNat(node1, iface1, node2, iface2)));
}
Also used : DropAclIn(org.batfish.z3.state.DropAclIn) NodeDropAclOut(org.batfish.z3.state.NodeDropAclOut) AclLineMatch(org.batfish.z3.state.AclLineMatch) Drop(org.batfish.z3.state.Drop) NodeDropAcl(org.batfish.z3.state.NodeDropAcl) NodeDropNoRoute(org.batfish.z3.state.NodeDropNoRoute) NumberedQuery(org.batfish.z3.state.NumberedQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PostIn(org.batfish.z3.state.PostIn) AclLineNoMatch(org.batfish.z3.state.AclLineNoMatch) HeaderSpaceMatchExpr(org.batfish.z3.expr.HeaderSpaceMatchExpr) State(org.batfish.z3.expr.StateExpr.State) Originate(org.batfish.z3.state.Originate) NodeDropNullRoute(org.batfish.z3.state.NodeDropNullRoute) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Query(org.batfish.z3.state.Query) PostOutEdge(org.batfish.z3.state.PostOutEdge) List(java.util.List) PreOutEdgePostNat(org.batfish.z3.state.PreOutEdgePostNat) OriginateVrf(org.batfish.z3.state.OriginateVrf) PreOutEdge(org.batfish.z3.state.PreOutEdge) Entry(java.util.Map.Entry) NodeDrop(org.batfish.z3.state.NodeDrop) NodeNeighborUnreachable(org.batfish.z3.state.NodeNeighborUnreachable) IpWildcard(org.batfish.datamodel.IpWildcard) TransformationHeaderField(org.batfish.z3.TransformationHeaderField) TrueExpr(org.batfish.z3.expr.TrueExpr) DropAcl(org.batfish.z3.state.DropAcl) DropNullRoute(org.batfish.z3.state.DropNullRoute) SynthesizerInput(org.batfish.z3.SynthesizerInput) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) NodeAccept(org.batfish.z3.state.NodeAccept) ImmutableList(com.google.common.collect.ImmutableList) StateExpr(org.batfish.z3.expr.StateExpr) LineAction(org.batfish.datamodel.LineAction) RuleStatement(org.batfish.z3.expr.RuleStatement) DropAclOut(org.batfish.z3.state.DropAclOut) Debug(org.batfish.z3.state.Debug) DropNoRoute(org.batfish.z3.state.DropNoRoute) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Accept(org.batfish.z3.state.Accept) NodeDropAclIn(org.batfish.z3.state.NodeDropAclIn) PreOut(org.batfish.z3.state.PreOut) AclDeny(org.batfish.z3.state.AclDeny) NotExpr(org.batfish.z3.expr.NotExpr) AclPermit(org.batfish.z3.state.AclPermit) VarIntExpr(org.batfish.z3.expr.VarIntExpr) PreInInterface(org.batfish.z3.state.PreInInterface) PostInVrf(org.batfish.z3.state.PostInVrf) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) NeighborUnreachable(org.batfish.z3.state.NeighborUnreachable) EqExpr(org.batfish.z3.expr.EqExpr) PostInInterface(org.batfish.z3.state.PostInInterface) AclDeny(org.batfish.z3.state.AclDeny) VarIntExpr(org.batfish.z3.expr.VarIntExpr) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) StateExpr(org.batfish.z3.expr.StateExpr) Entry(java.util.Map.Entry) PreOutEdge(org.batfish.z3.state.PreOutEdge) ImmutableSet(com.google.common.collect.ImmutableSet) EqExpr(org.batfish.z3.expr.EqExpr) PreOutEdgePostNat(org.batfish.z3.state.PreOutEdgePostNat)

Example 20 with BooleanExpr

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

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