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();
}
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("");
}
}
}
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);
}
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;
}
}
Aggregations