Search in sources :

Example 1 with Int

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(project) 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(project) | some t => t, cache(project,
 *         project.expression.accept(this).project(translate(project.columns))
 */
@Override
public final BooleanMatrix visit(ProjectExpression project) {
    BooleanMatrix ret = lookup(project);
    if (ret != null)
        return ret;
    final Int[] cols = new Int[project.arity()];
    for (int i = 0, arity = project.arity(); i < arity; i++) {
        cols[i] = project.column(i).accept(this);
    }
    return cache(project, project.expression().accept(this).project(cols));
}
Also used : BooleanMatrix(kodkod.engine.bool.BooleanMatrix) Int(kodkod.engine.bool.Int)

Example 2 with Int

use of kodkod.engine.bool.Int 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,
 *         intExpr.op(intExpr.expression.accept(this)))
 */
@Override
public final Int visit(UnaryIntExpression intExpr) {
    Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final Int child = intExpr.intExpr().accept(this);
    switch(intExpr.op()) {
        case NEG:
            ret = child.negate();
            break;
        case NOT:
            ret = child.not();
            break;
        case ABS:
            ret = child.abs();
            break;
        case SGN:
            ret = child.sgn();
            break;
        default:
            throw new IllegalArgumentException("Unknown operator: " + intExpr.op());
    }
    return cache(intExpr, ret);
}
Also used : Int(kodkod.engine.bool.Int)

Example 3 with Int

use of kodkod.engine.bool.Int 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 4 with Int

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

the class Evaluator method evaluate.

/**
 * Evaluates the specified int expession with respect to the relation-tuple
 * mappings given by this.instance and using this.options.
 *
 * @return the integer to which the expression evaluates given the mappings in
 *         this.instance and the options in this.options.
 * @throws kodkod.engine.fol2sat.HigherOrderDeclException intExpr contains a
 *             higher order declaration
 * @throws kodkod.engine.fol2sat.UnboundLeafException intExpr contains an
 *             undeclared variable or a relation not mapped by this.instance
 */
public int evaluate(IntExpression intExpr) {
    if (intExpr == null)
        throw new NullPointerException("intexpression");
    final Int sol = Translator.evaluate(intExpr, instance, options);
    this.wasOverflow = sol.defCond().getAccumOverflow() == BooleanConstant.TRUE;
    return sol.value();
}
Also used : Int(kodkod.engine.bool.Int)

Example 5 with Int

use of kodkod.engine.bool.Int 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)

Aggregations

Int (kodkod.engine.bool.Int)12 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)4 BooleanValue (kodkod.engine.bool.BooleanValue)4 BooleanFactory (kodkod.engine.bool.BooleanFactory)2 IntSet (kodkod.util.ints.IntSet)2 ArrayList (java.util.ArrayList)1 Decl (kodkod.ast.Decl)1 Variable (kodkod.ast.Variable)1 IntIterator (kodkod.util.ints.IntIterator)1