Skip to content

Commit a10a461

Browse files
committed
[#757] Add validator that detects the cyclic imports. Add print methods in Graph
1 parent d188623 commit a10a461

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

dmn-core/src/main/java/com/gs/dmn/Graph.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
import com.gs.dmn.runtime.Pair;
1616

17+
import java.io.IOException;
18+
import java.io.StringWriter;
19+
import java.io.Writer;
1720
import java.util.*;
21+
import java.util.function.Function;
22+
import java.util.function.Predicate;
1823

1924
public class Graph<T> {
2025
private final Set<T> nodes = new LinkedHashSet<>();
@@ -144,4 +149,34 @@ private void collectCyclesDFS(T node, Set<T> visited, Stack<T> stack, List<List<
144149
}
145150
stack.pop();
146151
}
152+
153+
public void printNodes(Writer writer, Function<T, String> nodeInfo) throws IOException {
154+
for (T node : nodes) {
155+
writer.write(nodeInfo.apply(node) + "\n");
156+
}
157+
}
158+
159+
public void printBF(T rootNode, StringWriter writer, Function<T, String> nodeInfo) {
160+
printBF(rootNode, writer, nodeInfo, x -> true);
161+
}
162+
163+
public void printBF(T rootNode, StringWriter writer, Function<T, String> nodeInfo, Predicate<T> filter) {
164+
Set<T> visited = new LinkedHashSet<>();
165+
if (filter.test(rootNode)) {
166+
printBF(rootNode, writer, nodeInfo, filter, 0, visited);
167+
}
168+
}
169+
170+
private void printBF(T node, StringWriter writer, Function<T, String> nodeInfo, Predicate<T> filter, int level, Set<T> visited) {
171+
String indent = "\t".repeat(level);
172+
writer.write(indent + nodeInfo.apply(node) + "\n");
173+
if (!visited.contains(node)) {
174+
visited.add(node);
175+
for (T child : getChildren(node)) {
176+
if (filter.test(child)) {
177+
printBF(child, writer, nodeInfo, filter, level + 1, visited);
178+
}
179+
}
180+
}
181+
}
147182
}

dmn-core/src/test/java/com/gs/dmn/GraphTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.gs.dmn.runtime.Pair;
1616
import org.junit.jupiter.api.Test;
1717

18+
import java.io.IOException;
19+
import java.io.StringWriter;
1820
import java.util.List;
1921
import java.util.Set;
2022

@@ -133,4 +135,46 @@ void testFindCyclesWhenSelfLoop() {
133135

134136
assertEquals(List.of(List.of(1, 1)), graph.findCycles());
135137
}
138+
139+
@Test
140+
void testPrintNodes() throws IOException {
141+
Graph<Integer> graph = new Graph<>();
142+
graph.addEdge(1, 2);
143+
graph.addEdge(2, 3);
144+
graph.addEdge(3, 1); // Cycle 1-2-3-1
145+
graph.addEdge(3, 4);
146+
graph.addEdge(4, 2); // Cycle 2-3-4-2
147+
148+
StringWriter writer = new StringWriter();
149+
graph.printNodes(writer, this::nodeInfo);
150+
String expectedNodes = "1\n2\n3\n4\n";
151+
assertEquals(expectedNodes, writer.getBuffer().toString());
152+
}
153+
154+
@Test
155+
void testPrintTree() {
156+
Graph<Integer> graph = new Graph<>();
157+
graph.addEdge(1, 2);
158+
graph.addEdge(2, 3);
159+
graph.addEdge(3, 1); // Cycle 1-2-3-1
160+
graph.addEdge(3, 4);
161+
graph.addEdge(4, 2); // Cycle 2-3-4-2
162+
163+
StringWriter writer = new StringWriter();
164+
graph.printBF(1, writer, this::nodeInfo);
165+
String expectedNodes =
166+
"""
167+
1
168+
\t2
169+
\t\t3
170+
\t\t\t1
171+
\t\t\t4
172+
\t\t\t\t2
173+
""";
174+
assertEquals(expectedNodes, writer.getBuffer().toString());
175+
}
176+
177+
private String nodeInfo(Integer node) {
178+
return "" + node;
179+
}
136180
}

0 commit comments

Comments
 (0)