Search in sources :

Example 16 with Solver

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

the class PropertyAdder method instrumentReachability.

/*
   * Also instruments reachability, but to a destination router
   * rather than a destination port.
   */
Map<String, BoolExpr> instrumentReachability(String router) {
    Context ctx = _encoderSlice.getCtx();
    Solver solver = _encoderSlice.getSolver();
    Map<String, BoolExpr> reachableVars = new HashMap<>();
    Map<String, ArithExpr> idVars = new HashMap<>();
    initializeReachabilityVars(_encoderSlice, ctx, solver, reachableVars, idVars);
    ArithExpr baseId = idVars.get(router);
    _encoderSlice.add(ctx.mkEq(baseId, ctx.mkInt(1)));
    Graph g = _encoderSlice.getGraph();
    for (Entry<String, List<GraphEdge>> entry : g.getEdgeMap().entrySet()) {
        String r = entry.getKey();
        List<GraphEdge> edges = entry.getValue();
        if (!r.equals(router)) {
            ArithExpr id = idVars.get(r);
            BoolExpr cond = recursiveReachability(ctx, _encoderSlice, edges, idVars, r, id);
            solver.add(cond);
        }
    }
    return reachableVars;
}
Also used : Context(com.microsoft.z3.Context) ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr) Solver(com.microsoft.z3.Solver) HashMap(java.util.HashMap) Graph(org.batfish.symbolic.Graph) List(java.util.List) GraphEdge(org.batfish.symbolic.GraphEdge)

Example 17 with Solver

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

the class AbstractNodJob method getOriginateVrfConstraints.

/**
 * Try to find a model for each OriginateVrf. If an OriginateVrf does not have an entry in the
 * Map, then the query is unsat when originating from there.
 */
protected Map<OriginateVrf, Map<String, Long>> getOriginateVrfConstraints(Context ctx, SmtInput smtInput) {
    Solver solver = ctx.mkSolver();
    solver.add(smtInput._expr);
    int originateVrfBvSize = _originateVrfInstrumentation.getFieldBits();
    BitVecExpr originateVrfFieldConst = ctx.mkBVConst(OriginateVrfInstrumentation.ORIGINATE_VRF_FIELD_NAME, originateVrfBvSize);
    ImmutableMap.Builder<OriginateVrf, Map<String, Long>> models = ImmutableMap.builder();
    // keep refining until no new models
    while (true) {
        try {
            Map<String, Long> constraints = getSolution(solver, smtInput._variablesAsConsts);
            int originateVrfId = Math.toIntExact(constraints.get(OriginateVrfInstrumentation.ORIGINATE_VRF_FIELD_NAME));
            OriginateVrf originateVrf = _originateVrfInstrumentation.getOriginateVrfs().get(originateVrfId);
            models.put(originateVrf, constraints);
            // refine: different OriginateVrf
            solver.add(ctx.mkNot(ctx.mkEq(originateVrfFieldConst, ctx.mkBV(originateVrfId, originateVrfBvSize))));
        } catch (QueryUnsatException e) {
            break;
        }
    }
    return models.build();
}
Also used : Solver(com.microsoft.z3.Solver) BitVecExpr(com.microsoft.z3.BitVecExpr) OriginateVrf(org.batfish.z3.state.OriginateVrf) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 18 with Solver

use of com.microsoft.z3.Solver in project bmoth by hhu-stups.

the class ReplViewTest method formatCouplesInSetTest.

@Test
public void formatCouplesInSetTest() {
    Context ctx = new Context();
    Solver s = ctx.mkSolver();
    BoolExpr constraint = translatePredicate("x = {(1,2,3),(4,5,6)}", ctx);
    s.add(constraint);
    s.check();
    Model model = s.getModel();
    String output = new PrettyPrinter(model).getOutput();
    assertEquals("{x={((1,2),3),((4,5),6)}}", output);
}
Also used : Context(com.microsoft.z3.Context) BoolExpr(com.microsoft.z3.BoolExpr) Solver(com.microsoft.z3.Solver) Model(com.microsoft.z3.Model) Test(org.junit.Test)

Example 19 with Solver

use of com.microsoft.z3.Solver in project bmoth by hhu-stups.

the class ExplicitStateModelChecker method doModelCheck.

@Override
protected ModelCheckingResult doModelCheck() {
    final int maxInitialStates = BMothPreferences.getIntPreference(BMothPreferences.IntPreference.MAX_INITIAL_STATE);
    final int maxTransitions = BMothPreferences.getIntPreference(BMothPreferences.IntPreference.MAX_TRANSITIONS);
    stateSpace = new StateSpace();
    visited = new HashSet<>();
    Queue<State> queue = new LinkedList<>();
    // prepare initial states
    BoolExpr initialValueConstraint = getMachineTranslator().getInitialValueConstraint();
    Set<Model> models = finder.findSolutions(initialValueConstraint, maxInitialStates);
    models.stream().map(this::getStateFromModel).filter(this::isUnknown).forEach(root -> {
        stateSpace.addRootVertex(root);
        queue.add(root);
    });
    final BoolExpr invariant = getMachineTranslator().getInvariantConstraint();
    solver.add(invariant);
    // create joint operations constraint and permanently add to separate
    // solver
    final BoolExpr operationsConstraint = getMachineTranslator().getCombinedOperationConstraint();
    opSolver.add(operationsConstraint);
    while (!isAborted() && !queue.isEmpty()) {
        solver.push();
        State current = queue.poll();
        visited.add(current);
        // apply current state - remains stored in solver for loop iteration
        BoolExpr stateConstraint = current.getStateConstraint(getContext());
        solver.add(stateConstraint);
        // check invariant & state
        Status check = solver.check();
        switch(check) {
            case UNKNOWN:
                return createUnknown(visited.size(), solver.getReasonUnknown());
            case UNSATISFIABLE:
                return createCounterExampleFound(visited.size(), current, stateSpace);
            case SATISFIABLE:
            default:
        }
        // compute successors on separate finder
        models = opFinder.findSolutions(stateConstraint, maxTransitions);
        models.stream().map(this::getStateFromModel).forEach(successor -> {
            if (isUnknown(successor)) {
                stateSpace.addVertex(successor);
                queue.add(successor);
            }
            stateSpace.addEdge(current, successor);
        });
        solver.pop();
    }
    if (isAborted()) {
        return createAborted(visited.size());
    } else {
        ModelCheckingResult resultVerified = createVerified(visited.size(), stateSpace);
        if (buechiAutomaton != null) {
            // do ltl model check
            labelStateSpace();
            List<List<State>> cycles = new TarjanSimpleCycles<>(stateSpace).findSimpleCycles();
            for (List<State> cycle : cycles) {
                // if there is an accepting Buechi state in the cycle, a counterexample is found
                for (State state : cycle) {
                    if (buechiAutomaton.isAcceptingSet(state.getBuechiNodes())) {
                        return createLTLCounterExampleFound(visited.size(), state);
                    }
                }
            }
        }
        return resultVerified;
    }
}
Also used : Status(com.microsoft.z3.Status) BoolExpr(com.microsoft.z3.BoolExpr) Model(com.microsoft.z3.Model) ModelCheckingResult(de.bmoth.modelchecker.ModelCheckingResult)

Aggregations

BoolExpr (com.microsoft.z3.BoolExpr)15 Solver (com.microsoft.z3.Solver)14 Context (com.microsoft.z3.Context)12 Status (com.microsoft.z3.Status)9 List (java.util.List)7 ArithExpr (com.microsoft.z3.ArithExpr)6 HashMap (java.util.HashMap)5 Graph (org.batfish.symbolic.Graph)5 GraphEdge (org.batfish.symbolic.GraphEdge)5 BitVecExpr (com.microsoft.z3.BitVecExpr)4 Model (com.microsoft.z3.Model)3 Test (org.junit.Test)3 Z3Exception (com.microsoft.z3.Z3Exception)2 Z3_ast_print_mode (com.microsoft.z3.enumerations.Z3_ast_print_mode)2 LitmusLexer (dartagnan.LitmusLexer)2 LitmusParser (dartagnan.LitmusParser)2 PorthosLexer (dartagnan.PorthosLexer)2 PorthosParser (dartagnan.PorthosParser)2 Program (dartagnan.program.Program)2 Utils (dartagnan.utils.Utils)2