Skip to content

Commit

Permalink
Refactor the StaticValidator to reuse code in DependencyGraphs.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLandau committed Oct 27, 2013
1 parent f0e2868 commit cc06e51
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 110 deletions.
36 changes: 31 additions & 5 deletions src/org/ggp/base/util/gdl/model/DependencyGraphs.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,66 @@
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;

/**
* When dealing with GDL, dependency graphs are often useful. DependencyGraphs
* offers a variety of functionality for dealing with dependency graphs expressed
* in the form of SetMultimaps.
*
* These multimaps are paired with sets of all nodes, to account for the
* possibility of nodes not included in the multimap representation.
*
* All methods assume that keys in multimaps depend on their associated values,
* or in other words are downstream of or are children of those values.
*/
public class DependencyGraphs {
private DependencyGraphs() {}

/**
* Returns all elements of the dependency graph that match the
* given predicate, and any elements downstream of those matching
* given predicate, and any elements upstream of those matching
* elements.
*
* The graph may contain cycles.
*
* Each key in the dependency graph depends on/is downstream of
* its associated values.
*/
public static <T> ImmutableSet<T> getMatchingAndDownstream(
public static <T> ImmutableSet<T> getMatchingAndUpstream(
Set<T> allNodes,
SetMultimap<T, T> dependencyGraph,
Predicate<T> matcher) {
Set<T> results = Sets.newHashSet();

SetMultimap<T, T> reversedGraph = reverseGraph(dependencyGraph);

Deque<T> toTry = Queues.newArrayDeque();
toTry.addAll(Collections2.filter(allNodes, matcher));

while (!toTry.isEmpty()) {
T curElem = toTry.remove();
if (!results.contains(curElem)) {
results.add(curElem);
toTry.addAll(reversedGraph.get(curElem));
toTry.addAll(dependencyGraph.get(curElem));
}
}
return ImmutableSet.copyOf(results);
}

/**
* Returns all elements of the dependency graph that match the
* given predicate, and any elements downstream of those matching
* elements.
*
* The graph may contain cycles.
*
* Each key in the dependency graph depends on/is downstream of
* its associated values.
*/
public static <T> ImmutableSet<T> getMatchingAndDownstream(
Set<T> allNodes,
SetMultimap<T, T> dependencyGraph,
Predicate<T> matcher) {
return getMatchingAndUpstream(allNodes, reverseGraph(dependencyGraph), matcher);
}

public static <T> SetMultimap<T, T> reverseGraph(SetMultimap<T, T> graph) {
return Multimaps.invertFrom(graph, HashMultimap.<T, T>create());
}
Expand Down
Loading

0 comments on commit cc06e51

Please sign in to comment.