use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.
the class BoundsComputer method computeLowerBound.
// ==============================================================================================================//
/**
* Computes the lowerbound from bottom-up; it will also set a suitable initial
* value for each sig's upperbound. Precondition: sig is not a builtin sig
*/
private TupleSet computeLowerBound(List<Tuple> atoms, final PrimSig sig) throws Err {
int n = sc.sig2scope(sig);
TupleSet lower = factory.noneOf(1);
for (PrimSig c : sig.children()) lower.addAll(computeLowerBound(atoms, c));
TupleSet upper = lower.clone();
boolean isExact = sc.isExact(sig);
if (isExact || sig.isTopLevel())
for (n = n - upper.size(); n > 0; n--) {
Tuple atom = atoms.remove(atoms.size() - 1);
// atoms to the UPPERBOUND.
if (isExact)
lower.add(atom);
upper.add(atom);
}
lb.put(sig, lower);
ub.put(sig, upper);
return lower;
}
use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.
the class BoundsComputer method allocateSubsetSig.
// ==============================================================================================================//
/**
* Allocate relations for SubsetSig top-down.
*/
private Expression allocateSubsetSig(SubsetSig sig) throws Err {
// We must not visit the same SubsetSig more than once, so if we've been
// here already, then return the old value right away
Expression sum = sol.a2k(sig);
if (sum != null && sum != Expression.NONE)
return sum;
// Recursively form the union of all parent expressions
TupleSet ts = factory.noneOf(1);
for (Sig parent : sig.parents) {
Expression p = (parent instanceof PrimSig) ? sol.a2k(parent) : allocateSubsetSig((SubsetSig) parent);
ts.addAll(sol.query(true, p, false));
if (sum == null)
sum = p;
else
sum = sum.union(p);
}
// If subset is exact, then just use the "sum" as is
if (sig.exact) {
sol.addSig(sig, sum);
return sum;
}
// Allocate a relation for this subset sig, then bound it
rep.bound("Sig " + sig + " in " + ts + "\n");
Relation r = sol.addRel(sig.label, null, ts);
sol.addSig(sig, r);
// Add a constraint that it is INDEED a subset of the union of its
// parents
sol.addFormula(r.in(sum), sig.isSubset);
return r;
}
use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseSkolem.
/**
* Parse skolem.
*/
private ExprVar parseSkolem(String id) throws IOException, Err {
final XMLNode node = nmap.get(id);
if (node == null)
throw new IOException("Unknown ID " + id + " encountered.");
if (!node.is("skolem"))
throw new IOException("ID " + id + " is not a skolem.");
String label = label(node);
Expr type = null;
for (XMLNode sub : node) if (sub.is("types")) {
Expr t = parseType(sub);
if (type == null)
type = t;
else
type = type.plus(t);
}
int arity;
if (type == null || (arity = type.type().arity()) < 1)
throw new IOException("Skolem " + label + " is maltyped.");
ExprVar var = ExprVar.make(Pos.UNKNOWN, label, type.type());
TupleSet ts = parseTuples(node, arity);
expr2ts.put(var, ts);
return var;
}
use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseField.
/**
* Parse field.
*/
private Field parseField(String id) throws IOException, Err {
final XMLNode node = nmap.get(id);
if (node == null)
throw new IOException("Unknown FieldID " + id + " encountered.");
if (!node.is("field"))
throw new IOException("ID " + id + " is not a field.");
String label = label(node);
Pos isPrivate = yes(node, "private") ? Pos.UNKNOWN : null;
Pos isMeta = yes(node, "meta") ? Pos.UNKNOWN : null;
Expr type = null;
for (XMLNode sub : node) if (sub.is("types")) {
Expr t = parseType(sub);
if (type == null)
type = t;
else
type = type.plus(t);
}
int arity;
if (type == null || (arity = type.type().arity()) < 2)
throw new IOException("Field " + label + " is maltyped.");
String parentID = node.getAttribute("parentID");
Sig parent = id2sig.get(parentID);
if (parent == null)
throw new IOException("ID " + parentID + " is not a sig.");
Field field = null;
for (Field f : parent.getFields()) if (f.label.equals(label) && f.type().arity() == arity && choices.contains(f)) {
field = f;
choices.remove(f);
break;
}
if (field == null)
field = parent.addTrickyField(Pos.UNKNOWN, isPrivate, null, null, isMeta, new String[] { label }, UNIV.join(type))[0];
TupleSet ts = parseTuples(node, arity);
expr2ts.put(field, ts);
return field;
}
use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.
the class TableView method toTable.
/**
* Format a solution to a string
*
* @param solution
* @param instance
* @param sigs
* @return
*/
public static Map<String, Table> toTable(A4Solution solution, Instance instance, SafeList<Sig> sigs) {
Map<String, Table> map = new HashMap<String, Table>();
for (Sig s : sigs) {
if (!s.label.startsWith("this/"))
continue;
TupleSet instanceTuples = instance.tuples(s.label);
if (instanceTuples != null) {
SimTupleset sigInstances = toSimTupleset(instanceTuples);
Table table = new Table(sigInstances.size() + 1, s.getFields().size() + 1, 1);
table.set(0, 0, s.label);
if (s.getFields().size() == 0 && sigInstances.size() <= 1)
continue;
int c = 1;
for (Field f : s.getFields()) {
table.set(0, c++, f.label);
}
map.put(s.label, table);
int r = 1;
for (SimTuple sigInstance : sigInstances) {
assert sigInstance.arity() == 1;
SimTupleset leftJoin = SimTupleset.make(sigInstance);
table.set(r, 0, sigInstance.get(0));
c = 1;
for (Field f : s.getFields()) {
SimTupleset relations = toSimTupleset(solution.eval(f));
SimTupleset joined = leftJoin.join(relations);
Table relationTable = toTable(joined);
table.set(r, c++, relationTable);
}
r++;
}
}
}
return map;
}
Aggregations