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