Search in sources :

Example 81 with Err

use of edu.mit.csail.sdg.alloy4.Err 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)

Example 82 with Err

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

the class StaticInstanceReader method sig.

/**
 * Returns the AlloyType corresponding to the given sig; create an AlloyType for
 * it if none existed before.
 */
private AlloyType sig(PrimSig s) throws Err {
    if (s == Sig.NONE)
        throw new ErrorFatal("Unexpected sig \"none\" encountered.");
    AlloyType ans = sig2type.get(s);
    if (ans == null) {
        ans = makeType(s.label, s.isOne != null, s.isAbstract != null, false, s.isPrivate != null, s.isMeta != null, s.isEnum != null);
        sig2type.put(s, ans);
        if (s.parent != Sig.UNIV)
            ts.put(ans, sig(s.parent));
    }
    return ans;
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal)

Example 83 with Err

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

the class SimpleGUI method doShowParseTree.

/**
 * This method displays the parse tree.
 */
private Runner doShowParseTree() {
    if (wrap)
        return wrapMe();
    doRefreshRun();
    OurUtil.enableAll(runmenu);
    if (commands != null) {
        Module world = null;
        try {
            int resolutionMode = (Version.experimental && ImplicitThis.get()) ? 2 : 1;
            A4Options opt = new A4Options();
            opt.tempDirectory = alloyHome() + fs + "tmp";
            opt.solverDirectory = alloyHome() + fs + "binary";
            opt.originalFilename = Util.canon(text.get().getFilename());
            world = CompUtil.parseEverything_fromFile(A4Reporter.NOP, text.takeSnapshot(), opt.originalFilename, resolutionMode);
        } catch (Err er) {
            text.shade(er.pos);
            log.logRed(er.toString() + "\n\n");
            return null;
        }
        world.showAsTree(this);
    }
    return null;
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) A4Options(edu.mit.csail.sdg.translator.A4Options) Module(edu.mit.csail.sdg.ast.Module)

Example 84 with Err

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

the class SimpleGUI method convert.

/**
 * Converts an A4TupleSet into a SimTupleset object.
 */
private static SimTupleset convert(Object object) throws Err {
    if (!(object instanceof A4TupleSet))
        throw new ErrorFatal("Unexpected type error: expecting an A4TupleSet.");
    A4TupleSet s = (A4TupleSet) object;
    if (s.size() == 0)
        return SimTupleset.EMPTY;
    List<SimTuple> list = new ArrayList<SimTuple>(s.size());
    int arity = s.arity();
    for (A4Tuple t : s) {
        String[] array = new String[arity];
        for (int i = 0; i < t.arity(); i++) array[i] = t.atom(i);
        list.add(SimTuple.make(array));
    }
    return SimTupleset.make(list);
}
Also used : A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) SimTuple(edu.mit.csail.sdg.sim.SimTuple) ArrayList(java.util.ArrayList) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet)

Example 85 with Err

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

the class SimpleGUI method doRefreshRun.

// ===============================================================================================================//
/**
 * This method refreshes the "run" menu.
 */
private Runner doRefreshRun() {
    if (wrap)
        return wrapMe();
    KeyStroke ac = KeyStroke.getKeyStroke(VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
    try {
        wrap = true;
        runmenu.removeAll();
        menuItem(runmenu, "Execute Latest Command", 'E', 'E', doExecuteLatest());
        runmenu.add(new JSeparator());
        menuItem(runmenu, "Show Latest Instance", 'L', 'L', doShowLatest(), latestInstance.length() > 0);
        menuItem(runmenu, "Show Metamodel", 'M', 'M', doShowMetaModel());
        if (Version.experimental)
            menuItem(runmenu, "Show Parse Tree", 'P', doShowParseTree());
        menuItem(runmenu, "Open Evaluator", 'V', doLoadEvaluator());
    } finally {
        wrap = false;
    }
    List<Command> cp = commands;
    if (cp == null) {
        try {
            String source = text.get().getText();
            cp = CompUtil.parseOneModule_fromString(source);
        } catch (Err e) {
            commands = null;
            runmenu.getItem(0).setEnabled(false);
            runmenu.getItem(3).setEnabled(false);
            text.shade(new Pos(text.get().getFilename(), e.pos.x, e.pos.y, e.pos.x2, e.pos.y2));
            if (AlloyCore.isDebug() && VerbosityPref.get() == Verbosity.FULLDEBUG)
                log.logRed("Fatal Exception!" + e.dump() + "\n\n");
            else
                log.logRed(e.toString() + "\n\n");
            return null;
        } catch (Throwable e) {
            commands = null;
            runmenu.getItem(0).setEnabled(false);
            runmenu.getItem(3).setEnabled(false);
            log.logRed("Cannot parse the model.\n" + e.toString() + "\n\n");
            return null;
        }
        commands = cp;
    }
    text.clearShade();
    // To clear any residual error message
    log.clearError();
    if (cp == null) {
        runmenu.getItem(0).setEnabled(false);
        runmenu.getItem(3).setEnabled(false);
        return null;
    }
    if (cp.size() == 0) {
        runmenu.getItem(0).setEnabled(false);
        return null;
    }
    if (latestCommand >= cp.size())
        latestCommand = cp.size() - 1;
    runmenu.remove(0);
    try {
        wrap = true;
        for (int i = 0; i < cp.size(); i++) {
            JMenuItem y = new JMenuItem(cp.get(i).toString(), null);
            y.addActionListener(doRun(i));
            if (i == latestCommand) {
                y.setMnemonic(VK_E);
                y.setAccelerator(ac);
            }
            runmenu.add(y, i);
        }
        if (cp.size() >= 2) {
            JMenuItem y = new JMenuItem("Execute All", null);
            y.setMnemonic(VK_A);
            y.addActionListener(doRun(-1));
            runmenu.add(y, 0);
            runmenu.add(new JSeparator(), 1);
        }
    } finally {
        wrap = false;
    }
    return null;
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) Command(edu.mit.csail.sdg.ast.Command) Pos(edu.mit.csail.sdg.alloy4.Pos) KeyStroke(javax.swing.KeyStroke) JMenuItem(javax.swing.JMenuItem) JSeparator(javax.swing.JSeparator)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)32 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)28 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)27 Err (edu.mit.csail.sdg.alloy4.Err)25 Expr (edu.mit.csail.sdg.ast.Expr)25 Sig (edu.mit.csail.sdg.ast.Sig)24 Pos (edu.mit.csail.sdg.alloy4.Pos)16 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)15 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)15 ArrayList (java.util.ArrayList)15 Field (edu.mit.csail.sdg.ast.Sig.Field)12 LinkedHashMap (java.util.LinkedHashMap)10 IOException (java.io.IOException)9 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)8 Func (edu.mit.csail.sdg.ast.Func)8 TupleSet (kodkod.instance.TupleSet)8 Command (edu.mit.csail.sdg.ast.Command)7 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)5