Search in sources :

Example 1 with A4Options

use of edu.mit.csail.sdg.translator.A4Options 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 2 with A4Options

use of edu.mit.csail.sdg.translator.A4Options in project org.alloytools.alloy by AlloyTools.

the class ExampleUsingTheAPI method main.

public static void main(String[] args) throws Err {
    // Chooses the Alloy4 options
    A4Options opt = new A4Options();
    opt.solver = A4Options.SatSolver.SAT4J;
    // abstract sig A {}
    PrimSig A = new PrimSig("A", Attr.ABSTRACT);
    // sig B {}
    PrimSig B = new PrimSig("B");
    // one sig A1 extends A {}
    PrimSig A1 = new PrimSig("A1", A, Attr.ONE);
    // one sig A2 extends A {}
    PrimSig A2 = new PrimSig("A2", A, Attr.ONE);
    // A { f: B lone->lone B }
    Expr f = A.addField("f", B.lone_arrow_lone(B));
    // Since (B lone->lone B) is not unary, the default is "setOf", meaning
    // "f:set (B lone->lone B)"
    // A { g: B }
    Expr g = A.addField("g", B);
    // The line above is the same as: A.addField(null, "g", B.oneOf()) since
    // B is unary.
    // If you want "setOf", you need: A.addField(null, "g", B.setOf())
    // pred someG { some g }
    Func someG = new Func(null, "SomeG", null, null, g.some());
    // pred atMostThree[x:univ, y:univ] { #(x+y) >= 3 }
    Decl x = UNIV.oneOf("x");
    Decl y = UNIV.oneOf("y");
    Expr body = x.get().plus(y.get()).cardinality().lte(ExprConstant.makeNUMBER(3));
    Func atMost3 = new Func(null, "atMost3", Util.asList(x, y), null, body);
    List<Sig> sigs = Arrays.asList(new Sig[] { A, B, A1, A2 });
    // run { some A && atMostThree[B,B] } for 3 but 3 int, 3 seq
    Expr expr1 = A.some().and(atMost3.call(B, B));
    Command cmd1 = new Command(false, 3, 3, 3, expr1);
    A4Solution sol1 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd1, opt);
    System.out.println("[Solution1]:");
    System.out.println(sol1.toString());
    // run { some f && SomeG[] } for 3 but 2 int, 1 seq, 5 A, exactly 6 B
    Expr expr2 = f.some().and(someG.call());
    Command cmd2 = new Command(false, 3, 2, 1, expr2);
    cmd2 = cmd2.change(A, false, 1);
    cmd2 = cmd2.change(B, true, 1);
    A4Solution sol2 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd2, opt);
    while (sol2.satisfiable()) {
        System.out.println("[Solution2]:");
        System.out.println(sol2.toString());
        sol2 = sol2.next();
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) Expr(edu.mit.csail.sdg.ast.Expr) Func(edu.mit.csail.sdg.ast.Func) Command(edu.mit.csail.sdg.ast.Command) A4Options(edu.mit.csail.sdg.translator.A4Options) Decl(edu.mit.csail.sdg.ast.Decl) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 3 with A4Options

use of edu.mit.csail.sdg.translator.A4Options in project org.alloytools.alloy by AlloyTools.

the class ExampleUsingTheCompiler method main.

/*
     * Execute every command in every file. This method parses every file, then
     * execute every command. If there are syntax or type errors, it may throw a
     * ErrorSyntax or ErrorType or ErrorAPI or ErrorFatal exception. You should
     * catch them and display them, and they may contain filename/line/column
     * information.
     */
public static void main(String[] args) throws Err {
    // The visualizer (We will initialize it to nonnull when we visualize an
    // Alloy solution)
    VizGUI viz = null;
    // Alloy4 sends diagnostic messages and progress reports to the
    // A4Reporter.
    // By default, the A4Reporter ignores all these events (but you can
    // extend the A4Reporter to display the event for the user)
    A4Reporter rep = new A4Reporter() {

        // For example, here we choose to display each "warning" by printing
        // it to System.out
        @Override
        public void warning(ErrorWarning msg) {
            System.out.print("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
            System.out.flush();
        }
    };
    for (String filename : args) {
        // Parse+typecheck the model
        System.out.println("=========== Parsing+Typechecking " + filename + " =============");
        Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
        // Choose some default options for how you want to execute the
        // commands
        A4Options options = new A4Options();
        options.solver = A4Options.SatSolver.SAT4J;
        for (Command command : world.getAllCommands()) {
            // Execute the command
            System.out.println("============ Command " + command + ": ============");
            A4Solution ans = TranslateAlloyToKodkod.execute_command(rep, world.getAllReachableSigs(), command, options);
            // Print the outcome
            System.out.println(ans);
            // If satisfiable...
            if (ans.satisfiable()) {
                // You can query "ans" to find out the values of each set or
                // type.
                // This can be useful for debugging.
                // 
                // You can also write the outcome to an XML file
                ans.writeXML("alloy_example_output.xml");
                // You can then visualize the XML file by calling this:
                if (viz == null) {
                    viz = new VizGUI(false, "alloy_example_output.xml", null);
                } else {
                    viz.loadXML("alloy_example_output.xml", true);
                }
            }
        }
    }
}
Also used : VizGUI(edu.mit.csail.sdg.alloy4viz.VizGUI) Command(edu.mit.csail.sdg.ast.Command) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Options(edu.mit.csail.sdg.translator.A4Options) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) Module(edu.mit.csail.sdg.ast.Module) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 4 with A4Options

use of edu.mit.csail.sdg.translator.A4Options 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 5 with A4Options

use of edu.mit.csail.sdg.translator.A4Options in project org.alloytools.alloy by AlloyTools.

the class AlloyModelsTest method minimalAlloyDoc.

@Test
public void minimalAlloyDoc() throws Exception {
    CompModule world = CompUtil.parseEverything_fromString(A4Reporter.NOP, "sig Foo  {} \n" + "run {  1 < #Int } for 10 int\n");
    A4Options options = new A4Options();
    options.unrolls = 10;
    for (Command command : world.getAllCommands()) {
        A4Solution ans = TranslateAlloyToKodkod.execute_command(A4Reporter.NOP, world.getAllReachableSigs(), command, options);
        while (ans.satisfiable()) {
            String hc = "Answer: " + ans.toString().hashCode();
            System.out.println(hc);
            ans = ans.next();
        }
        return;
    }
}
Also used : CompModule(edu.mit.csail.sdg.parser.CompModule) Command(edu.mit.csail.sdg.ast.Command) A4Options(edu.mit.csail.sdg.translator.A4Options) A4Solution(edu.mit.csail.sdg.translator.A4Solution) Test(org.junit.Test)

Aggregations

A4Options (edu.mit.csail.sdg.translator.A4Options)12 Command (edu.mit.csail.sdg.ast.Command)10 A4Solution (edu.mit.csail.sdg.translator.A4Solution)10 Module (edu.mit.csail.sdg.ast.Module)7 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)4 Expr (edu.mit.csail.sdg.ast.Expr)3 Sig (edu.mit.csail.sdg.ast.Sig)3 CompModule (edu.mit.csail.sdg.parser.CompModule)3 Err (edu.mit.csail.sdg.alloy4.Err)2 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)2 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)2 Decl (edu.mit.csail.sdg.ast.Decl)2 Func (edu.mit.csail.sdg.ast.Func)2 Test (org.junit.Test)2 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)1 VizGUI (edu.mit.csail.sdg.alloy4viz.VizGUI)1 SimpleCallback1 (edu.mit.csail.sdg.alloy4whole.SimpleReporter.SimpleCallback1)1 SimpleTask1 (edu.mit.csail.sdg.alloy4whole.SimpleReporter.SimpleTask1)1 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)1 Field (edu.mit.csail.sdg.ast.Sig.Field)1