Search in sources :

Example 1 with SatSolver

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

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

the class PreferencesDialog method testSolvers.

protected Iterable<SatSolver> testSolvers() {
    List<SatSolver> satChoices = SatSolver.values().makeCopy();
    satChoices.remove(SatSolver.BerkMinPIPE);
    String fs = System.getProperty("file.separator");
    String test2 = Subprocess.exec(20000, new String[] { binary + fs + "spear", "--model", "--dimacs", binary + fs + "tmp.cnf" });
    if (!isSat(test2))
        satChoices.remove(SatSolver.SpearPIPE);
    if (!loadLibrary("minisat")) {
        log.logBold("Warning: JNI-based SAT solver does not work on this platform.\n");
        log.log("This is okay, since you can still use SAT4J as the solver.\n" + "For more information, please visit http://alloy.mit.edu/alloy4/\n");
        log.logDivider();
        log.flush();
        satChoices.remove(SatSolver.MiniSatJNI);
    }
    if (!loadLibrary("minisatprover"))
        satChoices.remove(SatSolver.MiniSatProverJNI);
    if (!loadLibrary("lingeling"))
        satChoices.remove(SatSolver.LingelingJNI);
    if (!loadLibrary("glucose"))
        satChoices.remove(SatSolver.GlucoseJNI);
    if (!loadLibrary("cryptominisat"))
        satChoices.remove(SatSolver.CryptoMiniSatJNI);
    SatSolver now = Solver.get();
    if (!satChoices.contains(now)) {
        now = SatSolver.LingelingJNI;
        if (!satChoices.contains(now))
            now = SatSolver.SAT4J;
        Solver.set(now);
    }
    if (now == SatSolver.SAT4J && satChoices.size() > 3 && satChoices.contains(SatSolver.CNF) && satChoices.contains(SatSolver.KK)) {
        log.logBold("Warning: Alloy4 defaults to SAT4J since it is pure Java and very reliable.\n");
        log.log("For faster performance, go to Options menu and try another solver like MiniSat.\n");
        log.log("If these native solvers fail on your computer, remember to change back to SAT4J.\n");
        log.logDivider();
        log.flush();
    }
    return satChoices;
}
Also used : SatSolver(edu.mit.csail.sdg.translator.A4Options.SatSolver)

Aggregations

SatSolver (edu.mit.csail.sdg.translator.A4Options.SatSolver)2 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)1 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)1 Command (edu.mit.csail.sdg.ast.Command)1 Decl (edu.mit.csail.sdg.ast.Decl)1 Expr (edu.mit.csail.sdg.ast.Expr)1 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)1 Func (edu.mit.csail.sdg.ast.Func)1 Module (edu.mit.csail.sdg.ast.Module)1 Sig (edu.mit.csail.sdg.ast.Sig)1 A4Options (edu.mit.csail.sdg.translator.A4Options)1 A4Solution (edu.mit.csail.sdg.translator.A4Solution)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1