use of kodkod.instance.TupleFactory in project org.alloytools.alloy by AlloyTools.
the class OverflowTheoremTest method setupBounds.
protected void setupBounds() {
Relation ret = Relation.unary("ret");
int min = min(bw);
int max = max(bw);
List<String> atoms = new ArrayList<String>(max - min + 1);
for (int i = min; i <= max; i++) {
atoms.add(String.valueOf(i));
}
final Universe universe = new Universe(atoms);
TupleFactory factory = universe.factory();
this.bounds = new Bounds(factory.universe());
for (int i = min; i <= max; i++) {
bounds.boundExactly(i, factory.setOf(String.valueOf(i)));
}
bounds.bound(ret, factory.noneOf(1), factory.allOf(1));
}
use of kodkod.instance.TupleFactory in project org.alloytools.alloy by AlloyTools.
the class SymmetryBreaker method breakTotalOrder.
/**
* If possible, breaks symmetry on the given total ordering predicate and
* returns a formula f such that the meaning of total with respect to
* this.bounds is equivalent to the meaning of f with respect to this.bounds'.
* If symmetry cannot be broken on the given predicate, returns null.
* <p>
* We break symmetry on the relation constrained by the given predicate iff
* total.first, total.last, and total.ordered have the same upper bound, which,
* when cross-multiplied with itself gives the upper bound of total.relation.
* Assuming that this is the case, we then break symmetry on total.relation,
* total.first, total.last, and total.ordered using one of the methods described
* in {@linkplain #breakMatrixSymmetries(Map, boolean)}; the method used depends
* on the value of the "agressive" flag. The partition that formed the upper
* bound of total.ordered is removed from this.symmetries.
* </p>
*
* @return null if symmetry cannot be broken on total; otherwise returns a
* formula f such that the meaning of total with respect to this.bounds
* is equivalent to the meaning of f with respect to this.bounds'
* @ensures this.symmetries and this.bounds are modified as desribed in
* {@linkplain #breakMatrixSymmetries(Map, boolean)} iff total.first,
* total.last, and total.ordered have the same upper bound, which, when
* cross-multiplied with itself gives the upper bound of total.relation
* @see #breakMatrixSymmetries(Map,boolean)
*/
private final Formula breakTotalOrder(RelationPredicate.TotalOrdering total, boolean aggressive) {
final Relation first = total.first(), last = total.last(), ordered = total.ordered(), relation = total.relation();
final IntSet domain = bounds.upperBound(ordered).indexView();
if (symmetricColumnPartitions(ordered) != null && bounds.upperBound(first).indexView().contains(domain.min()) && bounds.upperBound(last).indexView().contains(domain.max())) {
// construct the natural ordering that corresponds to the ordering
// of the atoms in the universe
final IntSet ordering = Ints.bestSet(usize * usize);
int prev = domain.min();
for (IntIterator atoms = domain.iterator(prev + 1, usize); atoms.hasNext(); ) {
int next = atoms.next();
ordering.add(prev * usize + next);
prev = next;
}
if (ordering.containsAll(bounds.lowerBound(relation).indexView()) && bounds.upperBound(relation).indexView().containsAll(ordering)) {
// remove the ordered partition from the set of symmetric
// partitions
removePartition(domain.min());
final TupleFactory f = bounds.universe().factory();
if (aggressive) {
bounds.boundExactly(first, f.setOf(f.tuple(1, domain.min())));
bounds.boundExactly(last, f.setOf(f.tuple(1, domain.max())));
bounds.boundExactly(ordered, bounds.upperBound(total.ordered()));
bounds.boundExactly(relation, f.setOf(2, ordering));
return Formula.TRUE;
} else {
final Relation firstConst = Relation.unary("SYM_BREAK_CONST_" + first.name());
final Relation lastConst = Relation.unary("SYM_BREAK_CONST_" + last.name());
final Relation ordConst = Relation.unary("SYM_BREAK_CONST_" + ordered.name());
final Relation relConst = Relation.binary("SYM_BREAK_CONST_" + relation.name());
bounds.boundExactly(firstConst, f.setOf(f.tuple(1, domain.min())));
bounds.boundExactly(lastConst, f.setOf(f.tuple(1, domain.max())));
bounds.boundExactly(ordConst, bounds.upperBound(total.ordered()));
bounds.boundExactly(relConst, f.setOf(2, ordering));
return Formula.and(first.eq(firstConst), last.eq(lastConst), ordered.eq(ordConst), relation.eq(relConst));
// return first.eq(firstConst).and(last.eq(lastConst)).and(
// ordered.eq(ordConst)).and( relation.eq(relConst));
}
}
}
return null;
}
use of kodkod.instance.TupleFactory in project org.alloytools.alloy by AlloyTools.
the class Translation method interpret.
public Instance interpret(Bounds bounds) {
final SATSolver solver = cnf();
final Instance instance = new Instance(bounds.universe());
final TupleFactory f = bounds.universe().factory();
for (IndexedEntry<TupleSet> entry : bounds.intBounds()) {
instance.add(entry.index(), entry.value());
}
for (Relation r : bounds.relations()) {
// if (bnds != bounds && bnds.findRelByName(r.name()) == null)
// continue;
TupleSet lower = bounds.lowerBound(r);
IntSet indices = Ints.bestSet(lower.capacity());
indices.addAll(lower.indexView());
IntSet vars = primaryVariables(r);
if (!vars.isEmpty()) {
// System.out.println(r + ": [" + vars.min() + ", " + vars.max()
// + "]");
int lit = vars.min();
for (IntIterator iter = bounds.upperBound(r).indexView().iterator(); iter.hasNext(); ) {
final int index = iter.next();
if (!indices.contains(index) && solver.valueOf(lit++))
indices.add(index);
}
}
instance.add(r, f.setOf(r.arity(), indices));
}
return instance;
}
use of kodkod.instance.TupleFactory in project org.alloytools.alloy by AlloyTools.
the class Handshake method bounds.
/**
* Returns a bounds for the given number of persons.
*
* @return a bounds for the given number of persons.
*/
public Bounds bounds(int persons) {
final List<String> atoms = new ArrayList<String>(persons);
atoms.add("Hilary");
atoms.add("Jocelyn");
for (int i = 2; i < persons; i++) {
atoms.add("Person" + i);
}
final Universe u = new Universe(atoms);
final TupleFactory f = u.factory();
final Bounds b = new Bounds(u);
b.boundExactly(Person, f.allOf(1));
b.boundExactly(Hilary, f.setOf("Hilary"));
b.boundExactly(Jocelyn, f.setOf("Jocelyn"));
b.bound(spouse, f.allOf(2));
b.bound(shaken, f.allOf(2));
return b;
}
use of kodkod.instance.TupleFactory in project org.alloytools.alloy by AlloyTools.
the class ToyFilesystem method bounds.
/**
* Returns the toy filesystem bounds.
*
* @return toy filesystem bounds
*/
public final Bounds bounds() {
final Universe universe = new Universe("d0", "d1", "f0", "f1", "f2");
final Bounds bounds = new Bounds(universe);
final TupleFactory factory = universe.factory();
bounds.boundExactly(root, factory.setOf("d0"));
bounds.bound(dir, factory.setOf("d0", "d1"));
bounds.bound(file, factory.setOf("f0", "f1", "f2"));
bounds.bound(contents, factory.setOf(factory.tuple("d0", "d1")), bounds.upperBound(dir).product(factory.allOf(1)));
return bounds;
}
Aggregations