Search in sources :

Example 66 with Model

use of com.microsoft.z3.Model 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 67 with Model

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

the class VerificationResult method debug.

public void debug(EncoderSlice enc, boolean showConstraints, String filter) {
    if (showConstraints) {
        System.out.println("================= Constraints ==================");
        for (BoolExpr be : enc.getSolver().getAssertions()) {
            String x = be.simplify().toString();
            if (filter == null || x.contains(filter)) {
                System.out.println(x);
            }
        }
    }
    if (_verified) {
        System.out.println("verified");
    } else {
        System.out.println("================= Model ================");
        enc.getSymbolicDecisions().getDataForwarding().forEach((router, map) -> map.forEach((edge, e) -> {
            String expr = e.toString();
            if (expr.contains("DATA-")) {
                String result = _model.get(expr);
                if ("true".equals(result)) {
                    System.out.println(edge);
                }
            }
        }));
        System.out.println("");
        _model.forEach((var, val) -> {
            if (filter == null || var.contains(filter)) {
                System.out.println(var + "=" + val);
            }
        });
    }
    if (enc.getUnsatCore().getDoTrack()) {
        System.out.println("================= Unsat Core ================");
        for (BoolExpr be : enc.getSolver().getUnsatCore()) {
            BoolExpr constraint = enc.getUnsatCore().getTrackingVars().get(be.toString());
            System.out.println("Var: " + be);
            System.out.println(constraint);
            System.out.println("");
        }
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) SortedSet(java.util.SortedSet) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) BoolExpr(com.microsoft.z3.BoolExpr) SortedMap(java.util.SortedMap) Nullable(javax.annotation.Nullable) BoolExpr(com.microsoft.z3.BoolExpr)

Example 68 with Model

use of com.microsoft.z3.Model 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 69 with Model

use of com.microsoft.z3.Model 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

Test (org.junit.Test)45 Model (org.eclipse.xtext.valueconverter.bug250313.Model)30 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)16 BoolExpr (com.microsoft.z3.BoolExpr)11 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)11 Model (org.eclipse.xtext.parsetree.reconstr.bug299395.Model)9 SubModel (org.eclipse.xtext.parsetree.reconstr.bug299395.SubModel)9 Model (com.microsoft.z3.Model)7 Init (dartagnan.program.Init)7 Program (dartagnan.program.Program)7 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 com.microsoft.z3 (com.microsoft.z3)6 Event (dartagnan.program.Event)6 Load (dartagnan.program.Load)6 Location (dartagnan.program.Location)6 MemEvent (dartagnan.program.MemEvent)6 Register (dartagnan.program.Register)6 Store (dartagnan.program.Store)6 EPackage (org.eclipse.emf.ecore.EPackage)6