Skip to content

Commit

Permalink
Merge pull request #41561 from adripo/depgraph-methods
Browse files Browse the repository at this point in the history
feat: getAllDependents & getAllDependencies
  • Loading branch information
azinneera authored Nov 23, 2023
2 parents a430acd + b20b0ea commit 1c44405
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. When sendin

### Language

-
- [Add getAllDependents and getAllDependencies method to the DependencyGraph class](https://github.com/ballerina-platform/ballerina-lang/pull/41561)
-
-
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public T getRoot() {
return rootNode;
}

// Returns all direct and indirect dependents of the node T
public Collection<T> getAllDependents(T node) {
Set<T> allDependents = new HashSet<>();
Set<T> visited = new HashSet<>();
getAllDependentsRecursive(node, allDependents, visited);
return allDependents;
}

// Returns all direct and indirect dependencies of node T
public Collection<T> getAllDependencies(T node) {
Set<T> allDependencies = new HashSet<>();
Set<T> visited = new HashSet<>();
getAllDependenciesRecursive(node, allDependencies, visited);
return allDependencies;
}

public boolean contains(T node) {
return dependencies.containsKey(node);
}
Expand Down Expand Up @@ -192,6 +208,30 @@ private void sortTopologically(T vertex, List<T> visited, List<T> ancestors, Lis
ancestors.remove(vertex);
}

private void getAllDependentsRecursive(T node, Set<T> allDependents, Set<T> visited) {
visited.add(node);
Collection<T> directDependents = getDirectDependents(node);
allDependents.addAll(directDependents);

for (T dependent : directDependents) {
if (!visited.contains(dependent)) {
getAllDependentsRecursive(dependent, allDependents, visited);
}
}
}

private void getAllDependenciesRecursive(T node, Set<T> allDependencies, Set<T> visited) {
visited.add(node);
Collection<T> directDependencies = getDirectDependencies(node);
allDependencies.addAll(directDependencies);

for (T dependency : directDependencies) {
if (!visited.contains(dependency)) {
getAllDependenciesRecursive(dependency, allDependencies, visited);
}
}
}

/**
* Builds a {@code DependencyGraph}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -628,6 +630,56 @@ public void testVersionResolutionHARD() {
ResolutionResponse.ResolutionStatus.RESOLVED);
}

@Test
public void testGetAllDependents() {
// Create a sample DependencyGraph with String nodes
DependencyGraph<String> dependencyGraph = DependencyGraph.from(new LinkedHashMap<>() {{
put("A", new LinkedHashSet<>() {{
add("B");
add("C");
}});
put("B", new LinkedHashSet<>() {{
add("D");
}});
put("C", new LinkedHashSet<>());
put("D", new LinkedHashSet<>());
put("E", new LinkedHashSet<>() {{
add("F");
}});
put("F", new LinkedHashSet<>());
}});

Collection<String> allDependents = dependencyGraph.getAllDependents("D");
Set<String> expectedDependents = new HashSet<>(Arrays.asList("A", "B"));

Assert.assertEquals(expectedDependents, allDependents);
}

@Test
public void testGetAllDependencies() {
// Create a sample DependencyGraph with String nodes
DependencyGraph<String> dependencyGraph = DependencyGraph.from(new LinkedHashMap<>() {{
put("A", new LinkedHashSet<>() {{
add("B");
add("C");
}});
put("B", new LinkedHashSet<>() {{
add("D");
}});
put("C", new LinkedHashSet<>());
put("D", new LinkedHashSet<>());
put("E", new LinkedHashSet<>() {{
add("F");
}});
put("F", new LinkedHashSet<>());
}});

Collection<String> allDependencies = dependencyGraph.getAllDependencies("A");
Set<String> expectedDependencies = new HashSet<>(Arrays.asList("B", "C", "D"));

Assert.assertEquals(expectedDependencies, allDependencies);
}

@Test
public void testTopologicalSortOfModuleDescriptor() {
PackageName packageName = PackageName.from("package");
Expand Down

0 comments on commit 1c44405

Please sign in to comment.