Search in sources :

Example 21 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class SkolemizationTest method testDeepSkolems.

private final void testDeepSkolems(Multiplicity mult) {
    final Variable va = Variable.unary("va");
    final Variable vb = Variable.unary("vb");
    final Variable vc = Variable.unary("vc");
    final Variable vd = Variable.unary("vd");
    final Set<String> skolems = new HashSet<String>(4);
    Decl da1 = va.oneOf(r1a);
    Decl db = vb.declare(mult, r1b);
    Decl dc = vc.declare(mult, r1a);
    Decl dc1 = vc.oneOf(r1a);
    Decl dd = vd.declare(mult, r1b);
    Decl dd1 = vd.oneOf(r1b);
    skolems.add("$" + vb.name());
    Instance inst = solve(va.in(vb.join(r2b)).forSome(db).forAll(da1));
    assertSkolems(bounds, inst, skolems);
    skolems.add("$" + vc.name());
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(da1).forSome(dc).forAll(dd1));
    assertSkolems(bounds, inst, skolems);
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forSome(dc).forAll(da1.and(dd1)));
    assertSkolems(bounds, inst, skolems);
    skolems.add("$" + vd.name());
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(da1).forSome(dc).not().forAll(dd).not());
    assertSkolems(bounds, inst, skolems);
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forAll(dc).forAll(db).forSome(da1).not().forAll(dd1));
    skolems.remove("$" + vd.name());
    assertSkolems(bounds, inst, skolems);
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(dc1).forAll(da1).forAll(dd1));
    skolems.remove("$" + vc.name());
    assertSkolems(bounds, inst, skolems);
}
Also used : Variable(kodkod.ast.Variable) Instance(kodkod.instance.Instance) Decl(kodkod.ast.Decl) HashSet(java.util.HashSet)

Example 22 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class SymmetryBreakingTest method testTotalOrdering.

public void testTotalOrdering() {
    bounds.bound(to1, factory.area(factory.tuple("0", "0"), factory.tuple("4", "4")));
    bounds.bound(ord1, factory.setOf("0", "1", "2", "3", "4"));
    bounds.bound(first1, bounds.upperBound(ord1));
    bounds.bound(last1, bounds.upperBound(ord1));
    final Formula ordered1 = to1.totalOrder(ord1, first1, last1);
    assertNotNull(solve(to1.some().and(ordered1)));
    assertPrimVarNum(0);
    assertAuxVarNum(0);
    assertClauseNum(0);
    bounds.bound(r1, factory.range(factory.tuple("0"), factory.tuple("4")));
    assertNotNull(solve(to1.join(r1).some().and(ordered1)));
    assertPrimVarNum(bounds.upperBound(r1).size());
    bounds.boundExactly(r1, bounds.upperBound(r1));
    assertNotNull(solve(to1.join(r1).some().and(ordered1)));
    assertPrimVarNum(0);
    bounds.bound(to2, factory.setOf("5", "6", "7", "8", "9").product(factory.setOf("5", "7", "8")));
    bounds.bound(ord2, factory.setOf("5", "7", "8"));
    bounds.bound(first2, bounds.upperBound(ord2));
    bounds.bound(last2, bounds.upperBound(ord2));
    final Formula ordered2 = to2.totalOrder(ord2, first2, last2);
    assertNotNull(solve(to1.difference(to2).some().and(ordered2).and(ordered1)));
    assertPrimVarNum(0);
    assertAuxVarNum(0);
    assertClauseNum(0);
    bounds.bound(to3, factory.allOf(2));
    bounds.bound(ord3, factory.allOf(1));
    bounds.bound(first3, factory.setOf("9"));
    bounds.bound(last3, factory.setOf("8"));
    final Formula ordered3 = to3.totalOrder(ord3, first3, last3);
    assertNotNull(solve(to3.product(to1).some().and(ordered1).and(ordered3)));
    assertPrimVarNum(bounds.upperBound(to3).size() + bounds.upperBound(ord3).size() + 2);
    // SAT solver takes a while
    // bounds.boundExactly(r2, factory.setOf(factory.tuple("9","8")));
    // assertNotNull(solve(r2.in(to3).and(ordered3)));
    bounds.bound(to3, factory.allOf(2));
    bounds.bound(ord3, factory.setOf("3"));
    bounds.bound(first3, factory.allOf(1));
    bounds.bound(last3, factory.allOf(1));
    Instance instance = solve(ordered3);
    assertNotNull(instance);
    assertTrue(instance.tuples(to3).isEmpty());
    assertTrue(instance.tuples(ord3).equals(bounds.upperBound(ord3)));
    assertTrue(instance.tuples(first3).equals(bounds.upperBound(ord3)));
    assertTrue(instance.tuples(last3).equals(bounds.upperBound(ord3)));
}
Also used : Formula(kodkod.ast.Formula) Instance(kodkod.instance.Instance)

Example 23 with Instance

use of kodkod.instance.Instance 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)

Example 24 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class TranslatorTest method testIntersectionMultiplicity.

private final void testIntersectionMultiplicity(Multiplicity mult, Relation p, Relation q, Tuple intersection) {
    final Instance m = solve(p.intersection(q).apply(mult));
    assertNotNull(m);
    final Set<Tuple> ps = m.tuples(p), qs = m.tuples(q);
    assertFalse(ps.isEmpty());
    assertFalse(qs.isEmpty());
    assertTrue(ps.contains(intersection));
    assertTrue(qs.contains(intersection));
}
Also used : Instance(kodkod.instance.Instance) Tuple(kodkod.instance.Tuple)

Example 25 with Instance

use of kodkod.instance.Instance in project org.alloytools.alloy by AlloyTools.

the class BugTests method testGreg_11232005.

public final void testGreg_11232005() {
    final List<String> atoms = new ArrayList<String>(3);
    atoms.add("-1");
    atoms.add("0");
    atoms.add("1");
    final Universe u = new Universe(atoms);
    final TupleFactory t = u.factory();
    final Relation inc = Relation.binary("inc"), add = Relation.ternary("add"), one = Relation.unary("1"), param0 = Relation.unary("param0"), ints = Relation.unary("int");
    // (one param0 && ((1 . (param0 . add)) in (param0 . ^inc)))
    final Formula f = param0.one().and((one.join(param0.join(add))).in(param0.join(inc.closure())));
    final Bounds b = new Bounds(u);
    b.bound(param0, t.allOf(1));
    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")));
    // System.out.println(f);
    // System.out.println(b);
    final Instance instance = solver.solve(f, b).instance();
    assertTrue((new Evaluator(instance)).evaluate(f));
// System.out.println(instance);
// System.out.println((new Evaluator(instance)).evaluate(f ));
}
Also used : Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator)

Aggregations

Instance (kodkod.instance.Instance)25 Formula (kodkod.ast.Formula)17 Relation (kodkod.ast.Relation)15 TupleFactory (kodkod.instance.TupleFactory)15 Universe (kodkod.instance.Universe)14 TupleSet (kodkod.instance.TupleSet)13 Bounds (kodkod.instance.Bounds)12 ArrayList (java.util.ArrayList)8 Variable (kodkod.ast.Variable)8 Evaluator (kodkod.engine.Evaluator)8 IntExpression (kodkod.ast.IntExpression)6 Solution (kodkod.engine.Solution)6 Expression (kodkod.ast.Expression)5 Tuple (kodkod.instance.Tuple)5 Decl (kodkod.ast.Decl)4 HashSet (java.util.HashSet)3 Solver (kodkod.engine.Solver)3 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 Err (edu.mit.csail.sdg.alloy4.Err)1 UniqueNameGenerator (edu.mit.csail.sdg.alloy4.UniqueNameGenerator)1