From 5608d82666ad4e8e738e540e1fc39c5ee3da4290 Mon Sep 17 00:00:00 2001 From: adripo <26493496+adripo@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:39:09 +0200 Subject: [PATCH 1/5] feat: getAllDependents & getAllDependencies --- .../ballerina/projects/DependencyGraph.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java index 784ec17c2697..9bfe85bf9466 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DependencyGraph.java @@ -129,6 +129,22 @@ public T getRoot() { return rootNode; } + // Returns all direct and indirect dependents of the node T + public Collection getAllDependents(T node) { + Set allDependents = new HashSet<>(); + Set visited = new HashSet<>(); + getAllDependentsRecursive(node, allDependents, visited); + return allDependents; + } + + // Returns all direct and indirect dependencies of node T + public Collection getAllDependencies(T node) { + Set allDependencies = new HashSet<>(); + Set 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 visited, List ancestors, Lis ancestors.remove(vertex); } + private void getAllDependentsRecursive(T node, Set allDependents, Set visited) { + visited.add(node); + Collection directDependents = getDirectDependents(node); + allDependents.addAll(directDependents); + + for (T dependent : directDependents) { + if (!visited.contains(dependent)) { + getAllDependentsRecursive(dependent, allDependents, visited); + } + } + } + + private void getAllDependenciesRecursive(T node, Set allDependencies, Set visited) { + visited.add(node); + Collection directDependencies = getDirectDependencies(node); + allDependencies.addAll(directDependencies); + + for (T dependency : directDependencies) { + if (!visited.contains(dependency)) { + getAllDependenciesRecursive(dependency, allDependencies, visited); + } + } + } + /** * Builds a {@code DependencyGraph}. * From 727eb0fa32ec496c82b88deb9a019347eeb856ff Mon Sep 17 00:00:00 2001 From: adripo <26493496+adripo@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:41:59 +0200 Subject: [PATCH 2/5] chore: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05ed3faa7e3b..e4b1fb43a194 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) - - - From 385e4a3088ae14c22dcab9c54320a47a250ef3b3 Mon Sep 17 00:00:00 2001 From: adripo <26493496+adripo@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:26:06 +0200 Subject: [PATCH 3/5] test: validation of getAllDependents and getAllDependencies --- .../projects/test/DependencyGraphTests.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java index f6ccbb0f2795..d9b8fa5f96a0 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java @@ -628,6 +628,56 @@ public void testVersionResolutionHARD() { ResolutionResponse.ResolutionStatus.RESOLVED); } + @Test + public void testGetAllDependents() { + // Create a sample DependencyGraph with String nodes + DependencyGraph 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 allDependents = dependencyGraph.getAllDependents("A"); + Set expectedDependents = new HashSet<>(Arrays.asList("B", "C", "D")); + + Assert.assertEquals(expectedDependents, allDependents); + } + + @Test + public void testGetAllDependencies() { + // Create a sample DependencyGraph with String nodes + DependencyGraph 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 allDependencies = dependencyGraph.getAllDependencies("D"); + Set expectedDependencies = new HashSet<>(Arrays.asList("A", "B")); + + Assert.assertEquals(expectedDependencies, allDependencies); + } + @Test public void testTopologicalSortOfModuleDescriptor() { PackageName packageName = PackageName.from("package"); From 574f67256b8d9147a14fada28d5d28c6c5ccc8fe Mon Sep 17 00:00:00 2001 From: adripo <26493496+adripo@users.noreply.github.com> Date: Thu, 2 Nov 2023 02:42:49 +0100 Subject: [PATCH 4/5] fix: missing imports --- .../java/io/ballerina/projects/test/DependencyGraphTests.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java index d9b8fa5f96a0..9090ab3d438f 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java @@ -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; From 8242911bc2b0fc502388a7d059dac92a31c812df Mon Sep 17 00:00:00 2001 From: adripo <26493496+adripo@users.noreply.github.com> Date: Fri, 3 Nov 2023 19:10:21 +0100 Subject: [PATCH 5/5] fix: tests logic --- .../io/ballerina/projects/test/DependencyGraphTests.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java index 9090ab3d438f..3d1696388f01 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/DependencyGraphTests.java @@ -649,8 +649,8 @@ public void testGetAllDependents() { put("F", new LinkedHashSet<>()); }}); - Collection allDependents = dependencyGraph.getAllDependents("A"); - Set expectedDependents = new HashSet<>(Arrays.asList("B", "C", "D")); + Collection allDependents = dependencyGraph.getAllDependents("D"); + Set expectedDependents = new HashSet<>(Arrays.asList("A", "B")); Assert.assertEquals(expectedDependents, allDependents); } @@ -674,8 +674,8 @@ public void testGetAllDependencies() { put("F", new LinkedHashSet<>()); }}); - Collection allDependencies = dependencyGraph.getAllDependencies("D"); - Set expectedDependencies = new HashSet<>(Arrays.asList("A", "B")); + Collection allDependencies = dependencyGraph.getAllDependencies("A"); + Set expectedDependencies = new HashSet<>(Arrays.asList("B", "C", "D")); Assert.assertEquals(expectedDependencies, allDependencies); }