Search in sources :

Example 1 with A4Tuple

use of edu.mit.csail.sdg.translator.A4Tuple 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 A4Tuple

use of edu.mit.csail.sdg.translator.A4Tuple 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 A4Tuple

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

the class VizTree method convertValueToText.

/**
 * {@inheritDoc}
 */
@Override
public String convertValueToText(Object val, boolean selected, boolean expanded, boolean leaf, int row, boolean focus) {
    String c = ">";
    if (onWindows)
        c = selected ? " style=\"color:#ffffff;\">" : " style=\"color:#000000;\">";
    if (val instanceof A4Solution)
        return "<html> <b" + c + encode(title == null ? "" : title) + "</b></html>";
    if (val instanceof Sig) {
        String label = ((Sig) val).label;
        if (label.startsWith("this/"))
            label = label.substring(5);
        return "<html> <b" + c + "sig " + encode(label) + "</b></html>";
    }
    if (val instanceof ExprVar)
        return "<html> <b" + c + "set " + encode(((ExprVar) val).label) + "</b></html>";
    if (val instanceof String)
        return "<html> <span" + c + encode((String) val) + "</span></html>";
    if (val instanceof Pair)
        return "<html> <b" + c + "field " + encode(((ExprHasName) (((Pair<?, ?>) val).b)).label) + "</b></html>";
    if (val instanceof A4Tuple) {
        StringBuilder sb = new StringBuilder("<html> <span" + c);
        A4Tuple tp = (A4Tuple) val;
        for (int i = 1; i < tp.arity(); i++) {
            if (i > 1)
                sb.append(" -> ");
            sb.append(encode(tp.atom(i)));
        }
        sb.append("</span></html>");
        return sb.toString();
    }
    return "";
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) A4Solution(edu.mit.csail.sdg.translator.A4Solution) Pair(edu.mit.csail.sdg.alloy4.Pair)

Example 4 with A4Tuple

use of edu.mit.csail.sdg.translator.A4Tuple 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 5 with A4Tuple

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

the class Helper method atom2sig.

/**
 * Given an A4Solution, return a map that maps every atom to its most specific
 * signature.
 * <p>
 * For example, suppose we have "sig Animal { }" and "sig Dog, Cat extends
 * Animal { }". Suppose the solution says Animal={A$1, A$2, A$3, A$4} and
 * Dog={A$1} and Cat={A$2, A$3}. This method will return a map that maps A$1 to
 * Dog, A$2 to Cat, A$3 to Cat, and A$4 to Animal. (A$1 is both an Animal and a
 * Dog, but Dog is a subtype of Animal, so Dog is A$1's most specific signature)
 */
public static Map<String, PrimSig> atom2sig(A4Solution solution) throws Err {
    Map<String, PrimSig> map = new HashMap<String, PrimSig>();
    for (Sig s : solution.getAllReachableSigs()) if (s instanceof PrimSig)
        for (A4Tuple t : solution.eval(s)) {
            // We skip over SubsetSig and only care about PrimSig
            String atom = t.atom(0);
            PrimSig old = map.get(atom);
            if (old == null || ((PrimSig) s).isSameOrDescendentOf(old)) {
                map.put(atom, (PrimSig) s);
            }
        }
    return map;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) HashMap(java.util.HashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)6 A4TupleSet (edu.mit.csail.sdg.translator.A4TupleSet)4 Sig (edu.mit.csail.sdg.ast.Sig)3 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)2 Expr (edu.mit.csail.sdg.ast.Expr)2 A4Solution (edu.mit.csail.sdg.translator.A4Solution)2 ArrayList (java.util.ArrayList)2 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)1 Pair (edu.mit.csail.sdg.alloy4.Pair)1 Command (edu.mit.csail.sdg.ast.Command)1 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)1 ExprVar (edu.mit.csail.sdg.ast.ExprVar)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 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1