Search in sources :

Example 21 with IntSet

use of kodkod.util.ints.IntSet 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 22 with IntSet

use of kodkod.util.ints.IntSet in project org.alloytools.alloy by AlloyTools.

the class ResolutionBasedProof method highLevelCore.

/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.Proof#highLevelCore()
 */
@Override
public final Map<Formula, Node> highLevelCore() {
    if (coreRoots == null) {
        final RecordFilter unitFilter = new RecordFilter() {

            final IntSet coreUnits = StrategyUtils.coreUnits(solver.proof());

            final Set<Formula> roots = log().roots();

            @Override
            public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
                return roots.contains(translated) && coreUnits.contains(Math.abs(literal));
            }
        };
        coreRoots = new LinkedHashMap<Formula, Node>();
        final IntSet seenUnits = new IntTreeSet();
        for (Iterator<TranslationRecord> itr = log().replay(unitFilter); itr.hasNext(); ) {
            // it is possible that two top-level formulas have identical
            // meaning,
            // and are represented with the same core unit; in that case, we
            // want only
            // one of them in the core.
            final TranslationRecord rec = itr.next();
            if (seenUnits.add(rec.literal())) {
                coreRoots.put(rec.translated(), rec.node());
            }
        }
        coreRoots = Collections.unmodifiableMap(coreRoots);
    }
    return coreRoots;
}
Also used : Formula(kodkod.ast.Formula) IntTreeSet(kodkod.util.ints.IntTreeSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) IntSet(kodkod.util.ints.IntSet) Set(java.util.Set) TupleSet(kodkod.instance.TupleSet) IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 23 with IntSet

use of kodkod.util.ints.IntSet in project org.alloytools.alloy by AlloyTools.

the class ResolutionBasedProof method connectedCore.

/**
 * Returns the connected core based on the given set of core variables.
 *
 * @requires coreVar = StrategyUtils.coreVars(solver.proof());
 * @return let formulas = (this.log.records[int] & literal.{i: int | abs(i) in
 *         coreVars}).formula | connected = {f: formulas | some s: set coreNodes
 *         | f + this.log.formula in s and (s - this.log.formula).~components in
 *         s }
 */
private Set<Formula> connectedCore(final IntSet coreVars) {
    final Set<Formula> coreNodes = new IdentityHashSet<Formula>();
    final RecordFilter filter = new RecordFilter() {

        @Override
        public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
            return coreVars.contains(StrictMath.abs(literal));
        }
    };
    for (Iterator<TranslationRecord> itr = log().replay(filter); itr.hasNext(); ) {
        coreNodes.add(itr.next().translated());
    }
    final Set<Formula> connected = new IdentityHashSet<Formula>();
    final AbstractVoidVisitor traverser = new AbstractVoidVisitor() {

        final Set<Node> visited = new IdentityHashSet<Node>();

        /**
         * Returns true if the given node has been visited before or if it is not
         * contained in this.nodes set. Otherwise adds the node to the connected set and
         * returns false.
         *
         * @ensures this.visited' = this.visited + n
         * @ensures n !in this.visited && n in coreNodes => connected' = connected + n
         *          else connected' = connected
         * @return n in visited || n !in coreNodes
         */
        @Override
        protected boolean visited(Node n) {
            if (visited.add(n) && coreNodes.contains(n)) {
                connected.add((Formula) n);
                return false;
            }
            return true;
        }
    };
    for (Formula root : log().roots()) {
        root.accept(traverser);
    }
    return connected;
}
Also used : Formula(kodkod.ast.Formula) IdentityHashSet(kodkod.util.collections.IdentityHashSet) AbstractVoidVisitor(kodkod.ast.visitor.AbstractVoidVisitor) IntTreeSet(kodkod.util.ints.IntTreeSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) IntSet(kodkod.util.ints.IntSet) Set(java.util.Set) TupleSet(kodkod.instance.TupleSet) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 24 with IntSet

use of kodkod.util.ints.IntSet 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 25 with IntSet

use of kodkod.util.ints.IntSet in project org.alloytools.alloy by AlloyTools.

the class LeafInterpreter method interpret.

/**
 * Returns a {@link kodkod.engine.bool.BooleanMatrix matrix} m of
 * {@link kodkod.engine.bool.BooleanValue boolean formulas} representing the
 * specified relation.
 *
 * @requires r in this.relations
 * @return { m: BooleanMatrix | let lset =
 *         (this.rBounds[r].TupleSet).tuples.index, hset =
 *         (this.rBounds[r][TupleSet]).tuples.index, dset =
 *         [0..this.universe.size()^r.arity) | m.dimensions.dimensions =
 *         [0..r.arity) ->one this.universe.size() && m.elements[lset] = TRUE &&
 *         m.elements[dset-hset] = FALSE && all disj i, j: hset-lset |
 *         m.elements[i]+m.elements[j] in this.vars[r] && m.elements[i].label <
 *         m.elements[j].label <=> i < j }
 * @throws UnboundLeafException r !in this.relations
 */
public final BooleanMatrix interpret(Relation r) {
    if (!lowers.containsKey(r))
        throw new UnboundLeafException("Unbound relation: ", r);
    final IntSet lowerBound = lowers.get(r).indexView();
    final IntSet upperBound = uppers.get(r).indexView();
    final BooleanMatrix m = factory.matrix(Dimensions.square(universe().size(), r.arity()), upperBound, lowerBound);
    if (upperBound.size() > lowerBound.size()) {
        int varId = vars.get(r).min();
        for (IntIterator indeces = upperBound.iterator(); indeces.hasNext(); ) {
            int tupleIndex = indeces.next();
            if (!lowerBound.contains(tupleIndex))
                m.set(tupleIndex, factory.variable(varId++));
        }
    }
    return m;
}
Also used : IntIterator(kodkod.util.ints.IntIterator) IntSet(kodkod.util.ints.IntSet) BooleanMatrix(kodkod.engine.bool.BooleanMatrix)

Aggregations

IntSet (kodkod.util.ints.IntSet)45 IntIterator (kodkod.util.ints.IntIterator)21 IntBitSet (kodkod.util.ints.IntBitSet)9 IntTreeSet (kodkod.util.ints.IntTreeSet)8 Relation (kodkod.ast.Relation)7 Clause (kodkod.engine.satlab.Clause)6 TupleSet (kodkod.instance.TupleSet)6 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)5 Map (java.util.Map)4 Set (java.util.Set)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Formula (kodkod.ast.Formula)3 Node (kodkod.ast.Node)3 BooleanFactory (kodkod.engine.bool.BooleanFactory)3 RecordFilter (kodkod.engine.fol2sat.RecordFilter)3 TranslationRecord (kodkod.engine.fol2sat.TranslationRecord)3 Bounds (kodkod.instance.Bounds)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2