Search in sources :

Example 71 with Relation

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");
}
Also used : Relation(kodkod.ast.Relation) HashMap(java.util.HashMap) Evaluator(kodkod.engine.Evaluator) URL(java.net.URL)

Example 72 with Relation

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));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Relation(kodkod.ast.Relation) Element(org.w3c.dom.Element) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 73 with Relation

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;
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) LinkedHashSet(java.util.LinkedHashSet) TupleSet(kodkod.instance.TupleSet) Set(java.util.Set) Instance(kodkod.instance.Instance) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) HashMap(java.util.HashMap) Map(java.util.Map)

Example 74 with Relation

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");
}
Also used : ArrayIntVector(kodkod.util.ints.ArrayIntVector) TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) IntVector(kodkod.util.ints.IntVector) ArrayIntVector(kodkod.util.ints.ArrayIntVector) Tuple(kodkod.instance.Tuple)

Example 75 with Relation

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);
}
Also used : TupleSet(kodkod.instance.TupleSet) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression)

Aggregations

Relation (kodkod.ast.Relation)87 Bounds (kodkod.instance.Bounds)49 Formula (kodkod.ast.Formula)45 TupleFactory (kodkod.instance.TupleFactory)45 Universe (kodkod.instance.Universe)45 TupleSet (kodkod.instance.TupleSet)43 Solution (kodkod.engine.Solution)30 Expression (kodkod.ast.Expression)26 IntExpression (kodkod.ast.IntExpression)24 Solver (kodkod.engine.Solver)24 ArrayList (java.util.ArrayList)23 Variable (kodkod.ast.Variable)23 Instance (kodkod.instance.Instance)15 Decls (kodkod.ast.Decls)13 Evaluator (kodkod.engine.Evaluator)9 Tuple (kodkod.instance.Tuple)8 IntSet (kodkod.util.ints.IntSet)7 BinaryExpression (kodkod.ast.BinaryExpression)6 Decl (kodkod.ast.Decl)6 LinkedHashSet (java.util.LinkedHashSet)4