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