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);
}
}
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);
}
}
}
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 "";
}
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);
}
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;
}
Aggregations