Search in sources :

Example 1 with Sort

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

the class MachineToZ3Translator method getDistinctVars.

public BoolExpr getDistinctVars(int from, int to) {
    if (tuple == null) {
        Expr[] variables = getVariables().stream().map(this::getVariable).toArray(Expr[]::new);
        Symbol[] symbols = Arrays.stream(variables).map(var -> var.getFuncDecl().getName()).toArray(Symbol[]::new);
        Sort[] sorts = Arrays.stream(variables).map(Expr::getSort).toArray(Sort[]::new);
        tuple = z3Context.mkTupleSort(z3Context.mkSymbol("tuple"), symbols, sorts);
    }
    Expr[] distinct = new Expr[to - from + 1];
    for (int v = from, i = 0; v <= to; v++, i++) {
        int finalV = v;
        Expr[] vector = getVariables().stream().map(var -> getPrimedVariable(var, new TranslationOptions(finalV))).toArray(Expr[]::new);
        distinct[i] = tuple.mkDecl().apply(vector);
    }
    return z3Context.mkDistinct(distinct);
}
Also used : SubstitutionOptions(de.bmoth.backend.SubstitutionOptions) java.util(java.util) com.microsoft.z3(com.microsoft.z3) PRIMED_0(de.bmoth.backend.TranslationOptions.PRIMED_0) UNPRIMED(de.bmoth.backend.TranslationOptions.UNPRIMED) de.bmoth.parser.ast.nodes(de.bmoth.parser.ast.nodes) SubstitutionVisitor(de.bmoth.parser.ast.visitors.SubstitutionVisitor) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) TranslationOptions(de.bmoth.backend.TranslationOptions) TranslationOptions(de.bmoth.backend.TranslationOptions)

Example 2 with Sort

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

the class Z3TypeInference method getZ3Sort.

public Sort getZ3Sort(Z3Type t, Context z3Context) {
    if (t instanceof Z3BasicType) {
        switch(((Z3BasicType) t).kind) {
            case INTEGER:
                return z3Context.getIntSort();
            case BOOL:
                return z3Context.getBoolSort();
            default:
                break;
        }
    } else if (t instanceof Z3SetType) {
        Sort subSort = getZ3Sort(((Z3SetType) t).subtype, z3Context);
        return z3Context.mkSetSort(subSort);
    } else if (t instanceof Z3CoupleType) {
        Z3CoupleType couple = (Z3CoupleType) t;
        Sort left = getZ3Sort(couple.left, z3Context);
        Sort right = getZ3Sort(couple.right, z3Context);
        Sort[] subSorts = new Sort[2];
        subSorts[0] = left;
        subSorts[1] = right;
        return z3Context.mkTupleSort(z3Context.mkSymbol("couple"), new Symbol[] { z3Context.mkSymbol("left"), z3Context.mkSymbol("right") }, subSorts);
    } else if (t instanceof Z3SequenceType) {
        Sort subSort = getZ3Sort(((Z3SequenceType) t).subtype, z3Context);
        Sort intType = z3Context.getIntSort();
        Sort[] subSorts = new Sort[2];
        subSorts[0] = z3Context.mkArraySort(intType, subSort);
        subSorts[1] = intType;
        return z3Context.mkTupleSort(z3Context.mkSymbol("sequence"), new Symbol[] { z3Context.mkSymbol("array"), z3Context.mkSymbol("size") }, subSorts);
    } else if (t instanceof Z3DeferredType) {
        return z3Context.mkUninterpretedSort(((Z3DeferredType) t).name);
    } else if (t instanceof Z3EnumeratedSetType) {
        List<String> elements = ((Z3EnumeratedSetType) t).elements;
        String[] array = elements.toArray(new String[elements.size()]);
        return z3Context.mkEnumSort(((Z3EnumeratedSetType) t).name, array);
    }
    throw new AssertionError("Missing Type Conversion: " + t.getClass());
}
Also used : Symbol(com.microsoft.z3.Symbol) Sort(com.microsoft.z3.Sort) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Sort

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

the class Encoder method initFailedLinkVariables.

/*
   * Initialize symbolic variables to represent link failures.
   */
private void initFailedLinkVariables() {
    for (List<GraphEdge> edges : _graph.getEdgeMap().values()) {
        for (GraphEdge ge : edges) {
            if (ge.getPeer() == null) {
                Interface i = ge.getStart();
                String name = getId() + "_FAILED-EDGE_" + ge.getRouter() + "_" + i.getName();
                ArithExpr var = getCtx().mkIntConst(name);
                _symbolicFailures.getFailedEdgeLinks().put(ge, var);
                _allVariables.put(var.toString(), var);
            }
        }
    }
    for (Entry<String, Set<String>> entry : _graph.getNeighbors().entrySet()) {
        String router = entry.getKey();
        Set<String> peers = entry.getValue();
        for (String peer : peers) {
            // sort names for unique
            String pair = (router.compareTo(peer) < 0 ? router + "_" + peer : peer + "_" + router);
            String name = getId() + "_FAILED-EDGE_" + pair;
            ArithExpr var = _ctx.mkIntConst(name);
            _symbolicFailures.getFailedInternalLinks().put(router, peer, var);
            _allVariables.put(var.toString(), var);
        }
    }
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) GraphEdge(org.batfish.symbolic.GraphEdge) Interface(org.batfish.datamodel.Interface)

Aggregations

Streams (com.google.common.collect.Streams)1 com.microsoft.z3 (com.microsoft.z3)1 ArithExpr (com.microsoft.z3.ArithExpr)1 Sort (com.microsoft.z3.Sort)1 Symbol (com.microsoft.z3.Symbol)1 SubstitutionOptions (de.bmoth.backend.SubstitutionOptions)1 TranslationOptions (de.bmoth.backend.TranslationOptions)1 PRIMED_0 (de.bmoth.backend.TranslationOptions.PRIMED_0)1 UNPRIMED (de.bmoth.backend.TranslationOptions.UNPRIMED)1 de.bmoth.parser.ast.nodes (de.bmoth.parser.ast.nodes)1 SubstitutionVisitor (de.bmoth.parser.ast.visitors.SubstitutionVisitor)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 Interface (org.batfish.datamodel.Interface)1