use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.
the class EvaluatorTest method setUp.
/*
* @see TestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
URL resource = getClass().getResource(SOLUTION);
evaluator = new Evaluator(InstanceCreator.getInstance(resource.openStream()));
Map<String, Relation> nameToRelation = new HashMap<String, Relation>();
for (Relation r : evaluator.instance().relations()) {
nameToRelation.put(r.name(), r);
}
univ = relation(nameToRelation, UNIV_PATH, "univ");
hilary = relation(nameToRelation, PATH, "Hilary");
jocelyn = relation(nameToRelation, PATH, "Jocelyn");
person = relation(nameToRelation, PATH, "Person");
spouse = relation(nameToRelation, PATH, "spouse");
shaken = relation(nameToRelation, PATH, "shaken");
}
use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.
the class InstanceCreator method processField.
/**
* Creates a Relation r whose name specified by the NAME attribute of the module
* and the field, and whose arity is specified by the ARITY attribute of the
* field. It adds a mapping to this.relations from r to a set of tuples that
* represents all the tuples assigned to the given field.
*/
private void processField(Element module, Element field) {
try {
final Relation fieldRelation = Relation.nary(getName(module) + "/" + getName(field), Integer.parseInt(field.getAttribute(ARITY)));
final Set<List<String>> tuples = new LinkedHashSet<List<String>>();
for (Iterator<Element> t = getChildrenByTag(field, TUPLE); t.hasNext(); ) {
tuples.add(getNames(getChildrenByTag(t.next(), ATOM)));
}
relations.put(fieldRelation, tuples);
} catch (NumberFormatException nfe) {
throw new InstanceCreationException("Fields must have an arity atribute: " + field.getAttribute(NAME));
}
}
use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.
the class InstanceCreator method getInstance.
/**
* Fills the instance with the relations specified in this.document; if an error
* occurs during processing, the instance remains null
*/
private Instance getInstance() {
initAtomsAndRelations();
final Universe u = new Universe(atoms);
final TupleFactory f = u.factory();
final Instance instance = new Instance(u);
for (Map.Entry<Relation, Set<List<String>>> entry : relations.entrySet()) {
Relation r = entry.getKey();
TupleSet s = f.noneOf(r.arity());
for (List<?> atoms : entry.getValue()) {
s.add(f.tuple(atoms));
}
instance.add(r, s);
}
return instance;
}
use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.
the class BenchmarkSymmStats2 method toNauty.
private static void toNauty(Bounds bounds, PrintStream stream) {
int size = bounds.universe().size() + bounds.ints().size();
for (Relation r : bounds.relations()) {
final int upsize = bounds.upperBound(r).size(), lowsize = bounds.lowerBound(r).size();
size += (upsize == lowsize ? upsize : upsize + lowsize) * r.arity();
}
stream.println("n=" + size + " $0 *=13 k = 0 " + size + " +d -a g");
int v = bounds.universe().size();
final IntVector vec = new ArrayIntVector();
vec.add(v);
for (Relation r : bounds.relations()) {
final int arity = r.arity();
final TupleSet up = bounds.upperBound(r), down = bounds.lowerBound(r);
final TupleSet[] sets = up.size() == down.size() || down.size() == 0 ? new TupleSet[] { up } : new TupleSet[] { down, up };
for (TupleSet s : sets) {
for (Tuple t : s) {
for (int i = 0, max = arity - 1; i < max; i++) {
stream.println(v + " : " + (v + 1) + " " + t.atomIndex(i) + ";");
v++;
}
stream.println(v + " : " + t.atomIndex(arity - 1) + ";");
v++;
}
vec.add(v);
}
}
for (TupleSet s : bounds.intBounds().values()) {
stream.println(v + " : " + s.iterator().next().atomIndex(0) + ";");
v++;
vec.add(v);
}
// stream.println(".");
stream.print("f = [ 0:" + (vec.get(0) - 1));
for (int i = 1; i < vec.size(); i++) {
stream.print(" | " + vec.get(i - 1) + ":" + (vec.get(i) - 1));
}
stream.println(" ]");
stream.println("x");
stream.println("o");
stream.println("q");
}
use of kodkod.ast.Relation in project org.alloytools.alloy by AlloyTools.
the class TranslatorTest method testFlattening.
public final void testFlattening() {
final List<String> atoms = new ArrayList<String>(9);
atoms.add("-1");
atoms.add("0");
atoms.add("1");
atoms.add("null");
for (int i = 0; i < 5; i++) {
atoms.add("m" + i);
}
final Universe u = new Universe(atoms);
final TupleFactory t = u.factory();
final Relation[] m = new Relation[5];
for (int i = 0; i < 5; i++) {
m[i] = Relation.unary("m" + i);
}
final Relation univ = Relation.unary("univ"), none = Relation.unary("none"), iden = Relation.binary("inden"), mutant = Relation.unary("mutant"), inc = Relation.binary("inc"), add = Relation.ternary("add"), i0 = Relation.unary("i0"), zero = Relation.unary("0"), one = Relation.unary("1"), ints = Relation.unary("int");
final Bounds b = new Bounds(u);
b.boundExactly(univ, t.allOf(1));
b.boundExactly(none, t.noneOf(1));
TupleSet idenSet = t.noneOf(2);
for (String atom : atoms) {
idenSet.add(t.tuple(atom, atom));
}
b.boundExactly(iden, idenSet);
b.bound(mutant, t.range(t.tuple("m0"), t.tuple("m4")));
for (int i = 0; i < 5; i++) {
b.boundExactly(m[i], t.setOf("m" + i));
}
b.bound(i0, t.range(t.tuple("-1"), t.tuple("1")));
b.boundExactly(zero, t.setOf(t.tuple("0")));
b.boundExactly(one, t.setOf(t.tuple("1")));
b.boundExactly(ints, t.allOf(1));
b.boundExactly(inc, t.setOf(t.tuple("-1", "0"), t.tuple("0", "1")));
// [1, 1, -1], [1, -1, 0], [1, 0, 1], [-1, 1, 0], [-1, -1, 1],
// [-1, 0, -1], [0, 1, 1], [0, -1, -1], [0, 0, 0]]
b.boundExactly(add, t.setOf(t.tuple("1", "1", "-1"), t.tuple("1", "-1", "0"), t.tuple("1", "0", "1"), t.tuple("-1", "1", "0"), t.tuple("-1", "-1", "1"), t.tuple("-1", "0", "-1"), t.tuple("0", "1", "1"), t.tuple("0", "-1", "-1"), t.tuple("0", "0", "0")));
/*
* ((one i0 && one mutant) && !((if (i0 in (0 . ^~inc)) then ((add . 0) . i0)
* else i0) = (if !((if (mutant in m4) then (if (i0 in 0) then 1 else 0) else
* (if (mutant in m3) then (if ((i0 = 0) || (i0 in (0 . ^~inc))) then 1 else 0)
* else (if (mutant in m2) then (if (i0 in (0 . ^inc)) then 1 else 0) else (if
* (mutant in m1) then (if ((i0 = 0) || (i0 in (0 . ^inc))) then 1 else 0) else
* (if (mutant in m0) then (if !(i0 in 0) then 1 else 0) else (if (i0 in (0 .
* ^~inc)) then 1 else 0)))))) in 0) then ((add . 0) . i0) else i0)))
*/
final Formula[] cm = new Formula[5];
for (int i = 0; i < 5; i++) {
cm[i] = mutant.in(m[i]);
}
// (i0 in (0 . ^~inc))
final Formula fs0 = i0.in(zero.join(inc.transpose().closure()));
// (i0 in (0 . ^inc))
final Formula fs1 = i0.in(zero.join(inc.closure()));
// (i0 = 0)
final Formula fs2 = i0.eq(zero);
final Expression em0 = cm[0].thenElse(i0.in(zero).not().thenElse(one, zero), fs0.thenElse(one, zero));
final Expression em1 = cm[1].thenElse((fs2.or(fs1)).thenElse(one, zero), em0);
final Expression em2 = cm[2].thenElse(fs1.thenElse(one, zero), em1);
final Expression em3 = cm[3].thenElse(fs2.or(fs0).thenElse(one, zero), em2);
final Expression em4 = cm[4].thenElse(i0.in(zero).thenElse(one, zero), em3);
final Expression es1 = add.join(zero).join(i0);
final Expression expr2 = em4.in(zero).not().thenElse(es1, i0);
final Expression expr1 = fs0.thenElse(es1, i0);
final Formula f = i0.one().and(mutant.one()).and(expr1.eq(expr2).not());
final Instance instance = solver.solve(f, b).instance();
assertNotNull(instance);
// System.out.println(instance);
}
Aggregations