Search in sources :

Example 6 with XMLNode

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

the class EvaluatorExample method main.

public static void main(String[] args) throws Exception {
    A4Reporter rep = new A4Reporter();
    File tmpAls = CompUtil.flushModelToFile(model, null);
    {
        Module world = CompUtil.parseEverything_fromString(rep, model);
        A4Options opt = new A4Options();
        opt.originalFilename = tmpAls.getAbsolutePath();
        opt.solver = A4Options.SatSolver.SAT4J;
        Command cmd = world.getAllCommands().get(0);
        A4Solution sol = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), cmd, opt);
        assert sol.satisfiable();
        sol.writeXML(outputfilename);
        // eval with existing A4Solution
        Expr e = CompUtil.parseOneExpression_fromString(world, "univ");
        System.out.println(sol.eval(e));
        e = CompUtil.parseOneExpression_fromString(world, "Point");
        System.out.println(sol.eval(e));
    }
    // reload everything from files
    {
        XMLNode xmlNode = new XMLNode(new File(outputfilename));
        String alloySourceFilename = xmlNode.iterator().next().getAttribute("filename");
        Module ansWorld = CompUtil.parseEverything_fromFile(rep, null, alloySourceFilename);
        A4Solution ans = A4SolutionReader.read(ansWorld.getAllReachableSigs(), xmlNode);
        Expr e = CompUtil.parseOneExpression_fromString(ansWorld, "univ");
        System.out.println(ans.eval(e));
        e = CompUtil.parseOneExpression_fromString(ansWorld, "Point");
        System.out.println(ans.eval(e));
    }
}
Also used : Expr(edu.mit.csail.sdg.ast.Expr) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Command(edu.mit.csail.sdg.ast.Command) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Options(edu.mit.csail.sdg.translator.A4Options) Module(edu.mit.csail.sdg.ast.Module) File(java.io.File) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 7 with XMLNode

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

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

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

the class StaticThemeReaderWriter method parseAlloyRelation.

/*
     * ========================================================= ================
     * ===================
     */
/**
 * Return null if the element is malformed.
 */
private static AlloyRelation parseAlloyRelation(VizState now, XMLNode x) {
    /*
         * <relation name="name"> 2 or more <type name=".."/> </relation>
         */
    List<AlloyType> ans = new ArrayList<AlloyType>();
    if (!x.is("relation"))
        return null;
    String name = x.getAttribute("name");
    if (name.length() == 0)
        return null;
    for (XMLNode sub : x.getChildren("type")) {
        String typename = sub.getAttribute("name");
        if (typename.length() == 0)
            return null;
        AlloyType t = now.getCurrentModel().hasType(typename);
        if (t == null)
            return null;
        ans.add(t);
    }
    if (ans.size() < 2)
        return null;
    else
        return now.getCurrentModel().hasRelation(name, ans);
}
Also used : XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) ArrayList(java.util.ArrayList)

Example 10 with XMLNode

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

the class SimpleReporter method validate.

/**
 * Validate the given filename to see if it is a valid Alloy XML instance file.
 */
private static void validate(String filename) throws Exception {
    A4SolutionReader.read(new ArrayList<Sig>(), new XMLNode(new File(filename))).toString();
    StaticInstanceReader.parseInstance(new File(filename));
}
Also used : XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)13 Expr (edu.mit.csail.sdg.ast.Expr)6 Sig (edu.mit.csail.sdg.ast.Sig)6 StringReader (java.io.StringReader)5 A4Solution (edu.mit.csail.sdg.translator.A4Solution)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)3 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)3 TupleSet (kodkod.instance.TupleSet)3 SafeList (edu.mit.csail.sdg.alloy4.SafeList)2 Command (edu.mit.csail.sdg.ast.Command)2 Module (edu.mit.csail.sdg.ast.Module)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 A4Options (edu.mit.csail.sdg.translator.A4Options)2 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1