Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getAllDependents & getAllDependencies #41561

Merged
merged 7 commits into from
Nov 23, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
-
-
-
Original file line number Diff line number Diff line change
@@ -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);
}
@@ -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}.
*
Original file line number Diff line number Diff line change
@@ -628,6 +628,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("A");
Set<String> expectedDependents = new HashSet<>(Arrays.asList("B", "C", "D"));

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("D");
Set<String> expectedDependencies = new HashSet<>(Arrays.asList("A", "B"));

Assert.assertEquals(expectedDependencies, allDependencies);
}

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