use of kodkod.instance.Tuple in project org.alloytools.alloy by AlloyTools.
the class BookExamples method trial.
/**
* This tries a particular solution against the formula.
*/
private static Solution trial(A4Reporter rep, TupleFactory fac, Solver solver, Iterable<Sig> sigs, Formula f, A4Solution frame, Object[] t) {
try {
frame.kr2typeCLEAR();
Bounds b = null;
TupleSet ts = null;
for (int i = 1; i < t.length; i++) {
Object x = t[i];
if (x == null)
return null;
if (x instanceof String && ((String) x).length() > 0) {
// This
// means
// it's
// a
// unary
// Tuple
// containing
// the
// given
// atom
Tuple xx = fac.tuple((String) x);
if (ts == null)
ts = fac.noneOf(1);
ts.add(xx);
continue;
}
if (x instanceof Tuple) {
// This means it's a Tuple
Tuple xx = (Tuple) x;
if (ts == null)
ts = fac.noneOf(xx.arity());
ts.add(xx);
continue;
}
if (x instanceof String) {
// The empty string means the sig
// name follows here
i++;
if (i >= t.length - 1 || !(t[i] instanceof String) || !(t[i + 1] instanceof String))
return null;
String sigName = (String) (t[i]);
i++;
String fieldName = (String) (t[i]);
Sig first = hasSig(sigs, sigName);
if (first == null)
return null;
Expression expr = null;
if (fieldName.length() == 0) {
expr = frame.a2k(first);
} else {
for (Field field : first.getFields()) if (field.label.equals(fieldName)) {
expr = frame.a2k(field);
while (expr instanceof BinaryExpression) expr = ((BinaryExpression) expr).right();
break;
}
}
if (!(expr instanceof Relation))
return null;
if (b == null)
// We delay the expansive
b = frame.getBounds();
// really find a possible match
if (ts == null)
ts = fac.noneOf(expr.arity());
if (!ts.containsAll(b.lowerBound((Relation) expr)))
// Sanity check
return null;
if (!b.upperBound((Relation) expr).containsAll(ts))
// Sanity check
return null;
b.boundExactly((Relation) expr, ts);
ts = null;
continue;
}
}
SATFactory sat = solver.options().solver();
Solution sol;
try {
solver.options().setSolver(SATFactory.DefaultSAT4J);
sol = solver.solve(f, b);
} finally {
solver.options().setSolver(sat);
}
if (sol == null || (sol.outcome() != SATISFIABLE && sol.outcome() != TRIVIALLY_SATISFIABLE))
return null;
if (rep != null)
rep.debug("Comment: " + t[0] + "\n");
return sol;
} catch (Throwable ex) {
return null;
}
}
use of kodkod.instance.Tuple in project org.alloytools.alloy by AlloyTools.
the class TranslateKodkodToJava method printTupleset.
/**
* Print the tupleset using the name n.
*/
private void printTupleset(String n, TupleSet ts, Map<Object, String> atomMap) {
file.printf("TupleSet %s = factory.noneOf(%d);%n", n, ts.arity());
for (Tuple t : ts) {
file.printf("%s.add(", n);
for (int i = 0; i < ts.arity(); i++) {
if (i != 0)
file.printf(".product(");
Object a = t.atom(i);
String b = atomMap == null ? null : atomMap.get(a);
file.printf("factory.tuple(\"%s\")", (b == null ? a.toString() : b));
if (i != 0)
file.printf(")");
}
file.printf(");%n");
}
}
use of kodkod.instance.Tuple in project org.alloytools.alloy by AlloyTools.
the class IntConstraints method bounds.
/**
* Returns a bounds for the problem.
*/
public final Bounds bounds() {
final List<Integer> atoms = new ArrayList<Integer>(14);
for (int i = 0; i < 32; i++) {
atoms.add(Integer.valueOf(1 << i));
}
final Universe u = new Universe(atoms);
final TupleFactory f = u.factory();
final Bounds b = new Bounds(u);
// bound the integers
for (int i = 0; i < 32; i++) {
b.boundExactly(1 << i, f.setOf(Integer.valueOf(1 << i)));
}
// instance based on the low/high range for each variable.
for (int i = 0; i < 1000; i++) {
TupleSet lower = f.noneOf(1), upper = f.noneOf(1);
int min = low + i * 10, max = min + 10, bit = 31;
// constraints
while (bit >= 0) {
int bitVal = 1 << bit;
if ((min & bitVal) == (max & bitVal)) {
if ((min & bitVal) != 0) {
Tuple bitTuple = f.tuple(Integer.valueOf(bitVal));
lower.add(bitTuple);
upper.add(bitTuple);
}
} else {
// get out of the loop as soon as patterns diverge
break;
}
bit--;
}
// them into upper bound but not lower
while (bit >= 0) {
upper.add(f.tuple(Integer.valueOf(1 << bit)));
bit--;
}
b.bound(var[i], lower, upper);
}
return b;
}
use of kodkod.instance.Tuple in project org.alloytools.alloy by AlloyTools.
the class ALG197 method bounds.
/**
* Returns the bounds the problem (axioms 1, 4, 9-11, last formula of 14-15, and
* first formula of 16-22).
*
* @return the bounds for the problem
*/
@Override
public final Bounds bounds() {
final Bounds b = super.bounds();
final TupleFactory f = b.universe().factory();
final TupleSet op1h = b.upperBound(op1).clone();
final TupleSet op2h = b.upperBound(op2).clone();
// axiom
final TupleSet op1l = f.setOf(f.tuple("e16", "e16", "e15"));
// 14,
// line
// 6
// axiom
final TupleSet op2l = f.setOf(f.tuple("e26", "e26", "e25"));
// 15,
// line
// 6
op1h.removeAll(f.area(f.tuple("e16", "e16", "e10"), f.tuple("e16", "e16", "e16")));
op1h.addAll(op1l);
op2h.removeAll(f.area(f.tuple("e26", "e26", "e20"), f.tuple("e26", "e26", "e26")));
op2h.addAll(op2l);
b.bound(op1, op1l, op1h);
b.bound(op2, op2l, op2h);
final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e15", "e26"));
// first line of axioms 16-22
for (int i = 0; i < 7; i++) {
Tuple t = f.tuple("e16", "e2" + i);
high.add(t);
b.bound(h[i], f.setOf(t), high);
high.remove(t);
}
return b;
}
use of kodkod.instance.Tuple in project org.alloytools.alloy by AlloyTools.
the class BugTests method testFelix_02222008.
public final void testFelix_02222008() {
List<String> atomlist = Arrays.asList("X1", "X2", "X3");
Universe universe = new Universe(atomlist);
TupleFactory factory = universe.factory();
Bounds bounds = new Bounds(universe);
Relation x = Relation.unary("X");
TupleSet x_upper = factory.noneOf(1);
x_upper.add(factory.tuple("X1"));
x_upper.add(factory.tuple("X2"));
x_upper.add(factory.tuple("X3"));
bounds.bound(x, x_upper);
Variable a = Variable.unary("a");
Variable b = Variable.unary("b");
Variable c = Variable.unary("c");
Formula goal = x.lone().not().and(b.union(c).eq(a).forSome(c.oneOf(x)).forAll(b.oneOf(x)).forSome(a.setOf(x)));
Solver solver = new Solver();
solver.options().setSolver(SATFactory.DefaultSAT4J);
solver.options().setBitwidth(4);
solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
solver.options().setSymmetryBreaking(0);
solver.options().setSkolemDepth(2);
Iterator<Solution> itr = solver.solveAll(goal, bounds);
int sols = 0;
while (itr.hasNext()) {
Solution sol = itr.next();
Instance inst = sol.instance();
if (inst == null)
break;
sols++;
for (Relation rel : inst.relations()) {
if (rel != x) {
if (rel.arity() == 1) {
// rel = a
assertEquals(inst.tuples(x), inst.tuples(rel));
} else {
// rel = c
final TupleSet dom = factory.noneOf(1);
for (Tuple t : inst.tuples(rel)) {
dom.add(factory.tuple(t.atom(0)));
}
assertEquals(inst.tuples(x), dom);
}
}
}
}
assertEquals(3, sols);
}
Aggregations