Search in sources :

Example 11 with Command

use of edu.mit.csail.sdg.ast.Command 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 12 with Command

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

the class CompModule method getSubnodes.

/**
 * {@inheritDoc}
 */
@Override
public List<? extends Browsable> getSubnodes() {
    ArrayList<Browsable> ans = new ArrayList<Browsable>();
    ArrayList<Browsable> x;
    if (opens.size() > 0) {
        x = new ArrayList<Browsable>(opens.size());
        for (Open e : opens.values()) if (!x.contains(e.realModule))
            x.add(e.realModule);
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " opens</b>" : " open</b>"), x));
    }
    if (sigs.size() > 0) {
        x = new ArrayList<Browsable>(sigs.values());
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " sigs</b>" : " sig</b>"), x));
    }
    if (funcs.size() > 0) {
        ArrayList<Browsable> x2 = new ArrayList<Browsable>(funcs.size());
        x = new ArrayList<Browsable>(funcs.size());
        for (ArrayList<Func> e : funcs.values()) for (Func f : e) if (f.isPred)
            x.add(f);
        else
            x2.add(f);
        if (x.size() > 0)
            ans.add(make("<b>" + x.size() + (x.size() > 1 ? " preds</b>" : " pred</b>"), x));
        if (x2.size() > 0)
            ans.add(make("<b>" + x2.size() + (x2.size() > 1 ? " funs</b>" : " fun</b>"), x2));
    }
    if (commands.size() > 0) {
        ArrayList<Browsable> x2 = new ArrayList<Browsable>(commands.size());
        x = new ArrayList<Browsable>(commands.size());
        for (Command e : commands) if (e.check)
            x.add(e);
        else
            x2.add(e);
        if (x.size() > 0)
            ans.add(make("<b>" + x.size() + (x.size() > 1 ? " checks</b>" : " check</b>"), x));
        if (x2.size() > 0)
            ans.add(make("<b>" + x2.size() + (x2.size() > 1 ? " runs</b>" : " run</b>"), x2));
    }
    if (facts.size() > 0) {
        x = new ArrayList<Browsable>(facts.size());
        for (Pair<String, Expr> e : facts) x.add(make(e.b.span(), e.b.span(), "<b>fact " + e.a + "</b>", e.b));
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " facts</b>" : " fact</b>"), x));
    }
    if (asserts.size() > 0) {
        x = new ArrayList<Browsable>(asserts.size());
        for (Map.Entry<String, Expr> e : asserts.entrySet()) {
            Pos sp = e.getValue().span();
            x.add(make(sp, sp, "<b>assert</b> " + e.getKey(), e.getValue()));
        }
        ans.add(make("<b>" + x.size() + (x.size() > 1 ? " asserts</b>" : " assert</b>"), x));
    }
    return ans;
}
Also used : Browsable(edu.mit.csail.sdg.ast.Browsable) Func(edu.mit.csail.sdg.ast.Func) ArrayList(java.util.ArrayList) Expr(edu.mit.csail.sdg.ast.Expr) Command(edu.mit.csail.sdg.ast.Command) Pos(edu.mit.csail.sdg.alloy4.Pos) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with Command

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

the class CompModule method resolveCommands.

/**
 * Each command now points to a typechecked Expr.
 */
private void resolveCommands(Expr globalFacts) throws Err {
    ConstList<Sig> exactSigs = ConstList.make(world.exactSigs);
    for (int i = 0; i < commands.size(); i++) {
        Command cmd = commands.get(i);
        cmd = resolveCommand(cmd, exactSigs, globalFacts);
        commands.set(i, cmd);
    }
}
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) Command(edu.mit.csail.sdg.ast.Command)

Example 14 with Command

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

the class CompModule method addDefaultCommand.

public void addDefaultCommand() {
    if (commands.isEmpty()) {
        addFunc(Pos.UNKNOWN, Pos.UNKNOWN, "$$Default", null, new ArrayList<Decl>(), null, ExprConstant.TRUE);
        commands.add(new Command(Pos.UNKNOWN, ExprConstant.TRUE, "Default", false, 4, 4, 4, 0, null, null, ExprVar.make(null, "$$Default"), null));
    }
}
Also used : Command(edu.mit.csail.sdg.ast.Command) Decl(edu.mit.csail.sdg.ast.Decl)

Example 15 with Command

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

the class TranslateAlloyToKodkod method execute_greedyCommand.

private static A4Solution execute_greedyCommand(A4Reporter rep, Iterable<Sig> sigs, Command usercommand, A4Options opt) throws Exception {
    // FIXTHIS: if the next command has a "smaller scope" than the last
    // command, we would get a Kodkod exception...
    // FIXTHIS: if the solver is "toCNF" or "toKodkod" then this method will
    // throw an Exception...
    // FIXTHIS: does solution enumeration still work when we're doing a
    // greedy solve?
    TranslateAlloyToKodkod tr = null;
    try {
        long start = System.currentTimeMillis();
        GreedySimulator sim = new GreedySimulator();
        sim.allSigs = sigs;
        sim.partial = null;
        A4Reporter rep2 = new A4Reporter(rep) {

            private boolean first = true;

            @Override
            public void translate(String solver, int bitwidth, int maxseq, int skolemDepth, int symmetry) {
                if (first)
                    super.translate(solver, bitwidth, maxseq, skolemDepth, symmetry);
                first = false;
            }

            @Override
            public void resultSAT(Object command, long solvingTime, Object solution) {
            }

            @Override
            public void resultUNSAT(Object command, long solvingTime, Object solution) {
            }
        };
        // Form the list of commands
        List<Command> commands = new ArrayList<Command>();
        while (usercommand != null) {
            commands.add(usercommand);
            usercommand = usercommand.parent;
        }
        // For each command...
        A4Solution sol = null;
        for (int i = commands.size() - 1; i >= 0; i--) {
            Command cmd = commands.get(i);
            sim.growableSigs = cmd.getGrowableSigs();
            while (cmd != null) {
                rep.debug(cmd.scope.toString());
                usercommand = cmd;
                tr = new TranslateAlloyToKodkod(rep2, opt, sigs, cmd);
                tr.makeFacts(cmd.formula);
                sim.totalOrderPredicates = tr.totalOrderPredicates;
                sol = tr.frame.solve(rep2, cmd, sim.partial == null || cmd.check ? new Simplifier() : sim, false);
                if (!sol.satisfiable() && !cmd.check) {
                    start = System.currentTimeMillis() - start;
                    if (sim.partial == null) {
                        rep.resultUNSAT(cmd, start, sol);
                        return sol;
                    } else {
                        rep.resultSAT(cmd, start, sim.partial);
                        return sim.partial;
                    }
                }
                if (sol.satisfiable() && cmd.check) {
                    start = System.currentTimeMillis() - start;
                    rep.resultSAT(cmd, start, sol);
                    return sol;
                }
                sim.partial = sol;
                if (sim.growableSigs.isEmpty())
                    break;
                for (Sig s : sim.growableSigs) {
                    CommandScope sc = cmd.getScope(s);
                    if (sc.increment > sc.endingScope - sc.startingScope) {
                        cmd = null;
                        break;
                    }
                    cmd = cmd.change(s, sc.isExact, sc.startingScope + sc.increment, sc.endingScope, sc.increment);
                }
            }
        }
        if (sol.satisfiable())
            rep.resultSAT(usercommand, System.currentTimeMillis() - start, sol);
        else
            rep.resultUNSAT(usercommand, System.currentTimeMillis() - start, sol);
        return sol;
    } catch (CapacityExceededException ex) {
        throw rethrow(ex);
    } catch (HigherOrderDeclException ex) {
        Pos p = tr != null ? tr.frame.kv2typepos(ex.decl().variable()).b : Pos.UNKNOWN;
        throw new ErrorType(p, "Analysis cannot be performed since it requires higher-order quantification that could not be skolemized.");
    }
}
Also used : A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) ArrayList(java.util.ArrayList) Sig(edu.mit.csail.sdg.ast.Sig) CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Command(edu.mit.csail.sdg.ast.Command) Pos(edu.mit.csail.sdg.alloy4.Pos) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException) CommandScope(edu.mit.csail.sdg.ast.CommandScope)

Aggregations

Command (edu.mit.csail.sdg.ast.Command)21 A4Solution (edu.mit.csail.sdg.translator.A4Solution)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)5 Expr (edu.mit.csail.sdg.ast.Expr)5 Func (edu.mit.csail.sdg.ast.Func)4 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)3 Pos (edu.mit.csail.sdg.alloy4.Pos)3 Decl (edu.mit.csail.sdg.ast.Decl)3 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)3 CompModule (edu.mit.csail.sdg.parser.CompModule)3 ArrayList (java.util.ArrayList)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 CommandScope (edu.mit.csail.sdg.ast.CommandScope)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 Test (org.junit.Test)2