use of com.microsoft.z3.BoolExpr 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);
}
use of com.microsoft.z3.BoolExpr in project batfish by batfish.
the class Encoder method verify.
/**
* Checks that a property is always true by seeing if the encoding is unsatisfiable. mkIf the
* model is satisfiable, then there is a counter example to the property.
*
* @return A VerificationResult indicating the status of the check.
*/
public Tuple<VerificationResult, Model> verify() {
EncoderSlice mainSlice = _slices.get(MAIN_SLICE_NAME);
int numVariables = _allVariables.size();
int numConstraints = _solver.getAssertions().length;
int numNodes = mainSlice.getGraph().getConfigurations().size();
int numEdges = 0;
for (Map.Entry<String, Set<String>> e : mainSlice.getGraph().getNeighbors().entrySet()) {
numEdges += e.getValue().size();
}
long start = System.currentTimeMillis();
Status status = _solver.check();
long time = System.currentTimeMillis() - start;
VerificationStats stats = null;
if (_question.getBenchmark()) {
stats = new VerificationStats();
stats.setAvgNumNodes(numNodes);
stats.setMaxNumNodes(numNodes);
stats.setMinNumNodes(numNodes);
stats.setAvgNumEdges(numEdges);
stats.setMaxNumEdges(numEdges);
stats.setMinNumEdges(numEdges);
stats.setAvgNumVariables(numVariables);
stats.setMaxNumVariables(numVariables);
stats.setMinNumVariables(numVariables);
stats.setAvgNumConstraints(numConstraints);
stats.setMaxNumConstraints(numConstraints);
stats.setMinNumConstraints(numConstraints);
stats.setAvgSolverTime(time);
stats.setMaxSolverTime(time);
stats.setMinSolverTime(time);
}
if (status == Status.UNSATISFIABLE) {
VerificationResult res = new VerificationResult(true, null, null, null, null, null, stats);
return new Tuple<>(res, null);
} else if (status == Status.UNKNOWN) {
throw new BatfishException("ERROR: satisfiability unknown");
} else {
VerificationResult result;
Model m;
while (true) {
m = _solver.getModel();
SortedMap<String, String> model = new TreeMap<>();
SortedMap<String, String> packetModel = new TreeMap<>();
SortedSet<String> fwdModel = new TreeSet<>();
SortedMap<String, SortedMap<String, String>> envModel = new TreeMap<>();
SortedSet<String> failures = new TreeSet<>();
buildCounterExample(this, m, model, packetModel, fwdModel, envModel, failures);
if (_previousEncoder != null) {
buildCounterExample(_previousEncoder, m, model, packetModel, fwdModel, envModel, failures);
}
result = new VerificationResult(false, model, packetModel, envModel, fwdModel, failures, stats);
if (!_question.getMinimize()) {
break;
}
BoolExpr blocking = environmentBlockingClause(m);
add(blocking);
Status s = _solver.check();
if (s == Status.UNSATISFIABLE) {
break;
}
if (s == Status.UNKNOWN) {
throw new BatfishException("ERROR: satisfiability unknown");
}
}
return new Tuple<>(result, m);
}
}
use of com.microsoft.z3.BoolExpr in project batfish by batfish.
the class EncoderSlice method initForwardingAcross.
/*
* Initialize boolean expressions to represent that traffic sent along
* and edge will reach the other side of the edge.
*/
private void initForwardingAcross() {
_symbolicDecisions.getDataForwarding().forEach((router, edge, var) -> {
BoolExpr inAcl;
if (edge.getEnd() == null) {
inAcl = mkTrue();
} else {
GraphEdge ge = getGraph().getOtherEnd().get(edge);
inAcl = _inboundAcls.get(ge);
if (inAcl == null) {
inAcl = mkTrue();
}
}
_forwardsAcross.put(router, edge, mkAnd(var, inAcl));
});
}
use of com.microsoft.z3.BoolExpr in project batfish by batfish.
the class EncoderSlice method equalCommunities.
private BoolExpr equalCommunities(SymbolicRoute best, SymbolicRoute vars) {
BoolExpr acc = mkTrue();
for (Map.Entry<CommunityVar, BoolExpr> entry : best.getCommunities().entrySet()) {
CommunityVar cvar = entry.getKey();
BoolExpr var = entry.getValue();
BoolExpr other = vars.getCommunities().get(cvar);
if (other == null) {
acc = mkAnd(acc, mkNot(var));
} else {
acc = mkAnd(acc, mkEq(var, other));
}
}
return acc;
}
use of com.microsoft.z3.BoolExpr in project batfish by batfish.
the class EncoderSlice method computeIpProtocols.
/*
* Convert a set of ip protocols to a boolean expression on the symbolic packet
*/
private BoolExpr computeIpProtocols(Set<IpProtocol> ipProtos) {
BoolExpr acc = mkFalse();
for (IpProtocol proto : ipProtos) {
ArithExpr protoNum = mkInt(proto.number());
acc = mkOr(acc, mkEq(protoNum, _symbolicPacket.getIpProtocol()));
}
return (BoolExpr) acc.simplify();
}
Aggregations