You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2024/09/25 05:59:39 UTC
(causeway) branch CAUSEWAY-3812-weighted.graphs created (now cd68d6b96f)
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a change to branch CAUSEWAY-3812-weighted.graphs
in repository https://gitbox.apache.org/repos/asf/causeway.git
at cd68d6b96f CAUSEWAY-3812: implements graph toString
This branch includes the following new commits:
new cd68d6b96f CAUSEWAY-3812: implements graph toString
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(causeway) 01/01: CAUSEWAY-3812: implements graph toString
Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch CAUSEWAY-3812-weighted.graphs
in repository https://gitbox.apache.org/repos/asf/causeway.git
commit cd68d6b96f1168700c5e8e5fa819dd4749bd3550
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Sep 25 07:59:32 2024 +0200
CAUSEWAY-3812: implements graph toString
---
.../apache/causeway/commons/graph/GraphUtils.java | 27 +++++++++++++++++
.../causeway/commons/graph/GraphUtilsTest.java | 35 +++++++++++++++++++++-
2 files changed, 61 insertions(+), 1 deletion(-)
diff --git a/commons/src/main/java/org/apache/causeway/commons/graph/GraphUtils.java b/commons/src/main/java/org/apache/causeway/commons/graph/GraphUtils.java
index 63735ee2e0..4433b53a04 100644
--- a/commons/src/main/java/org/apache/causeway/commons/graph/GraphUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/graph/GraphUtils.java
@@ -34,6 +34,7 @@ import org.apache.causeway.commons.collections.ImmutableEnumSet;
import org.apache.causeway.commons.functional.IndexedConsumer;
import org.apache.causeway.commons.graph.GraphUtils.GraphKernel.GraphCharacteristic;
import org.apache.causeway.commons.internal.assertions._Assert;
+import org.apache.causeway.commons.internal.base._Refs;
import org.apache.causeway.commons.internal.collections._PrimitiveCollections.IntList;
import lombok.Getter;
@@ -267,6 +268,32 @@ public class GraphUtils {
return new Graph<R>(kernel, nodes.map(nodeMapper));
}
+ @Override
+ public String toString() {
+ return toString(Object::toString);
+ }
+
+ public String toString(final Function<T, String> nodeFormatter) {
+ var sb = new StringBuilder();
+ var neighbourCount = _Refs.intRef(0);
+ for(int nodeIndex = 0; nodeIndex < kernel.nodeCount(); ++nodeIndex) {
+ var a = nodes().getElseFail(nodeIndex);
+ neighbourCount.setValue(0);
+ visitNeighbors(nodeIndex, b->{
+ sb
+ .append(String.format("%s -> %s", nodeFormatter.apply(a), nodeFormatter.apply(b)))
+ .append("\n");
+ neighbourCount.incAndGet();
+ });
+ if(neighbourCount.getValue()==0) {
+ sb
+ .append(String.format("%s", nodeFormatter.apply(a)))
+ .append("\n");
+ }
+ }
+ return sb.toString();
+ }
+
}
/**
diff --git a/commons/src/test/java/org/apache/causeway/commons/graph/GraphUtilsTest.java b/commons/src/test/java/org/apache/causeway/commons/graph/GraphUtilsTest.java
index c02b025df3..318ae91650 100644
--- a/commons/src/test/java/org/apache/causeway/commons/graph/GraphUtilsTest.java
+++ b/commons/src/test/java/org/apache/causeway/commons/graph/GraphUtilsTest.java
@@ -23,12 +23,46 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import org.springframework.util.StringUtils;
+
+import org.apache.causeway.commons.collections.Can;
import org.apache.causeway.commons.collections.ImmutableEnumSet;
import org.apache.causeway.commons.graph.GraphUtils.GraphKernel;
import org.apache.causeway.commons.graph.GraphUtils.GraphKernel.GraphCharacteristic;
+import org.apache.causeway.commons.io.TextUtils;
+
+import lombok.Value;
class GraphUtilsTest {
+ @Value
+ static class Customer {
+ String name;
+ }
+
+ @Test
+ void builder() {
+ var gBuilder = GraphUtils.GraphBuilder.directed(Customer.class);
+ gBuilder
+ .addNode(new Customer("A"))
+ .addNode(new Customer("B"))
+ .addNode(new Customer("C"))
+ .addNode(new Customer("D"))
+ .addEdge(0, 1)
+ .addEdge(1, 2)
+ .addEdge(2, 0);
+
+ var graph = gBuilder.build();
+ var textForm = graph.toString(Customer::getName);
+
+ //debug
+ //System.err.println(textForm);
+
+ assertEquals(
+ Can.of("A -> B", "B -> C", "C -> A", "D"),
+ TextUtils.readLines(textForm).filter(StringUtils::hasLength));
+ }
+
@Test
void subgraph() {
var graph = new GraphKernel(4, ImmutableEnumSet.noneOf(GraphCharacteristic.class));
@@ -57,7 +91,6 @@ class GraphUtilsTest {
assertFalse(subgraph3.isUndirected());
assertEquals(3, subgraph3.nodeCount());
assertEquals(2, subgraph3.edgeCount());
-
}
}