// TODO(leeca): Move this to
// RelationFinder.computeSuccessorHierarchy(Collection<Edge<? extends Element>>)
public Map<GraphNode, ? extends SuccessorEdges>
computeSpanningHierarchy(DirectedRelationFinder relations) {
SuccessorsMap builder = new SuccessorsMap();
Set<GraphNode> visited = Sets.newHashSet();
// Only include nodes that participate in the relations.
for (GraphEdge edge : getEdges()) {
// On forward matches, include the link only
// if the tail has not yet been visited.
if (relations.matchForward(edge.getRelation())) {
if (false == visited.contains((edge.getTail()))) {
builder.addForwardEdge(edge);
visited.add((edge.getTail()));
}
}
// For spanning hierarchies, each edge gets added only once.
// And the forward direction is preferred if both are allowed.
// On reverse matches, include the link only
// if the head has not yet been visited.
else if (relations.matchBackward(edge.getRelation())) {
if (false == visited.contains((edge.getHead()))) {
builder.addReverseEdge(edge);
visited.add((edge.getHead()));
}
}
}
return builder.getSuccessorMap();
}