Search in sources :

Example 16 with BooleanMatrix

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

the class BooleanMatrixTest method testClosure.

public final void testClosure() {
    BooleanMatrix mF44 = f.matrix(Dimensions.square(4, 2));
    assertTrue(equivalent(mF44, mF44.closure()));
    mF44.set(0, vars[0]);
    mF44.set(9, vars[9]);
    assertTrue(equivalent(mF44, mF44.closure()));
    mF44.set(2, vars[2]);
    BooleanValue[] result = new BooleanValue[mF44.dimensions().capacity()];
    for (int i = 0; i < result.length; i++) {
        result[i] = FALSE;
    }
    result[0] = vars[0];
    result[1] = f.and(vars[2], vars[9]);
    result[1] = f.or(result[1], f.and(vars[0], result[1]));
    result[2] = vars[2];
    result[9] = vars[9];
    assertTrue(equivalent(mF44.closure(), result));
    mF44.set(7, vars[7]);
    result[7] = vars[7];
    result[3] = f.and(vars[2], f.and(vars[9], vars[7]));
    result[11] = f.and(vars[7], vars[9]);
    assertTrue(equivalent(mF44.closure(), result));
// System.out.println(mF44.closure());
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) BooleanMatrix(kodkod.engine.bool.BooleanMatrix)

Example 17 with BooleanMatrix

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(intExpr) 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(intExpr) | some t => t, cache(intExpr,
 *         translate(intExpr))
 */
@Override
public final Int visit(ExprToIntCast intExpr) {
    Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    vars = vars.createNested();
    BooleanMatrix expr = intExpr.expression().accept(this);
    switch(intExpr.op()) {
        case CARDINALITY:
            ret = expr.cardinality();
            break;
        case SUM:
            final IntSet ints = interpreter.ints();
            ret = sum(expr, ints.iterator(), 0, ints.size() - 1);
            break;
        default:
            throw new IllegalArgumentException("unknown operator: " + intExpr.op());
    }
    for (Variable v : vars) ret.defCond().addVar(v);
    vars = vars.parent();
    return cache(intExpr, ret);
}
Also used : Variable(kodkod.ast.Variable) IntSet(kodkod.util.ints.IntSet) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) Int(kodkod.engine.bool.Int)

Example 18 with BooleanMatrix

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(binExpr) 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(binExpr) | some t => t, let op =
 *         (binExpr.op).(UNION->or + INTERSECTION->and + DIFFERENCE->difference
 *         + OVERRIDE->override + JOIN->dot + PRODUCT->cross) | cache(binExpr,
 *         op(binExpr.left.accept(this), binExpr.right.accept(this)))
 */
@Override
public BooleanMatrix visit(BinaryExpression binExpr) {
    BooleanMatrix ret = lookup(binExpr);
    if (ret != null)
        return ret;
    final BooleanMatrix left = binExpr.left().accept(this);
    final BooleanMatrix right = binExpr.right().accept(this);
    final ExprOperator op = binExpr.op();
    switch(op) {
        case UNION:
            ret = left.or(right);
            break;
        case INTERSECTION:
            ret = left.and(right);
            break;
        case DIFFERENCE:
            ret = left.difference(right);
            break;
        case OVERRIDE:
            ret = left.override(right);
            break;
        case JOIN:
            ret = left.dot(right);
            break;
        case PRODUCT:
            ret = left.cross(right);
            break;
        default:
            throw new IllegalArgumentException("Unknown operator: " + op);
    }
    return cache(binExpr, ret);
}
Also used : BooleanMatrix(kodkod.engine.bool.BooleanMatrix) ExprOperator(kodkod.ast.operator.ExprOperator)

Example 19 with BooleanMatrix

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

the class FOL2BoolTranslator method some.

/**
 * Translates the given existentially quantified formula as follows (where
 * A_0...A_|A| stand for boolean variables that represent the tuples of the
 * expression A, etc.): let quantFormula = "some a: A, b: B, ..., x: X | F(a, b,
 * ..., x)" | (A_0 && B_0 && ... && X_0 && translate(F(A_0, B_0, ..., X_0))) ||
 * ... || (A_|A| && B_|B| && ... && X_|X| && translate(F(A_|A|, B_|B|, ...,
 * X_|X|)) If the noOverflow option is specified, then the translation looks
 * like: let quantFormula = "some a: A, b: B, ..., x: X | F(a, b, ..., x)" |
 * (A_0 && B_0 && ... && X_0 && !of(F(A_0, B_0, ..., X_0)) && translate(F(A_0,
 * B_0, ..., X_0))) || ... || (A_|A| && B_|B| && ... && X_|X| && !of(F(A_|A|,
 * B_|B|, ..., X_|X|)) && translate(F(A_|A|, B_|B|, ..., X_|X|)) where
 * of(F(A_|a|, B_|b|, ..., X_|x|)) is the portion of the overflow circuit
 * generated by the translation of F(A_|a|, B_|b|, ..., X_|x|) contributed by
 * arithmetic operations over only the integer variables of this quantifier
 *
 * @param decls formula declarations
 * @param formula the formula body
 * @param currentDecl currently processed declaration; should be 0 initially
 * @param declConstraints the constraints implied by the declarations; should be
 *            Boolean.TRUE intially
 * @param acc the accumulator that contains the top level conjunction; should be
 *            an empty OR accumulator initially
 * @ensures the given accumulator contains the translation of the formula "some
 *          decls | formula"
 */
private void some(Decls decls, Formula formula, int currentDecl, BooleanValue declConstraints, BooleanAccumulator acc) {
    if (acc.isShortCircuited())
        return;
    final BooleanFactory factory = interpreter.factory();
    if (decls.size() == currentDecl) {
        BooleanValue formulaCircuit = formula.accept(this);
        BooleanValue finalCircuit = factory.and(declConstraints, formulaCircuit);
        acc.add(finalCircuit);
        return;
    }
    final Decl decl = decls.get(currentDecl);
    final BooleanMatrix declTransl = visit(decl);
    final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
    env = env.extend(decl.variable(), decl.expression(), groundValue, Quantifier.SOME);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        some(decls, formula, currentDecl + 1, factory.and(entry.value(), declConstraints), acc);
        groundValue.set(entry.index(), BooleanConstant.FALSE);
    }
    env = env.parent();
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) Decl(kodkod.ast.Decl) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) BooleanFactory(kodkod.engine.bool.BooleanFactory)

Example 20 with BooleanMatrix

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

the class FOL2BoolTranslator method sum.

/**
 * Translates the given sum expression as follows (where A_0...A_|A| stand for
 * boolean variables that represent the tuples of the expression A, etc.): let
 * sum = "sum a: A, b: B, ..., x: X | IE(a, b, ..., x) " | sum a: A, b: B, ...,
 * x: X | if (a in A && b in B && ... && x in X) then IE(a, b, ..., x) else 0 }.
 *
 * @param decls intexpr declarations
 * @param formula the formula body
 * @param currentDecl currently processed declaration; should be 0 initially
 * @param declConstraints the constraints implied by the declarations; should be
 *            Boolean.TRUE intially
 * @param values integer values computed so far
 */
private final void sum(Decls decls, IntExpression expr, int currentDecl, BooleanValue declConstraints, List<Int> values) {
    final BooleanFactory factory = interpreter.factory();
    if (decls.size() == currentDecl) {
        Int intExpr = expr.accept(this);
        Int newInt = intExpr.choice(declConstraints, factory.integer(0));
        values.add(newInt);
        return;
    }
    final Decl decl = decls.get(currentDecl);
    final BooleanMatrix declTransl = visit(decl);
    final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
    env = env.extend(decl.variable(), decl.expression(), groundValue);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        sum(decls, expr, currentDecl + 1, factory.and(entry.value(), declConstraints), values);
        groundValue.set(entry.index(), BooleanConstant.FALSE);
    }
    env = env.parent();
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) Decl(kodkod.ast.Decl) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) BooleanFactory(kodkod.engine.bool.BooleanFactory) Int(kodkod.engine.bool.Int)

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