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
Show file tree
Hide file tree
Changes from all 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
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 @@
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;

Check warning on line 137 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L134-L137

Added lines #L134 - L137 were not covered by tests
}

// 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;

Check warning on line 145 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L142-L145

Added lines #L142 - L145 were not covered by tests
}

public boolean contains(T node) {
return dependencies.containsKey(node);
}
Expand Down Expand Up @@ -192,6 +208,30 @@
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);

Check warning on line 214 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L212-L214

Added lines #L212 - L214 were not covered by tests

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

Check warning on line 218 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L218

Added line #L218 was not covered by tests
}
}
}

Check warning on line 221 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L220-L221

Added lines #L220 - L221 were not covered by tests

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

Check warning on line 226 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L224-L226

Added lines #L224 - L226 were not covered by tests

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

Check warning on line 230 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L230

Added line #L230 was not covered by tests
}
}
}

Check warning on line 233 in compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java#L232-L233

Added lines #L232 - L233 were not covered by tests

/**
* 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
Loading