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;
}
Aggregations