Search in sources :

Example 16 with ErrorFatal

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

the class StaticInstanceReader method sigMETA.

/**
 * Returns the AlloyType corresponding to the given sig; create an AlloyType for
 * it if none existed before.
 */
private AlloyType sigMETA(PrimSig s) throws Err {
    if (s == Sig.NONE)
        throw new ErrorFatal("Unexpected sig \"none\" encountered.");
    AlloyType type = sig2type.get(s);
    if (type != null)
        return type;
    if (s == Sig.UNIV)
        type = AlloyType.UNIV;
    else if (s == Sig.SIGINT)
        type = AlloyType.INT;
    else if (s == Sig.SEQIDX)
        type = AlloyType.SEQINT;
    else if (s == Sig.STRING)
        type = AlloyType.STRING;
    else
        type = makeType(s.label, s.isOne != null, s.isAbstract != null, false, s.isPrivate != null, s.isMeta != null, s.isEnum != null);
    sig2type.put(s, type);
    AlloyAtom atom = new AlloyAtom(type, (type == AlloyType.SEQINT ? Integer.MIN_VALUE : Integer.MAX_VALUE), s.label);
    atom2sets.put(atom, new LinkedHashSet<AlloySet>());
    sig2atom.put(s, atom);
    if (s.parent != Sig.UNIV && s.parent != null)
        ts.put(type, sigMETA(s.parent));
    if (s.parent != null)
        exts.add(new AlloyTuple(atom, sig2atom.get(s.parent)));
    Iterable<PrimSig> children = (s == Sig.UNIV ? toplevels : s.children());
    for (PrimSig sub : children) sigMETA(sub);
    return type;
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 17 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal 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 18 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal 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 19 with ErrorFatal

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprQt x) throws Err {
    Expr xx = x.desugar();
    if (xx instanceof ExprQt)
        x = (ExprQt) xx;
    else
        return visitThis(xx);
    if (x.op == ExprQt.Op.COMPREHENSION) {
        TempList<SimTuple> ans = new TempList<SimTuple>();
        enumerate(ans, 0, x, x.sub, 0);
        return SimTupleset.make(ans.makeConst());
    }
    if (x.op == ExprQt.Op.ALL)
        return enumerate(null, 0, x, x.sub.not(), 0) == 0;
    if (x.op == ExprQt.Op.NO)
        return enumerate(null, 0, x, x.sub, 0) == 0;
    if (x.op == ExprQt.Op.SOME)
        return enumerate(null, 0, x, x.sub, 0) >= 1;
    if (x.op == ExprQt.Op.LONE)
        return enumerate(null, 0, x, x.sub, 0) <= 1;
    if (x.op == ExprQt.Op.ONE)
        return enumerate(null, 0, x, x.sub, 0) == 1;
    if (x.op == ExprQt.Op.SUM)
        return trunc(enumerate(null, 0, x, x.sub, 0));
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprQt.accept()");
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) ExprQt(edu.mit.csail.sdg.ast.ExprQt)

Example 20 with ErrorFatal

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public SimTupleset visit(Field x) throws Err {
    if (x.defined) {
        final ExprVar v = (ExprVar) (x.sig.decl.get());
        final Expr b = x.decl().expr;
        final Env<ExprVar, Object> oldenv = env;
        env = new Env<ExprVar, Object>();
        if (!b.hasVar(v)) {
            SimTupleset ans = cset(x.sig).product(cset(b));
            env = oldenv;
            return ans;
        }
        SimTupleset ans = SimTupleset.EMPTY;
        for (SimTuple a : visit(x.sig)) {
            SimTupleset left = SimTupleset.make(a);
            env.put(v, left);
            SimTupleset right = cset(b);
            env.remove(v);
            ans = left.product(right).union(ans);
        }
        env = oldenv;
        return ans;
    }
    Object ans = sfs.get(x);
    if (ans instanceof SimTupleset)
        return (SimTupleset) ans;
    else
        throw new ErrorFatal("Unknown field " + x + " encountered during evaluation.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr)

Aggregations

ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)29 Err (edu.mit.csail.sdg.alloy4.Err)8 Expr (edu.mit.csail.sdg.ast.Expr)6 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)6 Pos (edu.mit.csail.sdg.alloy4.Pos)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)3 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)3 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 Sig (edu.mit.csail.sdg.ast.Sig)3 IOException (java.io.IOException)3 LinkedHashSet (java.util.LinkedHashSet)3 BinaryExpression (kodkod.ast.BinaryExpression)3 CapacityExceededException (kodkod.engine.CapacityExceededException)3 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)3 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)2