Search in sources :

Example 6 with A4Solution

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

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

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

Example 9 with A4Solution

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

the class AlloyModelsTest method testRecursion.

@Test
public void testRecursion() throws Exception {
    String filename = "src/test/resources/test-recursion.als";
    Module world = CompUtil.parseEverything_fromFile(A4Reporter.NOP, null, filename);
    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 : Command(edu.mit.csail.sdg.ast.Command) A4Options(edu.mit.csail.sdg.translator.A4Options) Module(edu.mit.csail.sdg.ast.Module) CompModule(edu.mit.csail.sdg.parser.CompModule) A4Solution(edu.mit.csail.sdg.translator.A4Solution) Test(org.junit.Test)

Example 10 with A4Solution

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

the class InternalTest method main2.

/**
 * Displays the amount of memory taken per solution enumeration.
 */
public static void main2(String[] args) throws Exception {
    String filename = "models/examples/algorithms/dijkstra.als";
    Module world = CompUtil.parseEverything_fromFile(A4Reporter.NOP, null, filename);
    A4Options options = new A4Options();
    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.gc();
            System.gc();
            System.gc();
            Thread.sleep(500);
            System.gc();
            System.gc();
            System.gc();
            Thread.sleep(500);
            long t = Runtime.getRuntime().totalMemory(), f = Runtime.getRuntime().freeMemory(), m = Runtime.getRuntime().maxMemory();
            System.out.println(hc + " total=" + t + " free=" + f + " max=" + m);
            System.out.flush();
            ans = ans.next();
        }
        return;
    }
}
Also used : Command(edu.mit.csail.sdg.ast.Command) A4Options(edu.mit.csail.sdg.translator.A4Options) Module(edu.mit.csail.sdg.ast.Module) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Aggregations

A4Solution (edu.mit.csail.sdg.translator.A4Solution)15 Command (edu.mit.csail.sdg.ast.Command)12 A4Options (edu.mit.csail.sdg.translator.A4Options)10 Module (edu.mit.csail.sdg.ast.Module)6 Sig (edu.mit.csail.sdg.ast.Sig)6 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)4 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)4 Expr (edu.mit.csail.sdg.ast.Expr)3 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 CompModule (edu.mit.csail.sdg.parser.CompModule)3 StringReader (java.io.StringReader)3 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)2 SafeList (edu.mit.csail.sdg.alloy4.SafeList)2 Decl (edu.mit.csail.sdg.ast.Decl)2 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)2 Func (edu.mit.csail.sdg.ast.Func)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)2 Test (org.junit.Test)2 Err (edu.mit.csail.sdg.alloy4.Err)1