Search in sources :

Example 36 with Sig

use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.

the class ScopeComputer method derive_scope_from_parent.

// ===========================================================================================================================//
/**
 * If A is not toplevel, and we haven't been able to derive its scope yet, then
 * give it its parent's scope.
 */
private boolean derive_scope_from_parent(Iterable<Sig> sigs) throws Err {
    boolean changed = false;
    Sig trouble = null;
    for (Sig s : sigs) if (!s.builtin && !s.isTopLevel() && sig2scope(s) < 0 && (s instanceof PrimSig)) {
        PrimSig p = ((PrimSig) s).parent;
        int pb = sig2scope(p);
        if (pb >= 0) {
            sig2scope(s, pb);
            changed = true;
        } else {
            trouble = s;
        }
    }
    if (changed)
        return true;
    if (trouble == null)
        return false;
    throw new ErrorSyntax(cmd.pos, "You must specify a scope for sig \"" + trouble + "\"");
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 37 with Sig

use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.

the class ScopeComputer method derive_overall_scope.

// ===========================================================================================================================//
/**
 * If A is toplevel, and we haven't been able to derive its scope yet, then let
 * it get the "overall" scope.
 */
private boolean derive_overall_scope(Iterable<Sig> sigs) throws Err {
    boolean changed = false;
    final int overall = (cmd.overall < 0 && cmd.scope.size() == 0) ? 3 : cmd.overall;
    for (Sig s : sigs) if (!s.builtin && s.isTopLevel() && sig2scope(s) < 0) {
        if (s.isEnum != null) {
            sig2scope(s, 0);
            continue;
        }
        // enum without children should get the empty set
        if (overall < 0)
            throw new ErrorSyntax(cmd.pos, "You must specify a scope for sig \"" + s + "\"");
        sig2scope(s, overall);
        changed = true;
    }
    return changed;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax)

Example 38 with Sig

use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.

the class TranslateAlloyToKodkod method execute_greedyCommand.

private static A4Solution execute_greedyCommand(A4Reporter rep, Iterable<Sig> sigs, Command usercommand, A4Options opt) throws Exception {
    // FIXTHIS: if the next command has a "smaller scope" than the last
    // command, we would get a Kodkod exception...
    // FIXTHIS: if the solver is "toCNF" or "toKodkod" then this method will
    // throw an Exception...
    // FIXTHIS: does solution enumeration still work when we're doing a
    // greedy solve?
    TranslateAlloyToKodkod tr = null;
    try {
        long start = System.currentTimeMillis();
        GreedySimulator sim = new GreedySimulator();
        sim.allSigs = sigs;
        sim.partial = null;
        A4Reporter rep2 = new A4Reporter(rep) {

            private boolean first = true;

            @Override
            public void translate(String solver, int bitwidth, int maxseq, int skolemDepth, int symmetry) {
                if (first)
                    super.translate(solver, bitwidth, maxseq, skolemDepth, symmetry);
                first = false;
            }

            @Override
            public void resultSAT(Object command, long solvingTime, Object solution) {
            }

            @Override
            public void resultUNSAT(Object command, long solvingTime, Object solution) {
            }
        };
        // Form the list of commands
        List<Command> commands = new ArrayList<Command>();
        while (usercommand != null) {
            commands.add(usercommand);
            usercommand = usercommand.parent;
        }
        // For each command...
        A4Solution sol = null;
        for (int i = commands.size() - 1; i >= 0; i--) {
            Command cmd = commands.get(i);
            sim.growableSigs = cmd.getGrowableSigs();
            while (cmd != null) {
                rep.debug(cmd.scope.toString());
                usercommand = cmd;
                tr = new TranslateAlloyToKodkod(rep2, opt, sigs, cmd);
                tr.makeFacts(cmd.formula);
                sim.totalOrderPredicates = tr.totalOrderPredicates;
                sol = tr.frame.solve(rep2, cmd, sim.partial == null || cmd.check ? new Simplifier() : sim, false);
                if (!sol.satisfiable() && !cmd.check) {
                    start = System.currentTimeMillis() - start;
                    if (sim.partial == null) {
                        rep.resultUNSAT(cmd, start, sol);
                        return sol;
                    } else {
                        rep.resultSAT(cmd, start, sim.partial);
                        return sim.partial;
                    }
                }
                if (sol.satisfiable() && cmd.check) {
                    start = System.currentTimeMillis() - start;
                    rep.resultSAT(cmd, start, sol);
                    return sol;
                }
                sim.partial = sol;
                if (sim.growableSigs.isEmpty())
                    break;
                for (Sig s : sim.growableSigs) {
                    CommandScope sc = cmd.getScope(s);
                    if (sc.increment > sc.endingScope - sc.startingScope) {
                        cmd = null;
                        break;
                    }
                    cmd = cmd.change(s, sc.isExact, sc.startingScope + sc.increment, sc.endingScope, sc.increment);
                }
            }
        }
        if (sol.satisfiable())
            rep.resultSAT(usercommand, System.currentTimeMillis() - start, sol);
        else
            rep.resultUNSAT(usercommand, System.currentTimeMillis() - start, sol);
        return sol;
    } catch (CapacityExceededException ex) {
        throw rethrow(ex);
    } catch (HigherOrderDeclException ex) {
        Pos p = tr != null ? tr.frame.kv2typepos(ex.decl().variable()).b : Pos.UNKNOWN;
        throw new ErrorType(p, "Analysis cannot be performed since it requires higher-order quantification that could not be skolemized.");
    }
}
Also used : A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) ArrayList(java.util.ArrayList) Sig(edu.mit.csail.sdg.ast.Sig) CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Command(edu.mit.csail.sdg.ast.Command) Pos(edu.mit.csail.sdg.alloy4.Pos) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException) CommandScope(edu.mit.csail.sdg.ast.CommandScope)

Example 39 with Sig

use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.

the class CompUtil method areIntsUsed.

// =============================================================================================================//
/**
 * Whether or not Int appears in the relation types found in these sigs
 */
public static boolean areIntsUsed(Iterable<Sig> sigs, Command cmd) {
    /* check for Int-typed relations */
    for (Sig s : sigs) {
        for (Field f : s.getFields()) {
            for (ProductType pt : f.type()) {
                for (int k = 0; k < pt.arity(); k++) {
                    if (pt.get(k) == SIGINT || pt.get(k) == SEQIDX)
                        return true;
                }
            }
        }
    }
    if (cmd == null)
        return false;
    /* check expressions; look for CAST2SIGING (Int[]) */
    try {
        Object intTriggerNode;
        intTriggerNode = cmd.formula.accept(new VisitQueryOnce<Object>() {

            @Override
            public Object visit(ExprCall x) throws Err {
                // Int[]
                if (x.fun.label.startsWith("integer/"))
                    return null;
                return super.visit(x);
            }

            @Override
            public Object visit(ExprUnary x) throws Err {
                if (x.op == Op.CAST2SIGINT)
                    return x;
                return super.visit(x);
            }
        });
        if (intTriggerNode != null)
            return true;
    } catch (Err e) {
    }
    return false;
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) VisitQueryOnce(edu.mit.csail.sdg.ast.VisitQueryOnce) Field(edu.mit.csail.sdg.ast.Sig.Field) ExprCall(edu.mit.csail.sdg.ast.ExprCall) Err(edu.mit.csail.sdg.alloy4.Err) ProductType(edu.mit.csail.sdg.ast.Type.ProductType) ExprUnary(edu.mit.csail.sdg.ast.ExprUnary)

Example 40 with Sig

use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.

the class A4SolutionReader method parseSig.

/**
 * Parse sig/set.
 */
private Sig parseSig(String id, int depth) throws IOException, Err {
    Sig ans = id2sig.get(id);
    if (ans != null)
        return ans;
    XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown SigID " + id + " encountered.");
    if (!node.is("sig"))
        throw new IOException("ID " + id + " is not a sig.");
    String label = label(node);
    Attr isAbstract = yes(node, "abstract") ? Attr.ABSTRACT : null;
    Attr isOne = yes(node, "one") ? Attr.ONE : null;
    Attr isLone = yes(node, "lone") ? Attr.LONE : null;
    Attr isSome = yes(node, "some") ? Attr.SOME : null;
    Attr isPrivate = yes(node, "private") ? Attr.PRIVATE : null;
    Attr isMeta = yes(node, "meta") ? Attr.META : null;
    Attr isEnum = yes(node, "enum") ? Attr.ENUM : null;
    Attr isExact = yes(node, "exact") ? Attr.EXACT : null;
    if (yes(node, "builtin")) {
        if (label.equals(UNIV.label)) {
            id2sig.put(id, UNIV);
            return UNIV;
        }
        if (label.equals(SIGINT.label)) {
            id2sig.put(id, SIGINT);
            return SIGINT;
        }
        if (label.equals(SEQIDX.label)) {
            id2sig.put(id, SEQIDX);
            return SEQIDX;
        }
        if (label.equals(STRING.label)) {
            id2sig.put(id, STRING);
            return STRING;
        }
        throw new IOException("Unknown builtin sig: " + label + " (id=" + id + ")");
    }
    if (depth > nmap.size())
        throw new IOException("Sig " + label + " (id=" + id + ") is in a cyclic inheritance relationship.");
    List<Sig> parents = null;
    TupleSet ts = factory.noneOf(1);
    for (XMLNode sub : node) {
        if (sub.is("atom")) {
            ts.add(factory.tuple(sub.getAttribute("label")));
            continue;
        }
        if (!sub.is("type"))
            continue;
        Sig parent = parseSig(sub.getAttribute("ID"), depth + 1);
        if (parents == null)
            parents = new ArrayList<Sig>();
        parents.add(parent);
    }
    if (parents == null) {
        String parentID = node.getAttribute("parentID");
        Sig parent = parseSig(parentID, depth + 1);
        if (!(parent instanceof PrimSig))
            throw new IOException("Parent of sig " + label + " (id=" + id + ") must not be a subset sig.");
        for (Expr choice : choices) if (choice instanceof PrimSig && parent == ((PrimSig) choice).parent && ((Sig) choice).label.equals(label)) {
            ans = (Sig) choice;
            choices.remove(choice);
            break;
        }
        if (ans == null) {
            ans = new PrimSig(label, (PrimSig) parent, isAbstract, isLone, isOne, isSome, isPrivate, isMeta, isEnum);
            allsigs.add(ans);
        }
    } else {
        for (Expr choice : choices) if (choice instanceof SubsetSig && ((Sig) choice).label.equals(label) && sameset(parents, ((SubsetSig) choice).parents)) {
            ans = (Sig) choice;
            choices.remove(choice);
            break;
        }
        if (ans == null) {
            ans = new SubsetSig(label, parents, isExact, isLone, isOne, isSome, isPrivate, isMeta);
            allsigs.add(ans);
        }
    }
    id2sig.put(id, ans);
    expr2ts.put(ans, ts);
    if (ans instanceof PrimSig) {
        // Add the atoms in this SIG into all parent sigs
        for (PrimSig ans2 = ((PrimSig) ans).parent; ans2 != null && !ans2.builtin; ans2 = ans2.parent) {
            TupleSet ts2 = expr2ts.get(ans2);
            if (ts2 == null)
                ts2 = ts.clone();
            else {
                ts2 = ts2.clone();
                ts2.addAll(ts);
            }
            expr2ts.put(ans2, ts2);
        }
    }
    return ans;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) TupleSet(kodkod.instance.TupleSet) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Attr(edu.mit.csail.sdg.ast.Attr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

Sig (edu.mit.csail.sdg.ast.Sig)45 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)33 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)25 Field (edu.mit.csail.sdg.ast.Sig.Field)17 Expr (edu.mit.csail.sdg.ast.Expr)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 ArrayList (java.util.ArrayList)8 Func (edu.mit.csail.sdg.ast.Func)7 HashMap (java.util.HashMap)7 TupleSet (kodkod.instance.TupleSet)7 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)6 A4Solution (edu.mit.csail.sdg.translator.A4Solution)6 LinkedHashMap (java.util.LinkedHashMap)6 Err (edu.mit.csail.sdg.alloy4.Err)5 SafeList (edu.mit.csail.sdg.alloy4.SafeList)4 Decl (edu.mit.csail.sdg.ast.Decl)4 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)4