Search in sources :

Example 1 with IdentityHashSet

use of kodkod.util.collections.IdentityHashSet in project org.alloytools.alloy by AlloyTools.

the class Nodes method allRoots.

/**
 * Returns all {@linkplain #roots(Formula) roots} of the given formula such that
 * a node in the given collection is reachable from that root.
 *
 * @return { r: roots(formula) | some r.*components & descendants.elements }
 */
@SuppressWarnings("unchecked")
public static Set<Formula> allRoots(Formula formula, Collection<? extends Node> descendants) {
    final Set<Node> desc = new IdentityHashSet<Node>(descendants);
    final AbstractDetector detector = new AbstractDetector(Collections.EMPTY_SET) {

        @Override
        protected Boolean lookup(Node n) {
            return desc.contains(n) ? Boolean.TRUE : cache.get(n);
        }

        @Override
        protected Boolean cache(Node n, boolean val) {
            final Boolean ret = Boolean.valueOf(val);
            cache.put(n, ret);
            return ret;
        }
    };
    final Set<Formula> roots = new LinkedHashSet<Formula>();
    for (Formula root : roots(formula)) {
        if (root.accept(detector)) {
            roots.add(root);
        }
    }
    return roots;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) AbstractDetector(kodkod.ast.visitor.AbstractDetector) Node(kodkod.ast.Node)

Example 2 with IdentityHashSet

use of kodkod.util.collections.IdentityHashSet 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 3 with IdentityHashSet

use of kodkod.util.collections.IdentityHashSet in project org.alloytools.alloy by AlloyTools.

the class ReductionAndProofTest method core.

private Set<Node> core(Formula formula, int granularity) {
    solver.options().setCoreGranularity(granularity);
    final Solution sol = solver.solve(formula, bounds);
    assertEquals(Solution.Outcome.UNSATISFIABLE, sol.outcome());
    sol.proof().minimize(new NCEStrategy(sol.proof().log()));
    return new IdentityHashSet<Node>(sol.proof().highLevelCore().values());
}
Also used : NCEStrategy(kodkod.engine.ucore.NCEStrategy) IdentityHashSet(kodkod.util.collections.IdentityHashSet) Solution(kodkod.engine.Solution)

Example 4 with IdentityHashSet

use of kodkod.util.collections.IdentityHashSet in project org.alloytools.alloy by AlloyTools.

the class Nodes method minRoots.

/**
 * Returns a minimal subset of {@linkplain #roots(Formula) roots} of the given
 * formula such that all nodes in the given collection are reachable from those
 * roots. The returned subset is a local minimum in that none of its members can
 * be removed without leaving some node in the descendants set unreachable from
 * the remaining roots.
 *
 * @requires descendants in formula.*components
 * @return { s: Set<Formula> | s.elements in roots(formula) and descendants in
 *         s.elements.*components and no s': Set<Formula> | s.containsAll(s')
 *         and s'.size()<s.size() and descendants in s.elements.*components }
 * @throws IllegalArgumentException descendants !in formula.*components
 */
public static Set<Formula> minRoots(Formula formula, Collection<? extends Node> descendants) {
    final Set<Node> desc = new IdentityHashSet<Node>(descendants);
    final VoidVisitor visitor = new AbstractVoidVisitor() {

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

        @Override
        protected boolean visited(Node n) {
            if (visited.add(n)) {
                desc.remove(n);
                return false;
            }
            return true;
        }
    };
    final Set<Formula> roots = new LinkedHashSet<Formula>();
    for (Formula root : roots(formula)) {
        final int size = desc.size();
        root.accept(visitor);
        if (desc.size() < size) {
            roots.add(root);
        }
        if (desc.isEmpty()) {
            break;
        }
    }
    if (!desc.isEmpty())
        throw new IllegalArgumentException("descendants !in formula.*components: formula=" + formula + " ; descendants=" + descendants);
    return roots;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) AbstractVoidVisitor(kodkod.ast.visitor.AbstractVoidVisitor) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) IdentityHashSet(kodkod.util.collections.IdentityHashSet) AbstractSet(java.util.AbstractSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Node(kodkod.ast.Node) AbstractVoidVisitor(kodkod.ast.visitor.AbstractVoidVisitor) VoidVisitor(kodkod.ast.visitor.VoidVisitor)

Example 5 with IdentityHashSet

use of kodkod.util.collections.IdentityHashSet in project org.alloytools.alloy by AlloyTools.

the class AnnotatedNode method relations.

/**
 * Returns the set of all relations at the leaves of this annotated node.
 *
 * @return Relation & this.node.*components
 */
public final Set<Relation> relations() {
    final Set<Relation> relations = new IdentityHashSet<Relation>();
    final AbstractVoidVisitor visitor = new AbstractVoidVisitor() {

        private final Set<Node> visited = new IdentityHashSet<Node>(sharedNodes.size());

        @Override
        protected boolean visited(Node n) {
            return sharedNodes.contains(n) && !visited.add(n);
        }

        @Override
        public void visit(Relation relation) {
            relations.add(relation);
        }
    };
    node.accept(visitor);
    return relations;
}
Also used : IdentityHashSet(kodkod.util.collections.IdentityHashSet) AbstractVoidVisitor(kodkod.ast.visitor.AbstractVoidVisitor) Relation(kodkod.ast.Relation) Set(java.util.Set) IdentityHashSet(kodkod.util.collections.IdentityHashSet) Node(kodkod.ast.Node)

Aggregations

IdentityHashSet (kodkod.util.collections.IdentityHashSet)6 Node (kodkod.ast.Node)4 Set (java.util.Set)3 Formula (kodkod.ast.Formula)3 AbstractVoidVisitor (kodkod.ast.visitor.AbstractVoidVisitor)3 LinkedHashSet (java.util.LinkedHashSet)2 BinaryFormula (kodkod.ast.BinaryFormula)2 NaryFormula (kodkod.ast.NaryFormula)2 Solution (kodkod.engine.Solution)2 AbstractSet (java.util.AbstractSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Relation (kodkod.ast.Relation)1 AbstractDetector (kodkod.ast.visitor.AbstractDetector)1 VoidVisitor (kodkod.ast.visitor.VoidVisitor)1 RecordFilter (kodkod.engine.fol2sat.RecordFilter)1 TranslationRecord (kodkod.engine.fol2sat.TranslationRecord)1 NCEStrategy (kodkod.engine.ucore.NCEStrategy)1 TupleSet (kodkod.instance.TupleSet)1 IntSet (kodkod.util.ints.IntSet)1