Search in sources :

Example 1 with A4TupleSet

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

the class StaticInstanceReader method atoms.

/**
 * Constructs the atoms corresponding to the given sig.
 */
private void atoms(A4Solution sol, PrimSig s) throws Err {
    Expr sum = Sig.NONE;
    for (PrimSig c : s.children()) {
        sum = sum.plus(c);
        atoms(sol, c);
    }
    // This ensures
    A4TupleSet ts = (A4TupleSet) (sol.eval(s.minus(sum)));
    // specific sig
    for (A4Tuple z : ts) {
        String atom = z.atom(0);
        int i, dollar = atom.lastIndexOf('$');
        try {
            i = Integer.parseInt(dollar >= 0 ? atom.substring(dollar + 1) : atom);
        } catch (NumberFormatException ex) {
            i = Integer.MAX_VALUE;
        }
        AlloyAtom at = new AlloyAtom(sig(s), ts.size() == 1 ? Integer.MAX_VALUE : i, atom);
        atom2sets.put(at, new LinkedHashSet<AlloySet>());
        string2atom.put(atom, at);
    }
}
Also used : A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) Expr(edu.mit.csail.sdg.ast.Expr) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 2 with A4TupleSet

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

the class StaticInstanceReader method setOrRel.

/**
 * Construct an AlloySet or AlloyRelation corresponding to the given expression.
 */
private void setOrRel(A4Solution sol, String label, Expr expr, boolean isPrivate, boolean isMeta) throws Err {
    for (List<PrimSig> ps : expr.type().fold()) {
        if (ps.size() == 1) {
            PrimSig t = ps.get(0);
            AlloySet set = makeSet(label, isPrivate, isMeta, sig(t));
            sets.add(set);
            for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(t)))) {
                atom2sets.get(string2atom.get(tp.atom(0))).add(set);
            }
        } else {
            Expr mask = null;
            List<AlloyType> types = new ArrayList<AlloyType>(ps.size());
            for (int i = 0; i < ps.size(); i++) {
                types.add(sig(ps.get(i)));
                if (mask == null)
                    mask = ps.get(i);
                else
                    mask = mask.product(ps.get(i));
            }
            AlloyRelation rel = makeRel(label, isPrivate, isMeta, types);
            Set<AlloyTuple> ts = new LinkedHashSet<AlloyTuple>();
            for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(mask)))) {
                AlloyAtom[] atoms = new AlloyAtom[tp.arity()];
                for (int i = 0; i < tp.arity(); i++) {
                    atoms[i] = string2atom.get(tp.atom(i));
                    if (atoms[i] == null)
                        throw new ErrorFatal("Unexpected XML inconsistency: cannot resolve atom " + tp.atom(i));
                }
                ts.add(new AlloyTuple(atoms));
            }
            rels.put(rel, ts);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 3 with A4TupleSet

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

the class SimpleGUI method convert.

/**
 * Converts an A4TupleSet into a SimTupleset object.
 */
private static SimTupleset convert(Object object) throws Err {
    if (!(object instanceof A4TupleSet))
        throw new ErrorFatal("Unexpected type error: expecting an A4TupleSet.");
    A4TupleSet s = (A4TupleSet) object;
    if (s.size() == 0)
        return SimTupleset.EMPTY;
    List<SimTuple> list = new ArrayList<SimTuple>(s.size());
    int arity = s.arity();
    for (A4Tuple t : s) {
        String[] array = new String[arity];
        for (int i = 0; i < t.arity(); i++) array[i] = t.atom(i);
        list.add(SimTuple.make(array));
    }
    return SimTupleset.make(list);
}
Also used : A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) SimTuple(edu.mit.csail.sdg.sim.SimTuple) ArrayList(java.util.ArrayList) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet)

Example 4 with A4TupleSet

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

the class ExampleCompilingFromSource method compileModuleAndRun.

public static void compileModuleAndRun(String model) throws Err {
    A4Reporter rep = new A4Reporter();
    // parse model from string
    CompModule world = CompUtil.parseEverything_fromString(rep, model);
    ConstList<Command> commands = world.getAllCommands();
    if (commands.size() != 1)
        throw new ErrorAPI("Must specify exactly one command; number of commands found: " + commands.size());
    Command cmd = commands.get(0);
    A4Options opt = new A4Options();
    opt.solver = A4Options.SatSolver.SAT4J;
    // solve
    A4Solution sol = TranslateAlloyToKodkod.execute_command(rep, world.getAllSigs(), cmd, opt);
    // print solution
    System.out.println(sol);
    for (Sig sig : world.getAllReachableSigs()) {
        System.out.println("traversing sig: " + sig);
        SafeList<Field> fields = sig.getFields();
        for (Field field : fields) {
            System.out.println("  traversing field: " + field);
            A4TupleSet ts = (sol.eval(field));
            for (A4Tuple t : ts) {
                System.out.print("    [");
                for (int i = 0; i < t.arity(); i++) System.out.print(t.atom(i) + " ");
                System.out.println("]");
            }
        }
    }
}
Also used : ErrorAPI(edu.mit.csail.sdg.alloy4.ErrorAPI) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) 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)

Aggregations

A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)4 A4TupleSet (edu.mit.csail.sdg.translator.A4TupleSet)4 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)2 Expr (edu.mit.csail.sdg.ast.Expr)2 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 ArrayList (java.util.ArrayList)2 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)1 Command (edu.mit.csail.sdg.ast.Command)1 Sig (edu.mit.csail.sdg.ast.Sig)1 Field (edu.mit.csail.sdg.ast.Sig.Field)1 CompModule (edu.mit.csail.sdg.parser.CompModule)1 SimTuple (edu.mit.csail.sdg.sim.SimTuple)1 A4Options (edu.mit.csail.sdg.translator.A4Options)1 A4Solution (edu.mit.csail.sdg.translator.A4Solution)1 LinkedHashSet (java.util.LinkedHashSet)1