Search in sources :

Example 51 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 52 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 53 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 54 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)

Example 55 with Expr

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

the class Porthos method main.

public static void main(String[] args) throws Z3Exception, IOException {
    List<String> MCMs = Arrays.asList("sc", "tso", "pso", "rmo", "alpha", "power", "arm");
    Options options = new Options();
    Option sourceOpt = new Option("s", "source", true, "source MCM");
    sourceOpt.setRequired(true);
    options.addOption(sourceOpt);
    Option targetOpt = new Option("t", "target", true, "target MCM");
    targetOpt.setRequired(true);
    options.addOption(targetOpt);
    Option inputOpt = new Option("i", "input", true, "input file path");
    inputOpt.setRequired(true);
    options.addOption(inputOpt);
    options.addOption("state", false, "PORTHOS performs state portability");
    options.addOption(Option.builder("draw").hasArg().desc("If a buf is found, it outputs a graph \\path_to_file.dot").build());
    options.addOption(Option.builder("rels").hasArgs().desc("Relations to be drawn in the graph").build());
    options.addOption(Option.builder("unroll").hasArg().desc("Unrolling steps").build());
    CommandLineParser parserCmd = new DefaultParser();
    HelpFormatter formatter = new HelpFormatter();
    CommandLine cmd;
    try {
        cmd = parserCmd.parse(options, args);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp("PORTHOS", options);
        System.exit(1);
        return;
    }
    String source = cmd.getOptionValue("source");
    if (!MCMs.stream().anyMatch(mcms -> mcms.trim().equals(source))) {
        System.out.println("Unrecognized source");
        System.exit(0);
        return;
    }
    String target = cmd.getOptionValue("target");
    if (!MCMs.stream().anyMatch(mcms -> mcms.trim().equals(target))) {
        System.out.println("Unrecognized target");
        System.exit(0);
        return;
    }
    String inputFilePath = cmd.getOptionValue("input");
    if (!inputFilePath.endsWith("pts") && !inputFilePath.endsWith("litmus")) {
        System.out.println("Unrecognized program format");
        System.exit(0);
        return;
    }
    File file = new File(inputFilePath);
    boolean statePortability = cmd.hasOption("state");
    String[] rels = new String[100];
    if (cmd.hasOption("rels")) {
        rels = cmd.getOptionValues("rels");
    }
    String program = FileUtils.readFileToString(file, "UTF-8");
    ANTLRInputStream input = new ANTLRInputStream(program);
    Program p = new Program(inputFilePath);
    if (inputFilePath.endsWith("litmus")) {
        LitmusLexer lexer = new LitmusLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        LitmusParser parser = new LitmusParser(tokens);
        p = parser.program(inputFilePath).p;
    }
    if (inputFilePath.endsWith("pts")) {
        PorthosLexer lexer = new PorthosLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PorthosParser parser = new PorthosParser(tokens);
        p = parser.program(inputFilePath).p;
    }
    int steps = 1;
    if (cmd.hasOption("unroll")) {
        steps = Integer.parseInt(cmd.getOptionValue("unroll"));
    }
    p.initialize(steps);
    Program pSource = p.clone();
    Program pTarget = p.clone();
    pSource.compile(source, false, true);
    Integer startEId = Collections.max(pSource.getEvents().stream().filter(e -> e instanceof Init).map(e -> e.getEId()).collect(Collectors.toSet())) + 1;
    pTarget.compile(target, false, true, startEId);
    Context ctx = new Context();
    ctx.setPrintMode(Z3_ast_print_mode.Z3_PRINT_SMTLIB_FULL);
    Solver s = ctx.mkSolver();
    Solver s2 = ctx.mkSolver();
    BoolExpr sourceDF = pSource.encodeDF(ctx);
    BoolExpr sourceCF = pSource.encodeCF(ctx);
    BoolExpr sourceDF_RF = pSource.encodeDF_RF(ctx);
    BoolExpr sourceDomain = Domain.encode(pSource, ctx);
    BoolExpr sourceMM = pSource.encodeMM(ctx, source);
    s.add(pTarget.encodeDF(ctx));
    s.add(pTarget.encodeCF(ctx));
    s.add(pTarget.encodeDF_RF(ctx));
    s.add(Domain.encode(pTarget, ctx));
    s.add(pTarget.encodeMM(ctx, target));
    s.add(pTarget.encodeConsistent(ctx, target));
    s.add(sourceDF);
    s.add(sourceCF);
    s.add(sourceDF_RF);
    s.add(sourceDomain);
    s.add(sourceMM);
    s.add(pSource.encodeInconsistent(ctx, source));
    s.add(encodeCommonExecutions(pTarget, pSource, ctx));
    s2.add(sourceDF);
    s2.add(sourceCF);
    s2.add(sourceDF_RF);
    s2.add(sourceDomain);
    s2.add(sourceMM);
    s2.add(pSource.encodeConsistent(ctx, source));
    if (!statePortability) {
        if (s.check() == Status.SATISFIABLE) {
            System.out.println("The program is not portable");
            // System.out.println("       0");
            if (cmd.hasOption("draw")) {
                String outputPath = cmd.getOptionValue("draw");
                Utils.drawGraph(p, pSource, pTarget, ctx, s.getModel(), outputPath, rels);
            }
            return;
        } else {
            System.out.println("The program is portable");
            // System.out.println("       1");
            return;
        }
    }
    int iterations = 0;
    Status lastCheck = Status.SATISFIABLE;
    Set<Expr> visited = new HashSet<Expr>();
    while (lastCheck == Status.SATISFIABLE) {
        lastCheck = s.check();
        if (lastCheck == Status.SATISFIABLE) {
            iterations = iterations + 1;
            Model model = s.getModel();
            s2.push();
            BoolExpr reachedState = encodeReachedState(pTarget, model, ctx);
            visited.add(reachedState);
            assert (iterations == visited.size());
            s2.add(reachedState);
            if (s2.check() == Status.UNSATISFIABLE) {
                System.out.println("The program is not state-portable");
                System.out.println("Iterations: " + iterations);
                // System.out.println("       0");
                return;
            } else {
                s2.pop();
                s.add(ctx.mkNot(reachedState));
            }
        } else {
            System.out.println("The program is state-portable");
            System.out.println("Iterations: " + iterations);
            // System.out.println("       1");
            return;
        }
    }
}
Also used : Arrays(java.util.Arrays) Solver(com.microsoft.z3.Solver) org.apache.commons.cli(org.apache.commons.cli) Context(com.microsoft.z3.Context) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) HashSet(java.util.HashSet) LitmusParser(dartagnan.LitmusParser) PorthosLexer(dartagnan.PorthosLexer) PorthosParser(dartagnan.PorthosParser) BoolExpr(com.microsoft.z3.BoolExpr) Status(com.microsoft.z3.Status) Program(dartagnan.program.Program) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Utils(dartagnan.utils.Utils) Collectors(java.util.stream.Collectors) File(java.io.File) Z3_ast_print_mode(com.microsoft.z3.enumerations.Z3_ast_print_mode) Init(dartagnan.program.Init) List(java.util.List) Model(com.microsoft.z3.Model) Domain(dartagnan.wmm.Domain) Encodings.encodeReachedState(dartagnan.utils.Encodings.encodeReachedState) Expr(com.microsoft.z3.Expr) Z3Exception(com.microsoft.z3.Z3Exception) Collections(java.util.Collections) LitmusLexer(dartagnan.LitmusLexer) Encodings.encodeCommonExecutions(dartagnan.utils.Encodings.encodeCommonExecutions) BoolExpr(com.microsoft.z3.BoolExpr) Solver(com.microsoft.z3.Solver) LitmusLexer(dartagnan.LitmusLexer) PorthosParser(dartagnan.PorthosParser) PorthosLexer(dartagnan.PorthosLexer) Init(dartagnan.program.Init) HashSet(java.util.HashSet) Context(com.microsoft.z3.Context) Status(com.microsoft.z3.Status) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Program(dartagnan.program.Program) LitmusParser(dartagnan.LitmusParser) BoolExpr(com.microsoft.z3.BoolExpr) Expr(com.microsoft.z3.Expr) Model(com.microsoft.z3.Model) File(java.io.File) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

Expr (edu.stanford.CVC4.Expr)57 Test (org.junit.Test)55 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)49 SExpr (edu.stanford.CVC4.SExpr)42 Expr (com.microsoft.z3.Expr)32 Result (edu.stanford.CVC4.Result)32 Rational (edu.stanford.CVC4.Rational)28 Expr (com.google.api.expr.v1alpha1.Expr)23 BoolExpr (com.microsoft.z3.BoolExpr)22 ArrayType (edu.stanford.CVC4.ArrayType)12 BitVectorType (edu.stanford.CVC4.BitVectorType)12 Status (com.microsoft.z3.Status)11 Type (edu.stanford.CVC4.Type)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