Search in sources :

Example 1 with Expr

use of com.google.api.expr.v1alpha1.Expr in project batfish by batfish.

the class Encoder method environmentBlockingClause.

/*
   * Generate a blocking clause for the encoding that says that one
   * of the environments that was true before must now be false.
   */
private BoolExpr environmentBlockingClause(Model m) {
    BoolExpr acc1 = mkFalse();
    BoolExpr acc2 = mkTrue();
    // Disable an environment edge if possible
    Map<LogicalEdge, SymbolicRoute> map = getMainSlice().getLogicalGraph().getEnvironmentVars();
    for (Map.Entry<LogicalEdge, SymbolicRoute> entry : map.entrySet()) {
        SymbolicRoute record = entry.getValue();
        BoolExpr per = record.getPermitted();
        Expr x = m.evaluate(per, false);
        if (x.toString().equals("true")) {
            acc1 = mkOr(acc1, mkNot(per));
        } else {
            acc2 = mkAnd(acc2, mkNot(per));
        }
    }
    // Disable a community value if possible
    for (Map.Entry<LogicalEdge, SymbolicRoute> entry : map.entrySet()) {
        SymbolicRoute record = entry.getValue();
        for (Map.Entry<CommunityVar, BoolExpr> centry : record.getCommunities().entrySet()) {
            BoolExpr comm = centry.getValue();
            Expr x = m.evaluate(comm, false);
            if (x.toString().equals("true")) {
                acc1 = mkOr(acc1, mkNot(comm));
            } else {
                acc2 = mkAnd(acc2, mkNot(comm));
            }
        }
    }
    return mkAnd(acc1, acc2);
}
Also used : CommunityVar(org.batfish.symbolic.CommunityVar) BoolExpr(com.microsoft.z3.BoolExpr) BoolExpr(com.microsoft.z3.BoolExpr) ArithExpr(com.microsoft.z3.ArithExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) Expr(com.microsoft.z3.Expr) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 2 with Expr

use of com.google.api.expr.v1alpha1.Expr in project batfish by batfish.

the class PropertyAdder method allEqual.

static BoolExpr allEqual(Context ctx, List<Expr> exprs) {
    BoolExpr acc = ctx.mkBool(true);
    if (exprs.size() > 1) {
        for (int i = 0; i < exprs.size() - 1; i++) {
            Expr x = exprs.get(i);
            Expr y = exprs.get(i + 1);
            acc = ctx.mkAnd(acc, ctx.mkEq(x, y));
        }
    }
    return acc;
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) BoolExpr(com.microsoft.z3.BoolExpr) Expr(com.microsoft.z3.Expr) ArithExpr(com.microsoft.z3.ArithExpr)

Example 3 with Expr

use of com.google.api.expr.v1alpha1.Expr in project batfish by batfish.

the class TransferSSA method joinPoint.

/*
   * The [phi] function from SSA that merges variables that may differ across
   * different branches of an mkIf statement.
   */
private Pair<Expr, Expr> joinPoint(TransferParam<SymbolicRoute> p, TransferResult<BoolExpr, BoolExpr> r, BoolExpr guard, Pair<String, Pair<Expr, Expr>> values) {
    String variableName = values.getFirst();
    Expr trueBranch = values.getSecond().getFirst();
    Expr falseBranch = values.getSecond().getSecond();
    if (variableName.equals("RETURN") || variableName.equals("FALLTHROUGH")) {
        Expr t = (trueBranch == null ? _enc.mkFalse() : // can use False because the value has not been assigned
        trueBranch);
        Expr f = (falseBranch == null ? _enc.mkFalse() : falseBranch);
        Expr tass = (trueBranch == null ? r.getReturnAssignedValue() : _enc.mkTrue());
        Expr fass = (falseBranch == null ? r.getReturnAssignedValue() : _enc.mkTrue());
        BoolExpr newAss = _enc.mkIf(guard, (BoolExpr) tass, (BoolExpr) fass);
        BoolExpr retAss = createBoolVariableWith(p, "ASSIGNED", newAss);
        BoolExpr variable = (variableName.equals("RETURN") ? r.getReturnValue() : r.getFallthroughValue());
        BoolExpr newValue = _enc.mkIf(r.getReturnAssignedValue(), variable, _enc.mkIf(guard, (BoolExpr) t, (BoolExpr) f));
        BoolExpr ret = createBoolVariableWith(p, variableName, newValue);
        return new Pair<>(ret, retAss);
    }
    if (variableName.equals("PREFIX-LEN")) {
        Expr t = (trueBranch == null ? p.getData().getPrefixLength() : trueBranch);
        Expr f = (falseBranch == null ? p.getData().getPrefixLength() : falseBranch);
        ArithExpr newValue = _enc.mkIf(guard, (ArithExpr) t, (ArithExpr) f);
        newValue = _enc.mkIf(r.getReturnAssignedValue(), p.getData().getPrefixLength(), newValue);
        ArithExpr ret = createArithVariableWith(p, "PREFIX-LEN", newValue);
        p.getData().setPrefixLength(ret);
        return new Pair<>(ret, null);
    }
    if (variableName.equals("ADMIN-DIST")) {
        Expr t = (trueBranch == null ? p.getData().getAdminDist() : trueBranch);
        Expr f = (falseBranch == null ? p.getData().getAdminDist() : falseBranch);
        ArithExpr newValue = _enc.mkIf(guard, (ArithExpr) t, (ArithExpr) f);
        newValue = _enc.mkIf(r.getReturnAssignedValue(), p.getData().getAdminDist(), newValue);
        ArithExpr ret = createArithVariableWith(p, "ADMIN-DIST", newValue);
        p.getData().setAdminDist(ret);
        return new Pair<>(ret, null);
    }
    if (variableName.equals("LOCAL-PREF")) {
        Expr t = (trueBranch == null ? p.getData().getLocalPref() : trueBranch);
        Expr f = (falseBranch == null ? p.getData().getLocalPref() : falseBranch);
        ArithExpr newValue = _enc.mkIf(guard, (ArithExpr) t, (ArithExpr) f);
        newValue = _enc.mkIf(r.getReturnAssignedValue(), p.getData().getLocalPref(), newValue);
        ArithExpr ret = createArithVariableWith(p, "LOCAL-PREF", newValue);
        p.getData().setLocalPref(ret);
        return new Pair<>(ret, null);
    }
    if (variableName.equals("METRIC")) {
        Expr t = (trueBranch == null ? p.getData().getMetric() : trueBranch);
        Expr f = (falseBranch == null ? p.getData().getMetric() : falseBranch);
        ArithExpr newValue = _enc.mkIf(guard, (ArithExpr) t, (ArithExpr) f);
        newValue = _enc.mkIf(r.getReturnAssignedValue(), p.getData().getMetric(), newValue);
        ArithExpr ret = createArithVariableWith(p, "METRIC", newValue);
        p.getData().setMetric(ret);
        return new Pair<>(ret, null);
    }
    if (variableName.equals("OSPF-TYPE")) {
        Expr t = (trueBranch == null ? p.getData().getOspfType().getBitVec() : trueBranch);
        Expr f = (falseBranch == null ? p.getData().getOspfType().getBitVec() : falseBranch);
        BitVecExpr newValue = _enc.mkIf(guard, (BitVecExpr) t, (BitVecExpr) f);
        newValue = _enc.mkIf(r.getReturnAssignedValue(), p.getData().getOspfType().getBitVec(), newValue);
        BitVecExpr ret = createBitVecVariableWith(p, "OSPF-TYPE", 2, newValue);
        p.getData().getOspfType().setBitVec(ret);
        return new Pair<>(ret, null);
    }
    for (Map.Entry<CommunityVar, BoolExpr> entry : p.getData().getCommunities().entrySet()) {
        CommunityVar cvar = entry.getKey();
        if (variableName.equals(cvar.getValue())) {
            Expr t = (trueBranch == null ? p.getData().getCommunities().get(cvar) : trueBranch);
            Expr f = (falseBranch == null ? p.getData().getCommunities().get(cvar) : falseBranch);
            BoolExpr newValue = _enc.mkIf(guard, (BoolExpr) t, (BoolExpr) f);
            newValue = _enc.mkIf(r.getReturnAssignedValue(), p.getData().getCommunities().get(cvar), newValue);
            BoolExpr ret = createBoolVariableWith(p, cvar.getValue(), newValue);
            p.getData().getCommunities().put(cvar, ret);
            return new Pair<>(ret, null);
        }
    }
    throw new BatfishException("[joinPoint]: unhandled case for " + variableName);
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) CommunityVar(org.batfish.symbolic.CommunityVar) BoolExpr(com.microsoft.z3.BoolExpr) BatfishException(org.batfish.common.BatfishException) BitVecExpr(com.microsoft.z3.BitVecExpr) IntExpr(org.batfish.datamodel.routing_policy.expr.IntExpr) CommunitySetExpr(org.batfish.datamodel.routing_policy.expr.CommunitySetExpr) CallExpr(org.batfish.datamodel.routing_policy.expr.CallExpr) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr) AsPathListExpr(org.batfish.datamodel.routing_policy.expr.AsPathListExpr) BoolExpr(com.microsoft.z3.BoolExpr) ArithExpr(com.microsoft.z3.ArithExpr) WithEnvironmentExpr(org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr) PrefixSetExpr(org.batfish.datamodel.routing_policy.expr.PrefixSetExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) Expr(com.microsoft.z3.Expr) LongExpr(org.batfish.datamodel.routing_policy.expr.LongExpr) Map(java.util.Map) HashMap(java.util.HashMap) Pair(org.batfish.common.Pair)

Example 4 with Expr

use of com.google.api.expr.v1alpha1.Expr in project batfish by batfish.

the class Z3ContextJob method getSolverInput.

protected BoolExpr getSolverInput(Expr answer, NodProgram program, boolean negate) {
    BoolExpr solverInput;
    if (answer.getArgs().length > 0) {
        Map<String, BitVecExpr> variablesAsConsts = program.getNodContext().getVariablesAsConsts();
        List<BitVecExpr> reversedVars = Lists.reverse(program.getNodContext().getVariableNames().stream().filter(name -> !TransformationHeaderField.transformationHeaderFieldNames.contains(name)).map(variablesAsConsts::get).collect(Collectors.toList()));
        Expr substitutedAnswer = answer.substituteVars(reversedVars.toArray(new Expr[] {}));
        solverInput = (BoolExpr) substitutedAnswer;
    } else {
        solverInput = (BoolExpr) answer;
    }
    if (negate) {
        solverInput = program.getNodContext().getContext().mkNot(solverInput);
    }
    return solverInput;
}
Also used : Arrays(java.util.Arrays) FuncDecl(com.microsoft.z3.FuncDecl) IpProtocol(org.batfish.datamodel.IpProtocol) ImmutableMap(com.google.common.collect.ImmutableMap) BatfishJob(org.batfish.job.BatfishJob) Context(com.microsoft.z3.Context) BitVecExpr(com.microsoft.z3.BitVecExpr) BatfishException(org.batfish.common.BatfishException) Streams(com.google.common.collect.Streams) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) State(org.batfish.datamodel.State) Fixedpoint(com.microsoft.z3.Fixedpoint) Settings(org.batfish.config.Settings) Flow(org.batfish.datamodel.Flow) List(java.util.List) BatfishJobResult(org.batfish.job.BatfishJobResult) Lists(com.google.common.collect.Lists) Params(com.microsoft.z3.Params) Map(java.util.Map) BoolExpr(com.microsoft.z3.BoolExpr) Expr(com.microsoft.z3.Expr) Status(com.microsoft.z3.Status) Ip(org.batfish.datamodel.Ip) BoolExpr(com.microsoft.z3.BoolExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) BitVecExpr(com.microsoft.z3.BitVecExpr) BoolExpr(com.microsoft.z3.BoolExpr) Expr(com.microsoft.z3.Expr)

Example 5 with Expr

use of com.google.api.expr.v1alpha1.Expr in project bmoth by hhu-stups.

the class StateTest method testStateEquals3.

@Test
public void testStateEquals3() throws Exception {
    HashMap<String, Expr> map = new HashMap<>();
    map.put("x", z3Context.mkInt(11));
    State state = new State(map);
    assertEquals(state, state);
    assertNotEquals(state, new Object());
}
Also used : Expr(com.microsoft.z3.Expr) HashMap(java.util.HashMap) State(de.bmoth.modelchecker.State) Test(org.junit.Test)

Aggregations

Expr (com.microsoft.z3.Expr)32 Expr (com.google.api.expr.v1alpha1.Expr)23 BoolExpr (com.microsoft.z3.BoolExpr)22 Test (org.junit.Test)17 Status (com.microsoft.z3.Status)11 HashMap (java.util.HashMap)11 Test (org.junit.jupiter.api.Test)11 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)10 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)9 ArithExpr (com.microsoft.z3.ArithExpr)8 BitVecExpr (com.microsoft.z3.BitVecExpr)8 Val (org.projectnessie.cel.common.types.ref.Val)7 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)6 State (de.bmoth.modelchecker.State)5 Map (java.util.Map)5 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)5 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)5 Env.newEnv (org.projectnessie.cel.Env.newEnv)5 Call (com.google.api.expr.v1alpha1.Expr.Call)4 Entry (com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry)4