Search in sources :

Example 6 with Instance

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

the class IntTest method testBinOp.

private final void testBinOp(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int result, int realResult, int mask) {
    final IntExpression e = ei.compose(op, ej);
    final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(e.eq(constant(result)));
    final Solution s = solve(f);
    Instance inst = s.instance();
    if (overflows(op, ei, ej, i, j, realResult)) {
        assertNull(f.toString(), inst);
    } else {
        assertNotNull(f.toString(), inst);
        final Evaluator eval = new Evaluator(inst, solver.options());
        assertEquals(f.toString(), result & mask, eval.evaluate(e) & mask);
    }
}
Also used : Formula(kodkod.ast.Formula) Instance(kodkod.instance.Instance) IntExpression(kodkod.ast.IntExpression) Evaluator(kodkod.engine.Evaluator) Solution(kodkod.engine.Solution)

Example 7 with Instance

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

the class IntTest method testBitsetCast.

public void testBitsetCast() {
    final int width = 4, msb = width - 1;
    solver.options().setBitwidth(width);
    final List<Integer> atoms = new ArrayList<Integer>(width);
    for (int i = 0; i < msb; i++) {
        atoms.add(Integer.valueOf(1 << i));
    }
    atoms.add(Integer.valueOf(-1 << msb));
    final Bounds b = new Bounds(new Universe(atoms));
    final TupleFactory f = b.universe().factory();
    for (Integer i : atoms) {
        b.boundExactly(i, f.setOf(i));
    }
    b.bound(r1, f.allOf(1));
    for (int i = -1 << msb, max = 1 << msb; i < max; i++) {
        Formula test = r1.sum().toBitset().eq(IntConstant.constant(i).toBitset());
        Solution sol = solver.solve(test, b);
        Instance inst = sol.instance();
        assertNotNull(inst);
        Evaluator eval = new Evaluator(inst, solver.options());
        assertEquals(i, eval.evaluate(r1.sum()));
    }
}
Also used : Formula(kodkod.ast.Formula) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) Solution(kodkod.engine.Solution)

Example 8 with Instance

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

the class BugTests method testMana_01132006.

public final void testMana_01132006() {
    // r0=[[], [[null], [DblLinkedList0]]],
    // null=[[[null]], [[null]]],
    // head=[[], [[DblLinkedList0, null], [DblLinkedList0,
    // DblLinkedListElem0]]],
    // next=[[], [[DblLinkedListElem0, null], [DblLinkedListElem0,
    // DblLinkedListElem0]]],
    // univ=[[[null], [DblLinkedList0], [1], [DblLinkedListElem0], [0]],
    // [[null], [DblLinkedList0], [1], [DblLinkedListElem0], [0]]]
    // r1=[[], [[null], [DblLinkedListElem0]]],
    final List<String> atoms = new ArrayList<String>(5);
    atoms.add("null");
    atoms.add("DblLinkedList0");
    atoms.add("1");
    atoms.add("DblLinkedListElem0");
    atoms.add("0");
    final Universe u = new Universe(atoms);
    final TupleFactory t = u.factory();
    // !((head . univ) in ((if (r1 in null) then (head ++ (r0 -> (r1 .
    // next))) else head) . univ))
    final Relation head = Relation.binary("head"), univ = Relation.unary("univ"), r0 = Relation.unary("r0"), r1 = Relation.unary("r1"), next = Relation.binary("next"), nil = Relation.unary("null"), none = Relation.unary("none");
    final Expression override = head.override(r0.product(r1.join(next)));
    final Expression ifElse = r1.in(nil).thenElse(override, head);
    final Formula f = head.join(univ).in(ifElse.join(univ)).not();
    final Bounds b = new Bounds(u);
    b.bound(r0, t.setOf("null", "DblLinkedList0"));
    b.bound(r1, t.setOf("null", "DblLinkedListElem0"));
    b.bound(head, t.setOf("DblLinkedList0").product(b.upperBound(r1)));
    b.bound(next, t.setOf(t.tuple("DblLinkedListElem0", "null"), t.tuple("DblLinkedListElem0", "DblLinkedListElem0")));
    b.boundExactly(univ, t.allOf(1));
    b.boundExactly(nil, t.setOf("null"));
    b.boundExactly(none, t.noneOf(1));
    // System.out.println(f);
    // System.out.println(b);
    final Instance instance = solver.solve(f, b).instance();
    assertNull(instance);
}
Also used : Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe)

Example 9 with Instance

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

the class BugTests method testVincent_02162006.

public final void testVincent_02162006() {
    // set ups universe of atoms [1..257]
    final List<Integer> atoms = new ArrayList<Integer>();
    // change this to 256, and the program works
    for (int i = 0; i < 257; i++) {
        atoms.add(i + 1);
    }
    final Universe universe = new Universe(atoms);
    final Bounds bounds = new Bounds(universe);
    final TupleFactory factory = universe.factory();
    // oneRel is bounded to the Integer 1
    final Relation oneRel = Relation.unary("oneRel");
    // rel can contain anything
    final Relation rel = Relation.unary("rel");
    bounds.bound(oneRel, factory.setOf(factory.tuple(atoms.get(0))), factory.setOf(factory.tuple(atoms.get(0))));
    bounds.bound(rel, factory.allOf(1));
    // constraint: oneRel in rel
    Formula formula = oneRel.in(rel);
    // solve
    final Instance instance = solver.solve(formula, bounds).instance();
    assertNotNull(instance);
// System.out.println(instance);
}
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)

Example 10 with Instance

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

the class BugTests method testFelix_03062008_2.

public final void testFelix_03062008_2() {
    Relation x5 = Relation.unary("Role");
    Relation x6 = Relation.unary("Session");
    List<String> atomlist = Arrays.asList("Role$0", "Session$0", "Session$1");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x5_upper = factory.noneOf(1);
    x5_upper.add(factory.tuple("Role$0"));
    bounds.bound(x5, x5_upper);
    TupleSet x6_upper = factory.noneOf(1);
    x6_upper.add(factory.tuple("Session$0"));
    x6_upper.add(factory.tuple("Session$1"));
    bounds.bound(x6, x6_upper);
    Variable x11 = Variable.unary("x_a");
    Decls x10 = x11.oneOf(x6);
    Variable x15 = Variable.unary("x_b");
    Decls x14 = x15.oneOf(x5);
    Variable x17 = Variable.unary("x_c");
    Decls x16 = x17.oneOf(x5);
    Decls x13 = x14.and(x16);
    Expression x20 = x15.product(x17);
    Expression x19 = x11.product(x20);
    Formula x18 = x19.some();
    Formula x12 = x18.forSome(x13);
    Formula x9 = x12.forAll(x10);
    Formula x24 = x5.some();
    Formula x23 = x24.not();
    Formula x28 = x5.eq(x5);
    Formula x29 = x6.eq(x6);
    Formula x25 = x28.and(x29);
    Formula x22 = x23.and(x25);
    Formula x8 = x9.and(x22).and(x5.no()).and(x6.no());
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(2);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(2);
    System.out.flush();
    Solution sol = solver.solve(x8, bounds);
    Instance inst = sol.instance();
    assertNotNull(inst);
    for (Relation rel : inst.relations()) {
        if (rel != x5 && rel != x6) {
            final TupleSet range = inst.tuples(x6).product(inst.tuples(x5));
            assertTrue(range.containsAll(inst.tuples(rel)));
        }
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Solution(kodkod.engine.Solution)

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