From d56ed8bdc9b3f5e1d2c4e56c78c31f376c9987dd Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Thu, 18 Apr 2024 11:53:25 +0200 Subject: [PATCH] Graph.toString and Graph.nodeCountByLabel (like in odb) (#176) * Graph.toString and Graph.nodeCountByLabel (like in odb) * also generate Domain.toString like before --- core/src/main/scala/flatgraph/Graph.scala | 11 +++++++++++ core/src/test/scala/flatgraph/GraphTests.scala | 3 +++ .../flatgraph/codegen/DomainClassesGenerator.scala | 3 +++ 3 files changed, 17 insertions(+) diff --git a/core/src/main/scala/flatgraph/Graph.scala b/core/src/main/scala/flatgraph/Graph.scala index 49119601..d499224d 100644 --- a/core/src/main/scala/flatgraph/Graph.scala +++ b/core/src/main/scala/flatgraph/Graph.scala @@ -157,4 +157,15 @@ class Graph(val schema: Schema, val storagePathMaybe: Option[Path] = None) exten } } + override def toString(): String = + s"Graph[${nodeCount()} nodes]" + + def nodeCountByLabel: Map[String, Int] = { + schema.nodeKinds + .map(schema.getNodeLabel) + .map(label => label -> nodeCount(label)) + .filter { case (label, count) => count > 0 } + .toMap + } + } diff --git a/core/src/test/scala/flatgraph/GraphTests.scala b/core/src/test/scala/flatgraph/GraphTests.scala index 8f726843..cfe216f8 100644 --- a/core/src/test/scala/flatgraph/GraphTests.scala +++ b/core/src/test/scala/flatgraph/GraphTests.scala @@ -173,6 +173,9 @@ class GraphTests extends AnyWordSpec with Matchers { Accessors.getNeighborsIn(V0_0_GNode).size shouldBe 2 V0_0_GNode.out.size shouldBe 1 V0_0_GNode.in.size shouldBe 2 + + g.toString shouldBe "Graph[4 nodes]" + g.nodeCountByLabel shouldBe Map("V0" -> 2, "V1" -> 2) } val schema = TestSchema.make(1, 1) diff --git a/domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scala b/domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scala index fcbbe7ef..b56595ce 100644 --- a/domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scala +++ b/domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scala @@ -890,6 +890,9 @@ class DomainClassesGenerator(schema: Schema) { | | override def close(): Unit = | _graph.close() + | + | override def toString(): String = + | String.format("$domainShortName[%s]", graph) |} | |@flatgraph.help.TraversalSource