Search in sources :

Example 11 with ArithExpr

use of com.microsoft.z3.ArithExpr in project batfish by batfish.

the class CounterExample method buildFailedLinks.

SortedSet<Edge> buildFailedLinks(Encoder enc) {
    Set<GraphEdge> failed = new HashSet<>();
    Graph g = enc.getMainSlice().getGraph();
    for (List<GraphEdge> edges : g.getEdgeMap().values()) {
        for (GraphEdge ge : edges) {
            ArithExpr e = enc.getSymbolicFailures().getFailedVariable(ge);
            assert e != null;
            if (intVal(e) != 0) {
                // Don't add both directions?
                GraphEdge other = g.getOtherEnd().get(ge);
                if (other == null || !failed.contains(other)) {
                    failed.add(ge);
                }
            }
        }
    }
    // Convert to Batfish Edge type
    SortedSet<Edge> failedEdges = new TreeSet<>();
    for (GraphEdge ge : failed) {
        failedEdges.add(fromGraphEdge(ge));
    }
    return failedEdges;
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) Graph(org.batfish.symbolic.Graph) TreeSet(java.util.TreeSet) GraphEdge(org.batfish.symbolic.GraphEdge) Edge(org.batfish.datamodel.Edge) GraphEdge(org.batfish.symbolic.GraphEdge) HashSet(java.util.HashSet)

Example 12 with ArithExpr

use of com.microsoft.z3.ArithExpr in project batfish by batfish.

the class PropertyChecker method checkBoundedLength.

/*
   * Compute whether the path length will always be bounded by a constant k
   * for a collection of source routers to any of a number of destination ports.
   */
public AnswerElement checkBoundedLength(HeaderLocationQuestion q, int k) {
    return checkProperty(q, (enc, srcRouters, destPorts) -> {
        ArithExpr bound = enc.mkInt(k);
        PropertyAdder pa = new PropertyAdder(enc.getMainSlice());
        Map<String, ArithExpr> lenVars = pa.instrumentPathLength(destPorts);
        Map<String, BoolExpr> boundVars = new HashMap<>();
        lenVars.forEach((n, ae) -> boundVars.put(n, enc.mkLe(ae, bound)));
        return boundVars;
    }, (vp) -> new SmtOneAnswerElement(vp.getResult()));
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr) HashMap(java.util.HashMap) SmtOneAnswerElement(org.batfish.symbolic.answers.SmtOneAnswerElement)

Example 13 with ArithExpr

use of com.microsoft.z3.ArithExpr in project batfish by batfish.

the class TransferSSA method computeIntermediatePrefixLen.

/*
   * Create a new variable representing the new prefix length after
   * applying the effect of aggregation.
   */
private void computeIntermediatePrefixLen(TransferParam<SymbolicRoute> param) {
    ArithExpr prefixLen = param.getData().getPrefixLength();
    if (_isExport && _proto.isBgp()) {
        _aggregates = aggregateRoutes();
        if (_aggregates.size() > 0) {
            for (Map.Entry<Prefix, Boolean> entry : _aggregates.entrySet()) {
                Prefix p = entry.getKey();
                Boolean isSuppressed = entry.getValue();
                ArithExpr len = _enc.mkInt(p.getPrefixLength());
                BoolExpr relevantPfx = _enc.isRelevantFor(p, _enc.getSymbolicPacket().getDstIp());
                BoolExpr relevantLen = _enc.mkGt(param.getData().getPrefixLength(), len);
                BoolExpr relevant = _enc.mkAnd(relevantPfx, relevantLen, _enc.mkBool(isSuppressed));
                prefixLen = _enc.mkIf(relevant, len, prefixLen);
            }
            ArithExpr i = createArithVariableWith(param, "PREFIX-LEN", prefixLen);
            param.getData().setPrefixLength(i);
        }
    }
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr) Prefix(org.batfish.datamodel.Prefix) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with ArithExpr

use of com.microsoft.z3.ArithExpr in project batfish by batfish.

the class TransferSSA method applyMetricUpdate.

private void applyMetricUpdate(TransferParam<SymbolicRoute> p) {
    boolean updateOspf = (!_isExport && _proto.isOspf());
    boolean updateBgp = (_isExport && _proto.isBgp());
    boolean updateMetric = updateOspf || updateBgp;
    if (updateMetric) {
        // If it is a BGP route learned from IGP, then we use metric 0
        ArithExpr newValue;
        ArithExpr cost = _enc.mkInt(_addedCost);
        ArithExpr sum = _enc.mkSum(p.getData().getMetric(), cost);
        if (_proto.isBgp()) {
            BoolExpr isBGP;
            String router = _conf.getName();
            boolean hasProtocolVar = _other.getProtocolHistory() != null;
            boolean onlyBGP = _enc.getOptimizations().getSliceHasSingleProtocol().contains(router);
            if (hasProtocolVar) {
                isBGP = _other.getProtocolHistory().checkIfValue(Protocol.BGP);
            } else if (onlyBGP) {
                isBGP = _enc.mkTrue();
            } else {
                isBGP = _enc.mkFalse();
            }
            newValue = _enc.mkIf(isBGP, sum, cost);
        } else {
            newValue = sum;
        }
        p.getData().setMetric(newValue);
    }
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr)

Example 15 with ArithExpr

use of com.microsoft.z3.ArithExpr in project batfish by batfish.

the class TransferSSA method createArithVariableWith.

/*
   * A collection of functions to create new SSA variables on-the-fly,
   * while also simultaneously setting their value based on an old value.
   */
private ArithExpr createArithVariableWith(TransferParam<SymbolicRoute> p, String name, ArithExpr e) {
    e = (ArithExpr) e.simplify();
    if (canInline(e)) {
        p.debug(name + "=" + e);
        return e;
    }
    String s = "SSA_" + name + generateId();
    ArithExpr x = _enc.getCtx().mkIntConst(s);
    // _enc.getAllVariables().add(x);
    BoolExpr eq = _enc.mkEq(x, e);
    _enc.add(eq);
    p.debug(eq.toString());
    return x;
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr)

Aggregations

ArithExpr (com.microsoft.z3.ArithExpr)27 BoolExpr (com.microsoft.z3.BoolExpr)25 HashMap (java.util.HashMap)12 GraphEdge (org.batfish.symbolic.GraphEdge)10 BitVecExpr (com.microsoft.z3.BitVecExpr)7 Prefix (org.batfish.datamodel.Prefix)6 Graph (org.batfish.symbolic.Graph)6 List (java.util.List)5 Map (java.util.Map)5 Context (com.microsoft.z3.Context)4 Solver (com.microsoft.z3.Solver)4 HashSet (java.util.HashSet)4 Interface (org.batfish.datamodel.Interface)4 CommunityVar (org.batfish.symbolic.CommunityVar)4 Expr (com.microsoft.z3.Expr)3 BatfishException (org.batfish.common.BatfishException)3 SubRange (org.batfish.datamodel.SubRange)3 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)3 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)3 SmtOneAnswerElement (org.batfish.symbolic.answers.SmtOneAnswerElement)3