Search in sources :

Example 21 with Sig

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

the class SimpleCLI method main.

public static void main(String[] args) throws Exception {
    final boolean sat4j = "yes".equals(System.getProperty("sat4j"));
    final boolean minisat = "yes".equals(System.getProperty("minisat"));
    SatSolver solver = A4Options.SatSolver.make("mem", "mem", "/zweb/sat/mem");
    final SimpleReporter rep = new SimpleReporter();
    final StringBuilder sb = rep.sb;
    for (String filename : args) {
        try {
            // Parse+Typecheck
            rep.sb.append("\n\nMain file = " + filename + "\n");
            if (db)
                db("Parsing+Typechecking...");
            Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
            if (db)
                db(" ok\n");
            List<Command> cmds = world.getAllCommands();
            for (ErrorWarning msg : rep.warnings) rep.sb.append("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
            rep.warnings.clear();
            // Do a detailed dump if we will not be executing the commands
            if (args.length != 1) {
                for (Module m : world.getAllReachableModules()) {
                    for (Sig x : m.getAllSigs()) {
                        sb.append("\nSig ").append(x.label).append(" at position ").append(x.pos).append("\n");
                        for (Decl d : x.getFieldDecls()) for (ExprHasName f : d.names) {
                            sb.append("\nField ").append(f.label).append(" with type ").append(f.type()).append("\n");
                            d.expr.toString(sb, 2);
                        }
                        rep.flush();
                    }
                    for (Func x : m.getAllFunc()) {
                        sb.append("\nFun/pred ").append(x.label).append(" at position ").append(x.pos).append("\n");
                        for (Decl d : x.decls) for (ExprHasName v : d.names) {
                            v.toString(sb, 2);
                            d.expr.toString(sb, 4);
                        }
                        x.returnDecl.toString(sb, 2);
                        x.getBody().toString(sb, 4);
                        rep.flush();
                    }
                    for (Pair<String, Expr> x : m.getAllFacts()) {
                        sb.append("\nFact ").append(x.a).append("\n");
                        x.b.toString(sb, 4);
                        rep.flush();
                    }
                    for (Pair<String, Expr> x : m.getAllAssertions()) {
                        sb.append("\nAssertion ").append(x.a).append("\n");
                        x.b.toString(sb, 4);
                        rep.flush();
                    }
                    if (m == world)
                        for (Command x : m.getAllCommands()) {
                            sb.append("\nCommand ").append(x.label).append("\n");
                            x.formula.toString(sb, 4);
                            rep.flush();
                        }
                }
                continue;
            }
            // Validate the metamodel generation code
            StringWriter metasb = new StringWriter();
            PrintWriter meta = new PrintWriter(metasb);
            Util.encodeXMLs(meta, "\n<alloy builddate=\"", Version.buildDate(), "\">\n\n");
            A4SolutionWriter.writeMetamodel(world.getAllReachableSigs(), filename, meta);
            Util.encodeXMLs(meta, "\n</alloy>");
            meta.flush();
            metasb.flush();
            String metaxml = metasb.toString();
            A4SolutionReader.read(new ArrayList<Sig>(), new XMLNode(new StringReader(metaxml)));
            StaticInstanceReader.parseInstance(new StringReader(metaxml));
            // Okay, now solve the commands
            A4Options options = new A4Options();
            options.originalFilename = filename;
            options.solverDirectory = "/zweb/zweb/tmp/alloy4/x86-freebsd";
            options.solver = sat4j ? A4Options.SatSolver.SAT4J : (minisat ? A4Options.SatSolver.MiniSatJNI : solver);
            for (int i = 0; i < cmds.size(); i++) {
                Command c = cmds.get(i);
                if (db) {
                    String cc = c.toString();
                    if (cc.length() > 60)
                        cc = cc.substring(0, 55);
                    db("Executing " + cc + "...\n");
                }
                rep.sb.append("Executing \"" + c + "\"\n");
                options.skolemDepth = 0;
                A4Solution s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
                if (s.satisfiable()) {
                    validate(s);
                    if (s.isIncremental()) {
                        s = s.next();
                        if (s.satisfiable())
                            validate(s);
                    }
                }
                options.skolemDepth = 2;
                s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
                if (s.satisfiable()) {
                    validate(s);
                    if (s.isIncremental()) {
                        s = s.next();
                        if (s.satisfiable())
                            validate(s);
                    }
                }
            }
        } catch (Throwable ex) {
            rep.sb.append("\n\nException: " + ex);
        }
        if (db) {
            if (args.length != 1)
                db(" ERROR!\n");
            else
                db("\n\n");
        }
    }
    rep.close();
}
Also used : XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Func(edu.mit.csail.sdg.ast.Func) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) Decl(edu.mit.csail.sdg.ast.Decl) Sig(edu.mit.csail.sdg.ast.Sig) Expr(edu.mit.csail.sdg.ast.Expr) StringWriter(java.io.StringWriter) Command(edu.mit.csail.sdg.ast.Command) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) A4Options(edu.mit.csail.sdg.translator.A4Options) StringReader(java.io.StringReader) Module(edu.mit.csail.sdg.ast.Module) SatSolver(edu.mit.csail.sdg.translator.A4Options.SatSolver) PrintWriter(java.io.PrintWriter) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 22 with Sig

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

the class VizTree method convertValueToText.

/**
 * {@inheritDoc}
 */
@Override
public String convertValueToText(Object val, boolean selected, boolean expanded, boolean leaf, int row, boolean focus) {
    String c = ">";
    if (onWindows)
        c = selected ? " style=\"color:#ffffff;\">" : " style=\"color:#000000;\">";
    if (val instanceof A4Solution)
        return "<html> <b" + c + encode(title == null ? "" : title) + "</b></html>";
    if (val instanceof Sig) {
        String label = ((Sig) val).label;
        if (label.startsWith("this/"))
            label = label.substring(5);
        return "<html> <b" + c + "sig " + encode(label) + "</b></html>";
    }
    if (val instanceof ExprVar)
        return "<html> <b" + c + "set " + encode(((ExprVar) val).label) + "</b></html>";
    if (val instanceof String)
        return "<html> <span" + c + encode((String) val) + "</span></html>";
    if (val instanceof Pair)
        return "<html> <b" + c + "field " + encode(((ExprHasName) (((Pair<?, ?>) val).b)).label) + "</b></html>";
    if (val instanceof A4Tuple) {
        StringBuilder sb = new StringBuilder("<html> <span" + c);
        A4Tuple tp = (A4Tuple) val;
        for (int i = 1; i < tp.arity(); i++) {
            if (i > 1)
                sb.append(" -> ");
            sb.append(encode(tp.atom(i)));
        }
        sb.append("</span></html>");
        return sb.toString();
    }
    return "";
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) A4Solution(edu.mit.csail.sdg.translator.A4Solution) Pair(edu.mit.csail.sdg.alloy4.Pair)

Example 23 with Sig

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

the class BookExamples method trial.

/**
 * If one of the solution is a solution to the given problem, return it, else
 * return null.
 */
static Solution trial(A4Reporter rep, A4Solution frame, Formula formula, Solver solver, boolean check) {
    TupleFactory fac = frame.getFactory();
    Solution sol = null;
    Iterable<Sig> sigs = frame.getAllReachableSigs();
    if (hasSig(sigs, "this/Book") != null) {
        Tuple B0N0A0 = t_tuple(fac, "Book$0", "Name$0", "Addr$0");
        Tuple B0N1A0 = t_tuple(fac, "Book$0", "Name$1", "Addr$0");
        Tuple B0N2A0 = t_tuple(fac, "Book$0", "Name$2", "Addr$0");
        Tuple B0N2A1 = t_tuple(fac, "Book$0", "Name$2", "Addr$1");
        Tuple B0N1A1 = t_tuple(fac, "Book$0", "Name$1", "Addr$1");
        Tuple B1N0A0 = t_tuple(fac, "Book$1", "Name$0", "Addr$0");
        Tuple B1N2A1 = t_tuple(fac, "Book$1", "Name$2", "Addr$1");
        Tuple B1N1A1 = t_tuple(fac, "Book$1", "Name$1", "Addr$1");
        Tuple B000 = t_tuple(fac, "Book$0", "Target$0", "Target$0");
        Tuple B001 = t_tuple(fac, "Book$0", "Target$0", "Target$1");
        Tuple B002 = t_tuple(fac, "Book$0", "Target$0", "Target$2");
        Tuple B010 = t_tuple(fac, "Book$0", "Target$1", "Target$0");
        Tuple B101 = t_tuple(fac, "Book$1", "Target$0", "Target$1");
        Tuple B110 = t_tuple(fac, "Book$1", "Target$1", "Target$0");
        Tuple B102 = t_tuple(fac, "Book$1", "Target$0", "Target$2");
        Tuple B210 = t_tuple(fac, "Book$2", "Target$1", "Target$0");
        Tuple B202 = t_tuple(fac, "Book$2", "Target$0", "Target$2");
        Tuple B212 = t_tuple(fac, "Book$2", "Target$1", "Target$2");
        Tuple B302 = t_tuple(fac, "Book$3", "Target$0", "Target$2");
        Tuple B310 = t_tuple(fac, "Book$3", "Target$1", "Target$0");
        Tuple B312 = t_tuple(fac, "Book$3", "Target$1", "Target$2");
        if (sol == null && B000 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.9", "Book$0", "", "this/Book", "", "Target$0", "", "this/Alias", "", "", "this/Group", "", "", "this/Addr", "", B000, "", "this/Book", "addr" });
        if (sol == null && B001 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.10", "Book$0", "", "this/Book", "", "", "this/Alias", "", "Target$0", "", "this/Group", "", "Target$1", "Target$2", "", "this/Addr", "", B001, B002, "", "this/Book", "addr" });
        if (sol == null && B001 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.11", "Book$0", "", "this/Book", "", "Target$0", "", "this/Alias", "", "", "this/Group", "", "Target$1", "Target$2", "", "this/Addr", "", B001, B002, "", "this/Book", "addr" });
        if (sol == null && B001 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.12", "Book$0", "", "this/Book", "", "Target$0", "", "this/Alias", "", "Target$1", "", "this/Group", "", "", "this/Addr", "", B001, "", "this/Book", "addr" });
        if (sol == null && B010 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.13", "Book$0", "Book$1", "", "this/Book", "", "", "this/Alias", "", "Target$0", "Target$1", "", "this/Group", "", "Target$2", "", "this/Addr", "", B010, B110, B102, "", "this/Book", "addr" });
        if (sol == null && B312 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.15", "Book$0", "Book$1", "Book$2", "Book$3", "", "this/Book", "", "", "this/Alias", "", "Target$0", "Target$1", "", "this/Group", "", "Target$2", "", "this/Addr", "", B102, B210, B202, B212, B302, B312, "", "this/Book", "addr" });
        if (sol == null && B101 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.16", "Book$0", "Book$1", "Book$2", "Book$3", "", "this/Book", "", "Target$1", "", "this/Alias", "", "Target$0", "", "this/Group", "", "", "this/Addr", "", B101, "", "this/Book", "addr" });
        if (sol == null && B102 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.17", "Book$0", "Book$1", "Book$2", "Book$3", "", "this/Book", "", "Target$0", "", "this/Alias", "", "Target$1", "", "this/Group", "", "Target$2", "", "this/Addr", "", B102, B210, B310, B302, "", "this/Book", "addr" });
        if (sol == null && B0N0A0 != null && check)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.6", "Book$0", "Book$1", "", "this/Book", "", "Addr$0", "", "this/Addr", "", "Name$0", "", "this/Name", "", B0N0A0, "", "this/Book", "addr" });
        if (sol == null && B1N0A0 != null && !check)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.4", "Book$0", "Book$1", "", "this/Book", "", "Addr$0", "", "this/Addr", "", "Name$0", "", "this/Name", "", B1N0A0, "", "this/Book", "addr" });
        if (sol == null && B0N2A1 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.5", "Book$0", "Book$1", "", "this/Book", "", "Addr$0", "Addr$1", "", "this/Addr", "", "Name$0", "Name$1", "Name$2", "", "this/Name", "", B0N2A1, B0N1A1, B1N2A1, B1N1A1, B1N0A0, "", "this/Book", "addr" });
        if (sol == null && B0N0A0 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.1", "Book$0", "", "this/Book", "", "Addr$0", "", "this/Addr", "", "Name$0", "", "this/Name", "", B0N0A0, "", "this/Book", "addr" });
        if (sol == null && B0N0A0 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.2", "Book$0", "", "this/Book", "", "Addr$0", "", "this/Addr", "", "Name$0", "Name$1", "Name$2", "", "this/Name", "", B0N0A0, B0N1A0, B0N2A0, "", "this/Book", "addr" });
        if (sol == null && B0N0A0 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 2.3", "Book$0", "", "this/Book", "", "Addr$0", "Addr$1", "", "this/Addr", "", "Name$0", "Name$1", "Name$2", "", "this/Name", "", B0N0A0, B0N1A0, B0N2A1, "", "this/Book", "addr" });
        if (sol == null && B001 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 5.2", "Book$0", "Book$1", "", "this/Book", "", "Target$0", "", "this/Name", "", "Target$1", "", "this/Addr", "", B001, B101, "", "this/Book", "addr" });
        if (sol == null && B102 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 5.3", "Book$0", "Book$1", "", "this/Book", "", "Target$0", "Target$1", "", "this/Name", "", "Target$2", "", "this/Addr", "", B010, B110, B102, "", "this/Book", "addr" });
    } else if (hasSig(sigs, "this/Woman") != null) {
        Tuple man0_woman0 = t_tuple(fac, "Person$1", "Person$0");
        Tuple man1_woman0 = t_tuple(fac, "Person$2", "Person$0");
        Tuple man0_woman1 = t_tuple(fac, "Person$1", "Person$3");
        Tuple man1_woman1 = t_tuple(fac, "Person$2", "Person$3");
        if (sol == null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 4.2", "Person$1", "", "this/Man", "", "Person$0", "", "this/Woman", "", man0_woman0, "", "this/Man", "wife", man0_woman0, "", "this/Person", "mother", "", "this/Person", "father" });
        if (sol == null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 4.3", "Person$1", "Person$2", "", "this/Man", "", "Person$0", "Person$3", "", "this/Woman", "", man1_woman0, man0_woman1, "", "this/Man", "wife", man1_woman1, man0_woman0, "", "this/Person", "mother", "", "this/Person", "father" });
    } else if (hasSig(sigs, "this/Process") != null) {
        String p0 = "Process$0", p1 = "Process$1", p2 = "Process$2";
        String t0 = "Time$0", t1 = "Time$1", t2 = "Time$2", t3 = "Time$3";
        Tuple s20 = t_tuple(fac, p2, p0), s01 = t_tuple(fac, p0, p1), s12 = t_tuple(fac, p1, p2);
        Tuple d000 = t_tuple(fac, p0, p0, t0), d110 = t_tuple(fac, p1, p1, t0), d220 = t_tuple(fac, p2, p2, t0);
        Tuple d001 = t_tuple(fac, p0, p0, t1), d021 = t_tuple(fac, p0, p2, t1), d111 = t_tuple(fac, p1, p1, t1);
        Tuple d002 = t_tuple(fac, p0, p0, t2), d112 = t_tuple(fac, p1, p1, t2), d122 = t_tuple(fac, p1, p2, t2);
        Tuple d003 = t_tuple(fac, p0, p0, t3), d113 = t_tuple(fac, p1, p1, t3), d223 = t_tuple(fac, p2, p2, t3);
        if (sol == null && d000 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 6.4", s20, s01, s12, "", "this/Process", "succ", d000, d110, d220, d001, d021, d111, d002, d112, d122, d003, d113, d223, "", "this/Process", "toSend", t_tuple(fac, p2, t3), "", "this/Process", "elected" });
    } else if (hasSig(sigs, "this/Desk") != null) {
        String g0 = "Guest$0", g1 = "Guest$1", r = "Room$0", k0 = "Key$0", k1 = "Key$1";
        String t0 = "Time$0", t1 = "Time$1", t2 = "Time$2", t3 = "Time$3", t4 = "Time$4", t5 = "Time$5";
        String c0 = "Card$0", c1 = "Card$1";
        if (sol == null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig E.3", t_tuple(fac, c0, k0), t_tuple(fac, c1, k1), "", "this/Card", "fst", t_tuple(fac, c0, k1), t_tuple(fac, c1, k0), "", "this/Card", "snd", t_tuple(fac, g0, c0, t1), t_tuple(fac, g0, c0, t2), t_tuple(fac, g1, c1, t2), t_tuple(fac, g0, c0, t3), t_tuple(fac, g1, c1, t3), t_tuple(fac, g0, c0, t4), t_tuple(fac, g1, c1, t4), t_tuple(fac, g0, c0, t5), t_tuple(fac, g1, c1, t5), "", "this/Guest", "cards", t_tuple(fac, r, k0, t0), t_tuple(fac, r, k0, t1), t_tuple(fac, r, k0, t2), t_tuple(fac, r, k1, t3), t_tuple(fac, r, k0, t4), t_tuple(fac, r, k1, t5), "", "this/Room", "key", t_tuple(fac, k1, t1), t_tuple(fac, k0, t2), t_tuple(fac, k1, t2), t_tuple(fac, k0, t3), t_tuple(fac, k1, t3), t_tuple(fac, k0, t4), t_tuple(fac, k1, t4), t_tuple(fac, k0, t5), t_tuple(fac, k1, t5), "", "this/Desk", "issued", t_tuple(fac, r, k0, t0), t_tuple(fac, r, k1, t1), t_tuple(fac, r, k0, t2), t_tuple(fac, r, k0, t3), t_tuple(fac, r, k0, t4), t_tuple(fac, r, k0, t5), "", "this/Desk", "prev" });
    } else if (hasSig(sigs, "this/FrontDesk") != null) {
        String g0 = "Guest$0", g1 = "Guest$1", r = "Room$0", k0 = "Key$0", k1 = "Key$1", k2 = "Key$2";
        String t0 = "Time$0", t1 = "Time$1", t2 = "Time$2", t3 = "Time$3", t4 = "Time$4";
        Tuple G0 = t_tuple(fac, g0), G1 = t_tuple(fac, g1);
        Tuple K0 = t_tuple(fac, r, k0), K1 = t_tuple(fac, r, k1), K2 = t_tuple(fac, r, k2);
        Tuple K0T0 = t_tuple(fac, r, k0, t0), K0T1 = t_tuple(fac, r, k0, t1), K0T2 = t_tuple(fac, r, k0, t2);
        Tuple K0T3 = t_tuple(fac, r, k0, t3), K1T4 = t_tuple(fac, r, k1, t4);
        Tuple F1 = t_tuple(fac, r, k0, t0), F2 = t_tuple(fac, r, k1, t1), F3 = t_tuple(fac, r, k1, t2);
        Tuple F4 = t_tuple(fac, r, k2, t3), F5 = t_tuple(fac, r, k2, t4);
        Tuple GK1 = t_tuple(fac, g0, k1, t1), GK2 = t_tuple(fac, g0, k1, t2), GK3 = t_tuple(fac, g0, k1, t3);
        Tuple GK4 = t_tuple(fac, g1, k2, t3), GK5 = t_tuple(fac, g0, k1, t4), GK6 = t_tuple(fac, g1, k2, t4);
        Tuple O1 = t_tuple(fac, r, g0, t1), O2 = t_tuple(fac, r, g1, t3), O3 = t_tuple(fac, r, g1, t4);
        if (sol == null && K0T0 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 6.13", G0, G1, "", "this/Guest", "", K0, K1, K2, "", "this/Room", "keys", K0T0, K0T1, K0T2, K0T3, K1T4, "", "this/Room", "currentKey", F1, F2, F3, F4, F5, "", "this/FrontDesk", "lastKey", GK1, GK2, GK3, GK4, GK5, GK6, "", "this/Guest", "keys", O1, O2, O3, "", "this/FrontDesk", "occupant", "Event$0", "Event$1", "", "this/Checkin", "", "Event$2", "", "this/Checkout", "", "Event$3", "", "this/Entry", "", t_tuple(fac, "Event$0", t0), t_tuple(fac, "Event$2", t1), t_tuple(fac, "Event$1", t2), t_tuple(fac, "Event$3", t3), "", "this/Event", "pre" });
        if (sol == null && K0T0 != null)
            sol = trial(rep, fac, solver, sigs, formula, frame, new Object[] { "Fig 6.6", G0, G1, "", "this/Guest", "", K0, K1, K2, "", "this/Room", "keys", K0T0, K0T1, K0T2, K0T3, K1T4, "", "this/Room", "currentKey", F1, F2, F3, F4, F5, "", "this/FrontDesk", "lastKey", GK1, GK2, GK3, GK4, GK5, GK6, "", "this/Guest", "keys", O1, O2, O3, "", "this/FrontDesk", "occupant" });
    }
    return sol;
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) TupleFactory(kodkod.instance.TupleFactory) Solution(kodkod.engine.Solution) Tuple(kodkod.instance.Tuple)

Example 24 with Sig

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

the class CompModule method getGlobalFromModule.

Pos getGlobalFromModule(CompModule module, String key) {
    Sig sig = module.sigs.get(key);
    if (sig != null)
        return sig.pos;
    ArrayList<Func> arrayList = module.funcs.get(key);
    if (arrayList == null || arrayList.isEmpty())
        return null;
    return arrayList.get(0).pos;
}
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) Func(edu.mit.csail.sdg.ast.Func)

Example 25 with Sig

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

the class CompModule method populate.

/**
 * Resolve the name based on the current context and this module.
 */
private Expr populate(TempList<Expr> ch, TempList<String> re, Decl rootfield, Sig rootsig, boolean rootfunparam, Func rootfunbody, Pos pos, String fullname, Expr THIS) {
    // Return object can be Func(with > 0 arguments) or Expr
    final String name = (fullname.charAt(0) == '@') ? fullname.substring(1) : fullname;
    boolean fun = (rootsig != null && (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF)) || (rootsig == null && !rootfunparam);
    if (name.equals("univ"))
        return ExprUnary.Op.NOOP.make(pos, UNIV);
    if (name.equals("Int"))
        return ExprUnary.Op.NOOP.make(pos, SIGINT);
    if (name.equals("seq/Int"))
        return ExprUnary.Op.NOOP.make(pos, SEQIDX);
    if (name.equals("String"))
        return ExprUnary.Op.NOOP.make(pos, STRING);
    if (name.equals("none"))
        return ExprUnary.Op.NOOP.make(pos, NONE);
    if (name.equals("iden"))
        return ExprConstant.Op.IDEN.make(pos, 0);
    if (name.equals("sig$") || name.equals("field$"))
        if (world != null) {
            Sig s = world.sigs.get(name);
            if (s != null)
                return ExprUnary.Op.NOOP.make(pos, s);
        }
    final List<Object> ans = name.indexOf('/') >= 0 ? getRawQS(fun ? 5 : 1, name) : getRawNQS(this, fun ? 5 : 1, name);
    final Sig param = params.get(name);
    if (param != null && !ans.contains(param))
        ans.add(param);
    for (Object x : ans) {
        if (x instanceof Sig) {
            Sig y = (Sig) x;
            ch.add(ExprUnary.Op.NOOP.make(pos, y, null, 0));
            re.add("sig " + y.label);
        } else if (x instanceof Func) {
            Func f = (Func) x;
            int fn = f.count();
            int penalty = 0;
            if (resolution == 1 && fn > 0 && rootsig != null && THIS != null && THIS.type().hasArity(1) && f.get(0).type().intersects(THIS.type())) {
                // If we're inside a sig, and there is a unary variable
                // bound to "this",
                // we should consider it as a possible FIRST ARGUMENT of a
                // fun/pred call
                ConstList<Expr> t = Util.asList(THIS);
                // penalty
                ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 1 + penalty) : ExprBadCall.make(pos, null, f, t, 1 + penalty));
                // of
                // 1
                re.add((f.isPred ? "pred this." : "fun this.") + f.label);
            }
            if (resolution == 1) {
                ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, penalty) : ExprBadCall.make(pos, null, f, null, penalty));
                re.add((f.isPred ? "pred " : "fun ") + f.label);
            }
            if (resolution == 2 && f != rootfunbody && THIS != null && fullname.charAt(0) != '@' && fn > 0 && f.get(0).type().intersects(THIS.type())) {
                // If there is some value bound to "this", we should
                // consider it as a possible FIRST ARGUMENT of a fun/pred
                // call
                ConstList<Expr> t = Util.asList(THIS);
                ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 0) : ExprBadCall.make(pos, null, f, t, 0));
                re.add((f.isPred ? "pred this." : "fun this.") + f.label);
            }
            if (resolution != 1) {
                ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, 0) : ExprBadCall.make(pos, null, f, null, 0));
                re.add((f.isPred ? "pred " : "fun ") + f.label);
            }
        }
    }
    // All else: we can call, and can refer to anything visible.
    for (CompModule m : getAllNameableModules()) for (Sig s : m.sigs.values()) if (m == this || s.isPrivate == null)
        for (Field f : s.getFields()) if (f.isMeta == null && (m == this || f.isPrivate == null) && f.label.equals(name))
            if (resolution == 1) {
                Expr x = null;
                if (rootsig == null) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                } else if (rootsig.isSameOrDescendentOf(f.sig)) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                    if (fullname.charAt(0) != '@')
                        x = THIS.join(x);
                } else if (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF) {
                    x = ExprUnary.Op.NOOP.make(pos, f, null, 1);
                }
                // penalty of 1
                if (x != null) {
                    ch.add(x);
                    re.add("field " + f.sig.label + " <: " + f.label);
                }
            } else if (rootfield == null || rootsig.isSameOrDescendentOf(f.sig)) {
                Expr x0 = ExprUnary.Op.NOOP.make(pos, f, null, 0);
                if (resolution == 2 && THIS != null && fullname.charAt(0) != '@' && f.type().firstColumnOverlaps(THIS.type())) {
                    ch.add(THIS.join(x0));
                    re.add("field " + f.sig.label + " <: this." + f.label);
                    if (rootsig != null)
                        continue;
                }
                ch.add(x0);
                re.add("field " + f.sig.label + " <: " + f.label);
            }
    if (metaSig() != null && (rootsig == null || rootfield == null)) {
        SafeList<PrimSig> children = null;
        try {
            children = metaSig().children();
        } catch (Err err) {
            return null;
        }
        // exception NOT possible
        for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
            Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
            ch.add(x);
            re.add("field " + f.sig.label + " <: " + f.label);
        }
    }
    if (metaField() != null && (rootsig == null || rootfield == null)) {
        SafeList<PrimSig> children = null;
        try {
            children = metaField().children();
        } catch (Err err) {
            return null;
        }
        // exception NOT possible
        for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
            Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
            ch.add(x);
            re.add("field " + f.sig.label + " <: " + f.label);
        }
    }
    return null;
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) Func(edu.mit.csail.sdg.ast.Func) 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) Expr(edu.mit.csail.sdg.ast.Expr) ConstList(edu.mit.csail.sdg.alloy4.ConstList) 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