use of com.microsoft.z3.Model in project batfish by batfish.
the class EncoderSlice method addHeaderSpaceConstraint.
/*
* Add constraints for the type of packets we will consider in the model.
* This can include restrictions on any packet field such as dstIp, protocol etc.
*/
private void addHeaderSpaceConstraint() {
BoolExpr acc;
if (_headerSpace.getDstIps().size() > 0) {
acc = mkFalse();
for (IpWildcard ipWildcard : _headerSpace.getDstIps()) {
BoolExpr bound = ipWildCardBound(_symbolicPacket.getDstIp(), ipWildcard);
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotDstIps().size() > 0) {
acc = mkTrue();
for (IpWildcard ipWildcard : _headerSpace.getNotDstIps()) {
BoolExpr bound = ipWildCardBound(_symbolicPacket.getDstIp(), ipWildcard);
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
if (_headerSpace.getSrcIps().size() > 0) {
acc = mkFalse();
for (IpWildcard ipWildcard : _headerSpace.getSrcIps()) {
BoolExpr bound = ipWildCardBound(_symbolicPacket.getSrcIp(), ipWildcard);
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotSrcIps().size() > 0) {
acc = mkTrue();
for (IpWildcard ipWildcard : _headerSpace.getNotSrcIps()) {
BoolExpr bound = ipWildCardBound(_symbolicPacket.getSrcIp(), ipWildcard);
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
if (_headerSpace.getSrcOrDstIps().size() > 0) {
acc = mkFalse();
for (IpWildcard ipWildcard : _headerSpace.getSrcOrDstIps()) {
BoolExpr bound1 = ipWildCardBound(_symbolicPacket.getDstIp(), ipWildcard);
BoolExpr bound2 = ipWildCardBound(_symbolicPacket.getSrcIp(), ipWildcard);
acc = mkOr(acc, bound1, bound2);
}
add(acc);
}
if (_headerSpace.getDstPorts().size() > 0) {
acc = mkFalse();
for (SubRange subRange : _headerSpace.getDstPorts()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getDstPort(), subRange);
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotDstPorts().size() > 0) {
acc = mkTrue();
for (SubRange subRange : _headerSpace.getNotDstPorts()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getDstPort(), subRange);
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
if (_headerSpace.getSrcPorts().size() > 0) {
acc = mkFalse();
for (SubRange subRange : _headerSpace.getSrcPorts()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getDstPort(), subRange);
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotSrcPorts().size() > 0) {
acc = mkTrue();
for (SubRange subRange : _headerSpace.getNotSrcPorts()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getDstPort(), subRange);
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
if (_headerSpace.getSrcOrDstPorts().size() > 0) {
acc = mkFalse();
for (SubRange subRange : _headerSpace.getSrcOrDstPorts()) {
BoolExpr bound1 = subRangeBound(_symbolicPacket.getDstPort(), subRange);
BoolExpr bound2 = subRangeBound(_symbolicPacket.getSrcPort(), subRange);
acc = mkOr(acc, bound1, bound2);
}
add(acc);
}
if (_headerSpace.getIcmpTypes().size() > 0) {
acc = mkFalse();
for (SubRange subRange : _headerSpace.getIcmpTypes()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getIcmpType(), subRange);
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotIcmpTypes().size() > 0) {
acc = mkTrue();
for (SubRange subRange : _headerSpace.getNotIcmpTypes()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getIcmpType(), subRange);
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
if (_headerSpace.getIcmpCodes().size() > 0) {
acc = mkFalse();
for (SubRange subRange : _headerSpace.getIcmpCodes()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getIcmpCode(), subRange);
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotIcmpCodes().size() > 0) {
acc = mkTrue();
for (SubRange subRange : _headerSpace.getNotIcmpCodes()) {
BoolExpr bound = subRangeBound(_symbolicPacket.getIcmpCode(), subRange);
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
if (_headerSpace.getIpProtocols().size() > 0) {
acc = mkFalse();
for (IpProtocol ipProtocol : _headerSpace.getIpProtocols()) {
BoolExpr bound = mkEq(_symbolicPacket.getIpProtocol(), mkInt(ipProtocol.number()));
acc = mkOr(acc, bound);
}
add(acc);
}
if (_headerSpace.getNotIpProtocols().size() > 0) {
acc = mkTrue();
for (IpProtocol ipProtocol : _headerSpace.getNotIpProtocols()) {
BoolExpr bound = mkEq(_symbolicPacket.getIpProtocol(), mkInt(ipProtocol.number()));
acc = mkAnd(acc, mkNot(bound));
}
add(acc);
}
// TODO: need to implement fragment offsets, Ecns, states, etc
}
use of com.microsoft.z3.Model in project batfish by batfish.
the class PropertyChecker method checkDeterminism.
/*
* Check if there exist multiple stable solutions to the network.
* If so, reports the forwarding differences between the two cases.
*/
public AnswerElement checkDeterminism(HeaderQuestion q) {
Graph graph = new Graph(_batfish);
Encoder enc1 = new Encoder(_settings, graph, q);
Encoder enc2 = new Encoder(enc1, graph, q);
enc1.computeEncoding();
enc2.computeEncoding();
addEnvironmentConstraints(enc1, q.getBaseEnvironmentType());
BoolExpr relatedFailures = relateFailures(enc1, enc2);
BoolExpr relatedEnvs = relateEnvironments(enc1, enc2);
BoolExpr relatedPkts = relatePackets(enc1, enc2);
BoolExpr related = enc1.mkAnd(relatedFailures, relatedEnvs, relatedPkts);
BoolExpr required = enc1.mkTrue();
for (GraphEdge ge : graph.getAllRealEdges()) {
SymbolicDecisions d1 = enc1.getMainSlice().getSymbolicDecisions();
SymbolicDecisions d2 = enc2.getMainSlice().getSymbolicDecisions();
BoolExpr dataFwd1 = d1.getDataForwarding().get(ge.getRouter(), ge);
BoolExpr dataFwd2 = d2.getDataForwarding().get(ge.getRouter(), ge);
assert dataFwd1 != null;
assert dataFwd2 != null;
required = enc1.mkAnd(required, enc1.mkEq(dataFwd1, dataFwd2));
}
enc1.add(related);
enc1.add(enc1.mkNot(required));
Tuple<VerificationResult, Model> tup = enc1.verify();
VerificationResult res = tup.getFirst();
Model model = tup.getSecond();
SortedSet<String> case1 = null;
SortedSet<String> case2 = null;
Flow flow = null;
CounterExample ce = new CounterExample(model);
if (!res.isVerified()) {
case1 = new TreeSet<>();
case2 = new TreeSet<>();
flow = ce.buildFlow(enc1.getMainSlice().getSymbolicPacket(), "(none)");
for (GraphEdge ge : graph.getAllRealEdges()) {
SymbolicDecisions d1 = enc1.getMainSlice().getSymbolicDecisions();
SymbolicDecisions d2 = enc2.getMainSlice().getSymbolicDecisions();
BoolExpr dataFwd1 = d1.getDataForwarding().get(ge.getRouter(), ge);
BoolExpr dataFwd2 = d2.getDataForwarding().get(ge.getRouter(), ge);
assert dataFwd1 != null;
assert dataFwd2 != null;
boolean b1 = ce.boolVal(dataFwd1);
boolean b2 = ce.boolVal(dataFwd2);
if (b1 != b2) {
if (b1) {
String route = ce.buildRoute(enc1.getMainSlice(), ge);
String msg = ge + " -- " + route;
case1.add(msg);
}
if (b2) {
String route = ce.buildRoute(enc2.getMainSlice(), ge);
String msg = ge + " -- " + route;
case2.add(msg);
}
}
}
}
// Ensure canonical order
boolean less = (case1 == null || (case1.first().compareTo(case2.first()) < 0));
if (less) {
return new SmtDeterminismAnswerElement(flow, case1, case2);
} else {
return new SmtDeterminismAnswerElement(flow, case2, case1);
}
}
use of com.microsoft.z3.Model in project bmoth by hhu-stups.
the class Issue73Test method testSatPredicateWithoutModel.
@Test
public void testSatPredicateWithoutModel() throws IOException {
String formula = "1 < 2";
BoolExpr constraint = translatePredicate(formula, z3Context);
SolutionFinder finder = new SolutionFinder(z3Solver, z3Context);
Set<Model> solutions = finder.findSolutions(constraint, 20);
assertEquals(0, solutions.size());
}
use of com.microsoft.z3.Model in project xtext-core by eclipse.
the class Bug305397SemanticSequencer method sequence.
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == Bug305397Package.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case Bug305397Package.ELEMENT:
sequence_Element(context, (Element) semanticObject);
return;
case Bug305397Package.MODEL:
sequence_Model(context, (Model) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
use of com.microsoft.z3.Model in project xtext-core by eclipse.
the class Bug299395TestLanguageSemanticSequencer method sequence.
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == Bug299395Package.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case Bug299395Package.MODEL:
sequence_Model(context, (Model) semanticObject);
return;
case Bug299395Package.SUB_MODEL:
sequence_SubModel(context, (SubModel) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Aggregations