Search in sources :

Example 6 with Evaluator

use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.

the class IntTest method testCompOp.

private final void testCompOp(IntCompOperator op, IntExpression ei, IntExpression ej, int i, int j, boolean result) {
    final Formula e = ei.compare(op, ej);
    final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(result ? e : e.not());
    final Solution s = solve(f);
    if (overflows(ei, i, 0) || overflows(ej, j, 0)) {
        assertNull(f.toString(), s.instance());
    } else {
        assertNotNull(f.toString(), s.instance());
        final Evaluator eval = new Evaluator(s.instance(), solver.options());
        assertFalse(result ^ eval.evaluate(e));
    }
}
Also used : Formula(kodkod.ast.Formula) Evaluator(kodkod.engine.Evaluator) Solution(kodkod.engine.Solution)

Example 7 with Evaluator

use of kodkod.engine.Evaluator 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 8 with Evaluator

use of kodkod.engine.Evaluator 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 9 with Evaluator

use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.

the class IntTest method testUnOp.

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

Example 10 with Evaluator

use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.

the class EvaluatorTest method testEvalTransitiveClosure.

public final void testEvalTransitiveClosure() {
    // // ^(Hilary->Jocelyn) = Hilary->Jocelyn
    // assertEquals(eval(hilary.product(jocelyn).closure()),
    // eval(hilary.product(jocelyn)));
    // // ^spouse = spouse + spouse.spouse
    // assertEquals(eval(spouse.closure()),
    // eval(spouse.union(spouse.join(spouse))));
    // // Hilary.^shaken = univ - (univ - Person.shaken)
    // assertEquals(eval(hilary.join(shaken.closure())),
    // eval(univ.difference(univ.difference(person.join(shaken)))));
    // try {
    // eval(person.closure());
    // fail("Expected IllegalArgumentException");
    // } catch (IllegalArgumentException iae) {}
    // ^r = value(^r)
    final Relation r = Relation.binary("r");
    final Universe u = evaluator.instance().universe();
    final TupleFactory f = u.factory();
    final Instance instance = new Instance(u);
    // value(r) = u[0]->u[1] + u[1]->u[2] + u[2]->u[3] + u[3]->u[4]
    TupleSet s = f.noneOf(r.arity());
    for (int i = 0; i < 4; i++) s.add(f.tuple(u.atom(i), u.atom(i + 1)));
    instance.add(r, s);
    // value(^r) = value(r) + u[0]->u[2] + u[0]->u[3] + u[0]->u[4] +
    // u[1]->u[3] u[1]->u[4] + u[2]->u[4]
    Set<Tuple> result = new HashSet<Tuple>();
    for (int i = 0; i < 4; i++) {
        for (int j = i + 1; j < 5; j++) {
            result.add(f.tuple(u.atom(i), u.atom(j)));
        }
    }
    assertEquals((new Evaluator(instance)).evaluate(r.closure()), result);
    // value(*r) = value(^r) + iden
    for (int i = 0; i < 10; i++) {
        result.add(f.tuple(u.atom(i), u.atom(i)));
    }
    assertEquals((new Evaluator(instance)).evaluate(r.reflexiveClosure()), result);
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) Instance(kodkod.instance.Instance) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) Tuple(kodkod.instance.Tuple) HashSet(java.util.HashSet)

Aggregations

Evaluator (kodkod.engine.Evaluator)18 Formula (kodkod.ast.Formula)12 Solution (kodkod.engine.Solution)10 Relation (kodkod.ast.Relation)9 TupleFactory (kodkod.instance.TupleFactory)9 Universe (kodkod.instance.Universe)9 Bounds (kodkod.instance.Bounds)8 Instance (kodkod.instance.Instance)8 TupleSet (kodkod.instance.TupleSet)7 IntExpression (kodkod.ast.IntExpression)6 ArrayList (java.util.ArrayList)4 Expression (kodkod.ast.Expression)3 Solver (kodkod.engine.Solver)3 Tuple (kodkod.instance.Tuple)3 HashSet (java.util.HashSet)2 Variable (kodkod.ast.Variable)2 A4Reporter (edu.mit.csail.sdg.alloy4.A4Reporter)1 UniqueNameGenerator (edu.mit.csail.sdg.alloy4.UniqueNameGenerator)1 Type (edu.mit.csail.sdg.ast.Type)1 File (java.io.File)1