Search in sources :

Example 1 with AbstractDetector

use of kodkod.ast.visitor.AbstractDetector 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)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)1 BinaryFormula (kodkod.ast.BinaryFormula)1 Formula (kodkod.ast.Formula)1 NaryFormula (kodkod.ast.NaryFormula)1 Node (kodkod.ast.Node)1 AbstractDetector (kodkod.ast.visitor.AbstractDetector)1 IdentityHashSet (kodkod.util.collections.IdentityHashSet)1