Search in sources :

Example 6 with BooleanMatrix

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(expr) and returns the cached value, if any. If a translation has
 * not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(expr) | some t => t, let op = (expr.op).(UNION->or +
 *         INTERSECTION->and + DIFFERENCE->difference + OVERRIDE->override +
 *         JOIN->dot + PRODUCT->cross) | cache(expr, op(expr.left.accept(this),
 *         expr.right.accept(this)))
 */
@Override
public BooleanMatrix visit(NaryExpression expr) {
    BooleanMatrix ret = lookup(expr);
    if (ret != null)
        return ret;
    final ExprOperator op = expr.op();
    final BooleanMatrix first = expr.child(0).accept(this);
    final BooleanMatrix[] rest = new BooleanMatrix[expr.size() - 1];
    for (int i = 0; i < rest.length; i++) {
        rest[i] = expr.child(i + 1).accept(this);
    }
    switch(op) {
        case UNION:
            ret = first.or(rest);
            break;
        case INTERSECTION:
            ret = first.and(rest);
            break;
        case OVERRIDE:
            ret = first.override(rest);
            break;
        case PRODUCT:
            ret = first.cross(rest);
            break;
        default:
            throw new IllegalArgumentException("Unknown associative operator: " + op);
    }
    return cache(expr, ret);
}
Also used : BooleanMatrix(kodkod.engine.bool.BooleanMatrix) ExprOperator(kodkod.ast.operator.ExprOperator)

Example 7 with BooleanMatrix

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(castExpr) and returns the cached value, if any. If a translation
 * has not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(castExpr) | some t => t, cache(castExpr,
 *         translate(castExpr))
 */
@Override
public BooleanMatrix visit(IntToExprCast castExpr) {
    BooleanMatrix ret = lookup(castExpr);
    if (ret != null)
        return ret;
    final Int child = castExpr.intExpr().accept(this);
    final BooleanFactory factory = interpreter.factory();
    final IntSet ints = interpreter.ints();
    ret = factory.matrix(Dimensions.square(interpreter.universe().size(), 1));
    switch(castExpr.op()) {
        case INTCAST:
            for (IntIterator iter = ints.iterator(); iter.hasNext(); ) {
                int i = iter.next();
                int atomIndex = interpreter.interpret(i);
                ret.set(atomIndex, factory.or(ret.get(atomIndex), child.eq(factory.integer(i))));
            }
            ret.setDefCond(child.defCond());
            break;
        case BITSETCAST:
            final List<BooleanValue> twosComplement = child.twosComplementBits();
            final int msb = twosComplement.size() - 1;
            // handle all bits but the sign bit
            for (int i = 0; i < msb; i++) {
                int pow2 = 1 << i;
                if (ints.contains(pow2)) {
                    ret.set(interpreter.interpret(pow2), twosComplement.get(i));
                }
            }
            // handle the sign bit
            if (ints.contains(-1 << msb)) {
                ret.set(interpreter.interpret(-1 << msb), twosComplement.get(msb));
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown cast operator: " + castExpr.op());
    }
    return cache(castExpr, ret);
}
Also used : IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet) BooleanValue(kodkod.engine.bool.BooleanValue) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) Int(kodkod.engine.bool.Int) BooleanFactory(kodkod.engine.bool.BooleanFactory)

Example 8 with BooleanMatrix

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(ifExpr) and returns the cached value, if any. If a translation
 * has not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(ifExpr) | some t => t, cache(ifExpr,
 *         ifExpr.condition.accept(this).choice(ifExpr.then.accept(this),
 *         ifExpr.else.accept(this)))
 */
@Override
public BooleanMatrix visit(IfExpression ifExpr) {
    BooleanMatrix ret = lookup(ifExpr);
    if (ret != null)
        return ret;
    final BooleanValue condition = ifExpr.condition().accept(this);
    final BooleanMatrix thenExpr = ifExpr.thenExpr().accept(this);
    final BooleanMatrix elseExpr = ifExpr.elseExpr().accept(this);
    ret = thenExpr.choice(condition, elseExpr);
    return cache(ifExpr, ret);
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) BooleanMatrix(kodkod.engine.bool.BooleanMatrix)

Example 9 with BooleanMatrix

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

the class BooleanMatrixTest method testCrossProduct.

public final void testCrossProduct() {
    final BooleanMatrix mT43 = mF43.not();
    fill(mT43, range(0, dim43.capacity() - 1));
    fill(mF4, range(0, dim4.capacity() - 1));
    assertCrossProductFalse(mF324, mT43);
    assertCrossProductFalse(mF324, mF4);
    fill(mT324, range(0, dim324.capacity() - 1));
    BooleanValue[] result = new BooleanValue[dim324.cross(dim43).capacity()];
    Arrays.fill(result, TRUE);
    final int c324 = dim324.capacity(), c43 = dim43.capacity();
    final int c32443 = c324 * c43;
    for (int i = 0; i < c32443; i++) {
        result[i] = f.and(mT324.get(i / c43), mT43.get(i % c43));
    }
    assertTrue(equivalent(mT324.cross(mT43), result));
    mT324.set(1, TRUE);
    for (int i = c43; i < c43 * 2; i++) {
        result[i] = mT43.get(i % c43);
    }
    assertTrue(equivalent(mT324.cross(mT43), result));
    mT43.set(5, TRUE);
    for (int i = 0; i < c324; i++) {
        result[i * c43 + 5] = mT324.get(i);
    }
    assertTrue(equivalent(mT324.cross(mT43), result));
    mT324.set(10, FALSE);
    for (int i = c43 * 10; i < c43 * 11; i++) {
        result[i] = FALSE;
    }
    assertTrue(equivalent(mT324.cross(mT43), result));
// System.out.println(Arrays.asList(result));
// System.out.println(mT324.crossProduct(mT43));
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) BooleanMatrix(kodkod.engine.bool.BooleanMatrix)

Example 10 with BooleanMatrix

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

the class BooleanMatrixTest method testOverride.

public final void testOverride() {
    assertTrue(equivalent(mT324.override(mT324), mT324));
    assertTrue(equivalent(mT324.override(mF324), mT324));
    assertTrue(equivalent(mF324.override(mT324), mT324));
    assertTrue(equivalent(mF324.override(mF324), mF324));
    final BooleanMatrix mF324c = mF324.clone(), mT324c = mT324.clone();
    mF324.set(3, vars[3]);
    mF324.set(17, vars[17]);
    mF324.set(22, vars[22]);
    assertTrue(equivalent(mF324.override(mF324c), mF324));
    assertTrue(equivalent(mF324.override(mT324), mT324));
    mF324c.set(9, vars[9]);
    assertTrue(equivalent(mF324.override(mF324c), mF324.or(mF324c)));
    mT324.set(0, BooleanConstant.FALSE);
    assertTrue(equivalent(mF324.override(mT324), mT324));
    assertTrue(equivalent(mT324.override(mT324c), mT324c));
    assertTrue(equivalent(mT324c.override(mT324), mT324));
    final BooleanMatrix mFoF = f.matrix(dim324);
    mF324.set(10, vars[10]);
    mF324c.set(3, vars[4]);
    mF324c.set(20, vars[20]);
    mF324c.set(19, vars[19]);
    mFoF.set(3, f.or(vars[4], f.and(vars[3], f.not(vars[4]))));
    mFoF.set(9, vars[9]);
    mFoF.set(10, f.and(vars[10], f.not(vars[9])));
    mFoF.set(17, f.and(vars[17], f.and(f.not(vars[19]), f.not(vars[20]))));
    mFoF.set(22, f.and(vars[22], f.and(f.not(vars[19]), f.not(vars[20]))));
    mFoF.set(20, vars[20]);
    mFoF.set(19, vars[19]);
    assertTrue(equivalent(mF324.override(mF324c), mFoF));
    mT324.set(3, vars[4]);
    mT324.set(11, vars[11]);
    for (int i = 16; i < 24; i++) mT324.set(i, vars[i - 16]);
    final BooleanMatrix mFoT = f.matrix(dim324);
    for (int i = 0; i < 16; i++) mFoT.set(i, mT324.get(i));
    final BooleanAccumulator g = BooleanAccumulator.treeGate(Operator.AND);
    for (int i = 0; i < 8; i++) g.add(f.not(vars[i]));
    final BooleanValue v3 = f.accumulate(g);
    for (int i = 16; i < 24; i++) mFoT.set(i, f.or(f.and(v3, mF324.get(i)), mT324.get(i)));
    assertTrue(equivalent(mF324.override(mT324), mFoT));
    final BooleanMatrix mToF = f.matrix(dim324);
    for (int i = 0; i < 8; i++) mToF.set(i, f.or(mF324.get(i), f.and(mT324.get(i), f.not(vars[3]))));
    for (int i = 8; i < 16; i++) mToF.set(i, f.or(mF324.get(i), f.and(mT324.get(i), f.not(vars[10]))));
    for (int i = 16; i < 24; i++) mToF.set(i, f.or(mF324.get(i), f.and(mT324.get(i), f.and(f.not(vars[17]), f.not(vars[22])))));
    assertTrue(equivalent(mT324.override(mF324), mToF));
    final BooleanMatrix mToT = f.matrix(dim324).not();
    mT324c.set(11, vars[12]);
    mT324c.set(12, vars[13]);
    mT324c.set(18, vars[18]);
    for (int i = 0; i < 16; i++) mToT.set(i, mT324.get(i));
    for (int i = 16; i < 24; i++) mToT.set(i, f.or(mT324.get(i), f.and(mT324c.get(i), v3)));
    assertTrue(equivalent(mT324c.override(mT324), mToT));
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) BooleanAccumulator(kodkod.engine.bool.BooleanAccumulator) BooleanMatrix(kodkod.engine.bool.BooleanMatrix)

Aggregations

BooleanMatrix (kodkod.engine.bool.BooleanMatrix)30 BooleanValue (kodkod.engine.bool.BooleanValue)13 BooleanFactory (kodkod.engine.bool.BooleanFactory)7 Decl (kodkod.ast.Decl)5 IntSet (kodkod.util.ints.IntSet)5 Int (kodkod.engine.bool.Int)4 ExprOperator (kodkod.ast.operator.ExprOperator)3 IntIterator (kodkod.util.ints.IntIterator)3 BooleanAccumulator (kodkod.engine.bool.BooleanAccumulator)2 ArrayList (java.util.ArrayList)1 BinaryExpression (kodkod.ast.BinaryExpression)1 Comprehension (kodkod.ast.Comprehension)1 Expression (kodkod.ast.Expression)1 IfExpression (kodkod.ast.IfExpression)1 IntExpression (kodkod.ast.IntExpression)1 IntToExprCast (kodkod.ast.IntToExprCast)1 Relation (kodkod.ast.Relation)1 SumExpression (kodkod.ast.SumExpression)1 Variable (kodkod.ast.Variable)1 ExprCompOperator (kodkod.ast.operator.ExprCompOperator)1