Search in sources :

Example 41 with Pos

use of edu.mit.csail.sdg.alloy4.Pos in project org.alloytools.alloy by AlloyTools.

the class CompModule method rejectNameClash.

// ============================================================================================================================//
private static void rejectNameClash(final List<CompModule> modules) throws Err {
    // The Alloy language forbids two overlapping sigs from having fields
    // with the same name.
    // In other words: if 2 fields have the same name, then their type's
    // first column must not intersect.
    final Map<String, List<Field>> fieldname2fields = new LinkedHashMap<String, List<Field>>();
    for (CompModule m : modules) {
        for (Sig sig : m.sigs.values()) {
            for (Field field : sig.getFields()) {
                List<Field> peers = fieldname2fields.get(field.label);
                if (peers == null) {
                    peers = new ArrayList<Field>();
                    fieldname2fields.put(field.label, peers);
                }
                for (Field field2 : peers) if (field.type().firstColumnOverlaps(field2.type()))
                    throw new ErrorType(field.pos, "Two overlapping signatures cannot have\n" + "two fields with the same name \"" + field.label + "\":\n\n1) one is in sig \"" + field.sig + "\"\n" + field.pos + "\n\n2) the other is in sig \"" + field2.sig + "\"\n" + field2.pos);
                peers.add(field);
            }
        }
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Util.asList(edu.mit.csail.sdg.alloy4.Util.asList) SafeList(edu.mit.csail.sdg.alloy4.SafeList) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) List(java.util.List) ExprList(edu.mit.csail.sdg.ast.ExprList) ArrayList(java.util.ArrayList) JoinableList(edu.mit.csail.sdg.alloy4.JoinableList) ConstList(edu.mit.csail.sdg.alloy4.ConstList) LinkedHashMap(java.util.LinkedHashMap)

Example 42 with Pos

use of edu.mit.csail.sdg.alloy4.Pos in project org.alloytools.alloy by AlloyTools.

the class TranslateAlloyToKodkod method execute_command.

/**
 * Based on the specified "options", execute one command and return the
 * resulting A4Solution object.
 *
 * @param rep - if nonnull, we'll send compilation diagnostic messages to it
 * @param sigs - the list of sigs; this list must be complete
 * @param cmd - the Command to execute
 * @param opt - the set of options guiding the execution of the command
 * @return null if the user chose "save to FILE" as the SAT solver, and nonnull
 *         if the solver finishes the entire solving and is either satisfiable
 *         or unsatisfiable.
 *         <p>
 *         If the return value X is satisfiable, you can call X.next() to get
 *         the next satisfying solution X2; and you can call X2.next() to get
 *         the next satisfying solution X3... until you get an unsatisfying
 *         solution.
 */
public static A4Solution execute_command(A4Reporter rep, Iterable<Sig> sigs, Command cmd, A4Options opt) throws Err {
    if (rep == null)
        rep = A4Reporter.NOP;
    TranslateAlloyToKodkod tr = null;
    try {
        if (cmd.parent != null || !cmd.getGrowableSigs().isEmpty())
            return execute_greedyCommand(rep, sigs, cmd, opt);
        tr = new TranslateAlloyToKodkod(rep, opt, sigs, cmd);
        tr.makeFacts(cmd.formula);
        return tr.frame.solve(rep, cmd, new Simplifier(), false);
    } catch (UnsatisfiedLinkError ex) {
        throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim(), ex);
    } 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.");
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        else
            throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
}
Also used : CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Err(edu.mit.csail.sdg.alloy4.Err) Pos(edu.mit.csail.sdg.alloy4.Pos) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException)

Example 43 with Pos

use of edu.mit.csail.sdg.alloy4.Pos 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 44 with Pos

use of edu.mit.csail.sdg.alloy4.Pos in project org.alloytools.alloy by AlloyTools.

the class CUP$CompParser$actions method syntax_error.

@Override
public void syntax_error(Symbol x) throws Err {
    Map<Integer, String> ch = new LinkedHashMap<Integer, String>();
    ch.put(CompSym.ARROW, "->");
    ch.put(CompSym.ANY_ARROW_SOME, "->");
    ch.put(CompSym.ANY_ARROW_ONE, "->");
    ch.put(CompSym.ANY_ARROW_LONE, "->");
    ch.put(CompSym.SOME_ARROW_ANY, "some");
    ch.put(CompSym.SOME_ARROW_SOME, "some");
    ch.put(CompSym.SOME_ARROW_ONE, "some");
    ch.put(CompSym.SOME_ARROW_LONE, "some");
    ch.put(CompSym.ONE_ARROW_ANY, "one");
    ch.put(CompSym.ONE_ARROW_SOME, "one");
    ch.put(CompSym.ONE_ARROW_ONE, "one");
    ch.put(CompSym.ONE_ARROW_LONE, "one");
    ch.put(CompSym.LONE_ARROW_ANY, "lone");
    ch.put(CompSym.LONE_ARROW_SOME, "lone");
    ch.put(CompSym.LONE_ARROW_ONE, "lone");
    ch.put(CompSym.LONE_ARROW_LONE, "lone");
    ch.put(CompSym.INTADD, "fun");
    ch.put(CompSym.INTSUB, "fun");
    ch.put(CompSym.INTMUL, "fun");
    ch.put(CompSym.INTDIV, "fun");
    ch.put(CompSym.INTREM, "fun");
    ch.put(CompSym.INTMIN, "fun");
    ch.put(CompSym.INTMAX, "fun");
    ch.put(CompSym.INTNEXT, "fun");
    ch.put(CompSym.TOTALORDER, "pred");
    ch.put(CompSym.ABSTRACT, "abstract");
    ch.put(CompSym.ALL, "all");
    ch.put(CompSym.ALL2, "all");
    ch.put(CompSym.AMPERSAND, "&");
    ch.put(CompSym.AND, "&&");
    ch.put(CompSym.AS, "as");
    ch.put(CompSym.ASSERT, "assert");
    ch.put(CompSym.AT, "@");
    ch.put(CompSym.BAR, "|");
    ch.put(CompSym.BUT, "but");
    ch.put(CompSym.CARET, "^");
    ch.put(CompSym.CHECK, "check");
    ch.put(CompSym.COLON, ":");
    ch.put(CompSym.COMMA, ", ");
    ch.put(CompSym.DISJ, "disj");
    ch.put(CompSym.DOMAIN, "<:");
    ch.put(CompSym.DOT, ".");
    ch.put(CompSym.ELSE, "else");
    ch.put(CompSym.ENUM, "enum");
    ch.put(CompSym.EQUALS, "=");
    ch.put(CompSym.EXACTLY, "exactly");
    ch.put(CompSym.EXH, "exh");
    ch.put(CompSym.EXPECT, "expect");
    ch.put(CompSym.EXTENDS, "extends");
    ch.put(CompSym.FACT, "fact");
    ch.put(CompSym.FOR, "for");
    ch.put(CompSym.FUN, "fun");
    ch.put(CompSym.GT, ">");
    ch.put(CompSym.GTE, ">=");
    ch.put(CompSym.HASH, "#");
    ch.put(CompSym.IDEN, "iden");
    ch.put(CompSym.IFF, "iff");
    ch.put(CompSym.IMPLIES, "=>");
    ch.put(CompSym.IN, "in");
    ch.put(CompSym.INT, "int");
    ch.put(CompSym.LBRACE, "{");
    ch.put(CompSym.LBRACKET, "[");
    ch.put(CompSym.LET, "let");
    ch.put(CompSym.LONE2, "lone");
    ch.put(CompSym.LONE, "lone");
    ch.put(CompSym.LPAREN, "(");
    ch.put(CompSym.LT, "<");
    ch.put(CompSym.LTE, "<=");
    ch.put(CompSym.MINUS, "-");
    ch.put(CompSym.MODULE, "module");
    ch.put(CompSym.NO2, "no");
    ch.put(CompSym.NO, "no");
    ch.put(CompSym.NONE, "none");
    ch.put(CompSym.NOT, "!");
    ch.put(CompSym.NOTEQUALS, "!");
    ch.put(CompSym.NOTGT, "!");
    ch.put(CompSym.NOTGTE, "!");
    ch.put(CompSym.NOTIN, "!");
    ch.put(CompSym.NOTLT, "!");
    ch.put(CompSym.NOTLTE, "!");
    ch.put(CompSym.ONE2, "one");
    ch.put(CompSym.ONE, "one");
    ch.put(CompSym.OPEN, "open");
    ch.put(CompSym.OR, "||");
    ch.put(CompSym.PART, "part");
    ch.put(CompSym.PLUS, "+");
    ch.put(CompSym.PLUSPLUS, "++");
    ch.put(CompSym.PRED, "pred");
    ch.put(CompSym.PRIVATE, "private");
    ch.put(CompSym.RANGE, ":>");
    ch.put(CompSym.RBRACE, "}");
    ch.put(CompSym.RBRACKET, "]");
    ch.put(CompSym.RPAREN, ")");
    ch.put(CompSym.RUN, "run");
    ch.put(CompSym.SEQ, "seq");
    ch.put(CompSym.SET, "set");
    ch.put(CompSym.SHL, "<<");
    ch.put(CompSym.SHR, ">>>");
    ch.put(CompSym.SHA, ">>");
    ch.put(CompSym.SIG, "sig");
    ch.put(CompSym.SIGINT, "Int");
    ch.put(CompSym.SLASH, "/");
    ch.put(CompSym.SOME2, "some");
    ch.put(CompSym.SOME, "some");
    ch.put(CompSym.STAR, "*");
    ch.put(CompSym.STRING, "String");
    ch.put(CompSym.SUM2, "sum");
    ch.put(CompSym.SUM, "sum");
    ch.put(CompSym.THIS, "this");
    ch.put(CompSym.TILDE, "~");
    ch.put(CompSym.UNIV, "univ");
    ch.put(CompSym.ID, "NAME");
    ch.put(CompSym.NUMBER, "NUMBER");
    ch.put(CompSym.STR, "STRING");
    TreeSet<String> list = new TreeSet<String>();
    Pos p = Pos.UNKNOWN;
    if (x != null && x.value instanceof Pos)
        p = (Pos) (x.value);
    else if (x != null && x.value instanceof Expr)
        p = ((Expr) (x.value)).pos;
    else if (x != null)
        p = x.pos;
    if (!stack.empty())
        for (Map.Entry<Integer, String> e : ch.entrySet()) {
            int key = e.getKey(), act = get_action(((Symbol) stack.peek()).parse_state, key);
            if (act == 0)
                continue;
            try {
                if (act > 0 || alloy_confirm(key))
                    list.add(e.getValue());
            } catch (Throwable ex) {
            // If the parser is really really confused, alloy_confirm() could fail with array out-of-bound exception, etc.
            }
        }
    String result = "There are " + list.size() + " possible tokens that can appear here:\n";
    for (String item : list) result = result + item + " ";
    throw new ErrorSyntax(p, (list.size() != 0) ? result : "");
}
Also used : ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos) TreeSet(java.util.TreeSet) Symbol(java_cup.runtime.Symbol) LinkedHashMap(java.util.LinkedHashMap)

Example 45 with Pos

use of edu.mit.csail.sdg.alloy4.Pos in project org.alloytools.alloy by AlloyTools.

the class CompUtil method parseEverything_fromFile.

// =============================================================================================================//
/**
 * Read everything from "file" and parse it; if it mentions submodules, open
 * them and parse them too.
 *
 * @param rep - if nonnull, we will report compilation progress messages to it
 * @param loaded - a cache of files that have been pre-fetched (can be null if
 *            there were no prefetching)
 * @param filename - the main module we are parsing
 * @return the root Module which contains pointers to all submodules
 * @throws Err if an error occurred
 *             <p>
 *             And if loaded!=null, it will contain all the files needed for
 *             this parse, and furthermore, other entries will be deleted.
 */
public static CompModule parseEverything_fromFile(A4Reporter rep, Map<String, String> loaded, String filename) throws Err {
    try {
        filename = Util.canon(filename);
        Set<String> thispath = new LinkedHashSet<String>();
        if (loaded == null)
            loaded = new LinkedHashMap<String, String>();
        Map<String, String> fc = new LinkedHashMap<String, String>(loaded);
        loaded.clear();
        List<Object> seenDollar = new ArrayList<Object>();
        CompModule root = parseRecursively(seenDollar, loaded, fc, new Pos(filename, 1, 1), filename, null, "", thispath, 1);
        root.seenDollar = seenDollar.size() > 0;
        return CompModule.resolveAll(rep == null ? A4Reporter.NOP : rep, root);
    } catch (FileNotFoundException ex) {
        throw new ErrorSyntax("File cannot be found.\n" + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new ErrorFatal("IOException occurred: " + ex.getMessage(), ex);
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        else
            throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Err(edu.mit.csail.sdg.alloy4.Err) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Pos(edu.mit.csail.sdg.alloy4.Pos)

Aggregations

Pos (edu.mit.csail.sdg.alloy4.Pos)28 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)24 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)16 ArrayList (java.util.ArrayList)12 Expr (edu.mit.csail.sdg.ast.Expr)10 Sig (edu.mit.csail.sdg.ast.Sig)10 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)10 Err (edu.mit.csail.sdg.alloy4.Err)9 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)8 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)7 LinkedHashMap (java.util.LinkedHashMap)7 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Command (edu.mit.csail.sdg.ast.Command)5 ExprVar (edu.mit.csail.sdg.ast.ExprVar)5 Func (edu.mit.csail.sdg.ast.Func)5 IOException (java.io.IOException)5 Symbol (java_cup.runtime.Symbol)5 Field (edu.mit.csail.sdg.ast.Sig.Field)4 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)3 HashMap (java.util.HashMap)3