Search in sources :

Example 11 with A4Reporter

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

the class CompModule method resolveParams.

/**
 * Every param in every module will now point to a nonnull Sig.
 */
private static void resolveParams(A4Reporter rep, List<CompModule> modules) throws Err {
    while (true) {
        boolean chg = false;
        Open missing = null;
        String missingName = "";
        for (CompModule mod : modules) for (Open open : mod.opens.values()) {
            CompModule sub = open.realModule;
            if (open.args.size() != sub.params.size())
                throw new ErrorSyntax(open.pos, "You supplied " + open.args.size() + " arguments to the open statement, but the imported module requires " + sub.params.size() + " arguments.");
            int i = 0;
            for (Map.Entry<String, Sig> p : sub.params.entrySet()) {
                Sig old = p.getValue();
                String kn = p.getKey(), vn = open.args.get(i);
                i++;
                Sig vv = mod.getRawSIG(open.pos, vn);
                if (vv == null) {
                    if (old == null) {
                        missing = open;
                        missingName = vn;
                    }
                    continue;
                }
                if (old == vv)
                    continue;
                if (old != null)
                    throw new ErrorFatal(open.pos, "Internal error (module re-instantiated with different arguments)");
                if (vv == NONE)
                    throw new ErrorSyntax(open.pos, "You cannot use \"none\" as an instantiating argument.");
                chg = true;
                p.setValue(vv);
                rep.parse("RESOLVE: " + (sub.path.length() == 0 ? "this/" : sub.path) + "/" + kn + " := " + vv + "\n");
            }
        }
        if (!chg && missing == null)
            return;
        if (!chg)
            throw new ErrorSyntax(missing.pos, "The signature name \"" + missingName + "\" cannot be found.");
    }
}
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) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal)

Example 12 with A4Reporter

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

the class TranslateAlloyToKodkod method execute_command.

/**
 * Based on the specified "options", execute one command and return the
 * resulting A4Solution object.
 *
 * @param rep - if nonnull, we'll send compilation diagnostic messages to it
 * @param sigs - the list of sigs; this list must be complete
 * @param cmd - the Command to execute
 * @param opt - the set of options guiding the execution of the command
 * @return null if the user chose "save to FILE" as the SAT solver, and nonnull
 *         if the solver finishes the entire solving and is either satisfiable
 *         or unsatisfiable.
 *         <p>
 *         If the return value X is satisfiable, you can call X.next() to get
 *         the next satisfying solution X2; and you can call X2.next() to get
 *         the next satisfying solution X3... until you get an unsatisfying
 *         solution.
 */
public static A4Solution execute_command(A4Reporter rep, Iterable<Sig> sigs, Command cmd, A4Options opt) throws Err {
    if (rep == null)
        rep = A4Reporter.NOP;
    TranslateAlloyToKodkod tr = null;
    try {
        if (cmd.parent != null || !cmd.getGrowableSigs().isEmpty())
            return execute_greedyCommand(rep, sigs, cmd, opt);
        tr = new TranslateAlloyToKodkod(rep, opt, sigs, cmd);
        tr.makeFacts(cmd.formula);
        return tr.frame.solve(rep, cmd, new Simplifier(), false);
    } catch (UnsatisfiedLinkError ex) {
        throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim(), ex);
    } 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.");
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        else
            throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
}
Also used : CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Err(edu.mit.csail.sdg.alloy4.Err) Pos(edu.mit.csail.sdg.alloy4.Pos) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException)

Example 13 with A4Reporter

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

Example 14 with A4Reporter

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

the class CompUtil method parseEverything_fromFile.

// =============================================================================================================//
/**
 * Read everything from "file" and parse it; if it mentions submodules, open
 * them and parse them too.
 *
 * @param rep - if nonnull, we will report compilation progress messages to it
 * @param loaded - a cache of files that have been pre-fetched (can be null if
 *            there were no prefetching)
 * @param filename - the main module we are parsing
 * @return the root Module which contains pointers to all submodules
 * @throws Err if an error occurred
 *             <p>
 *             And if loaded!=null, it will contain all the files needed for
 *             this parse, and furthermore, other entries will be deleted.
 */
public static CompModule parseEverything_fromFile(A4Reporter rep, Map<String, String> loaded, String filename) throws Err {
    try {
        filename = Util.canon(filename);
        Set<String> thispath = new LinkedHashSet<String>();
        if (loaded == null)
            loaded = new LinkedHashMap<String, String>();
        Map<String, String> fc = new LinkedHashMap<String, String>(loaded);
        loaded.clear();
        List<Object> seenDollar = new ArrayList<Object>();
        CompModule root = parseRecursively(seenDollar, loaded, fc, new Pos(filename, 1, 1), filename, null, "", thispath, 1);
        root.seenDollar = seenDollar.size() > 0;
        return CompModule.resolveAll(rep == null ? A4Reporter.NOP : rep, root);
    } catch (FileNotFoundException ex) {
        throw new ErrorSyntax("File cannot be found.\n" + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new ErrorFatal("IOException occurred: " + ex.getMessage(), ex);
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        else
            throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Err(edu.mit.csail.sdg.alloy4.Err) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Pos(edu.mit.csail.sdg.alloy4.Pos)

Example 15 with A4Reporter

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

the class AlloyTest method main.

public static void main(String[] args) throws Exception {
    A4Reporter rep = new A4Reporter();
    Module world = CompUtil.parseEverything_fromFile(rep, null, "/home/aleks/mvc.als");
    A4Options options = new A4Options();
    options.solver = A4Options.SatSolver.SAT4J;
    options.skolemDepth = 1;
    for (Command command : world.getAllCommands()) {
        A4Solution ans = null;
        try {
            ans = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), command, options);
            System.out.println(ans);
        } catch (Err ex) {
            Logger.getLogger(AlloyTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) 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) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Aggregations

Err (edu.mit.csail.sdg.alloy4.Err)9 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)6 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Pos (edu.mit.csail.sdg.alloy4.Pos)5 Command (edu.mit.csail.sdg.ast.Command)5 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)4 Expr (edu.mit.csail.sdg.ast.Expr)4 Sig (edu.mit.csail.sdg.ast.Sig)4 A4Options (edu.mit.csail.sdg.translator.A4Options)4 A4Solution (edu.mit.csail.sdg.translator.A4Solution)4 ArrayList (java.util.ArrayList)4 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)3 ErrorWarning (edu.mit.csail.sdg.alloy4.ErrorWarning)3 Decl (edu.mit.csail.sdg.ast.Decl)3 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)3 Module (edu.mit.csail.sdg.ast.Module)3 LinkedHashSet (java.util.LinkedHashSet)3 CapacityExceededException (kodkod.engine.CapacityExceededException)3 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)2 Func (edu.mit.csail.sdg.ast.Func)2