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 5992700f..5cecfd9b 100644 --- a/domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scala +++ b/domain-classes-generator/src/main/scala/flatgraph/codegen/DomainClassesGenerator.scala @@ -908,7 +908,7 @@ class DomainClassesGenerator(schema: Schema) { val domainMain = s"""package $basePackage - |import flatgraph.DiffGraphBuilder + |import flatgraph.{DiffGraphApplier, DiffGraphBuilder} |import flatgraph.help.DocSearchPackages |import flatgraph.help.Table.AvailableWidthProvider |import Language.* @@ -936,6 +936,12 @@ class DomainClassesGenerator(schema: Schema) { | | def empty: $domainShortName = new $domainShortName(new flatgraph.Graph(GraphSchema)) | + | def from(initialElements: DiffGraphBuilder => DiffGraphBuilder): $domainShortName = { + | val graph = new flatgraph.Graph(GraphSchema) + | DiffGraphApplier.applyDiff(graph, initialElements(new DiffGraphBuilder(GraphSchema))) + | new $domainShortName(graph) + | } + | | /** Instantiate a new graph with storage. If the file already exists, this will deserialize the given file into memory. | * `Graph.close` will serialise graph to that given file (and override whatever was there before), unless you | * specify `persistOnClose = false`. */ diff --git a/sbt-flatgraph/src/main/scala/flatgraph/codegen/sbt/FlatgraphCodegenSbtPlugin.scala b/sbt-flatgraph/src/main/scala/flatgraph/codegen/sbt/FlatgraphCodegenSbtPlugin.scala index 43b3b7d1..718c11fd 100644 --- a/sbt-flatgraph/src/main/scala/flatgraph/codegen/sbt/FlatgraphCodegenSbtPlugin.scala +++ b/sbt-flatgraph/src/main/scala/flatgraph/codegen/sbt/FlatgraphCodegenSbtPlugin.scala @@ -65,12 +65,7 @@ object FlatgraphCodegenSbtPlugin extends AutoPlugin { // directories / files that we want to monitor for changes - if none of these changed, we don't need to regenerate the domain classes val inputs = invalidateOnChangesInValue ++ // <- configurable in the build, e.g. `generateDomainClasses/invalidateOnChangesIn += file("foo.bar")` - Seq( - sourceDirectory.value, - baseDirectory.value / "build.sbt", - (ThisBuild/baseDirectory).value / "build.sbt", - dependenciesFile - ) + Seq(sourceDirectory.value, baseDirectory.value / "build.sbt", (ThisBuild / baseDirectory).value / "build.sbt", dependenciesFile) // inputs.foreach(println) lazy val currentSchemaAndDependenciesHash = FileUtils.md5(inputs) lazy val lastSchemaAndDependenciesHash: Option[String] = diff --git a/test-schemas-domain-classes/src/main/scala/testdomains/generic/GenericDomain.scala b/test-schemas-domain-classes/src/main/scala/testdomains/generic/GenericDomain.scala index 03df1802..1045836b 100644 --- a/test-schemas-domain-classes/src/main/scala/testdomains/generic/GenericDomain.scala +++ b/test-schemas-domain-classes/src/main/scala/testdomains/generic/GenericDomain.scala @@ -1,5 +1,5 @@ package testdomains.generic -import flatgraph.DiffGraphBuilder +import flatgraph.{DiffGraphApplier, DiffGraphBuilder} import flatgraph.help.DocSearchPackages import flatgraph.help.Table.AvailableWidthProvider import Language.* @@ -25,6 +25,12 @@ object GenericDomain { def empty: GenericDomain = new GenericDomain(new flatgraph.Graph(GraphSchema)) + def from(initialElements: DiffGraphBuilder => DiffGraphBuilder): GenericDomain = { + val graph = new flatgraph.Graph(GraphSchema) + DiffGraphApplier.applyDiff(graph, initialElements(new DiffGraphBuilder(GraphSchema))) + new GenericDomain(graph) + } + /** Instantiate a new graph with storage. If the file already exists, this will deserialize the given file into memory. `Graph.close` will * serialise graph to that given file (and override whatever was there before), unless you specify `persistOnClose = false`. */ diff --git a/test-schemas-domain-classes/src/main/scala/testdomains/gratefuldead/GratefulDead.scala b/test-schemas-domain-classes/src/main/scala/testdomains/gratefuldead/GratefulDead.scala index 5331a2a6..808a787a 100644 --- a/test-schemas-domain-classes/src/main/scala/testdomains/gratefuldead/GratefulDead.scala +++ b/test-schemas-domain-classes/src/main/scala/testdomains/gratefuldead/GratefulDead.scala @@ -1,5 +1,5 @@ package testdomains.gratefuldead -import flatgraph.DiffGraphBuilder +import flatgraph.{DiffGraphApplier, DiffGraphBuilder} import flatgraph.help.DocSearchPackages import flatgraph.help.Table.AvailableWidthProvider import Language.* @@ -25,6 +25,12 @@ object GratefulDead { def empty: GratefulDead = new GratefulDead(new flatgraph.Graph(GraphSchema)) + def from(initialElements: DiffGraphBuilder => DiffGraphBuilder): GratefulDead = { + val graph = new flatgraph.Graph(GraphSchema) + DiffGraphApplier.applyDiff(graph, initialElements(new DiffGraphBuilder(GraphSchema))) + new GratefulDead(graph) + } + /** Instantiate a new graph with storage. If the file already exists, this will deserialize the given file into memory. `Graph.close` will * serialise graph to that given file (and override whatever was there before), unless you specify `persistOnClose = false`. */ diff --git a/test-schemas-domain-classes/src/main/scala/testdomains/hierarchical/Hierarchical.scala b/test-schemas-domain-classes/src/main/scala/testdomains/hierarchical/Hierarchical.scala index bc689b8b..12e26d29 100644 --- a/test-schemas-domain-classes/src/main/scala/testdomains/hierarchical/Hierarchical.scala +++ b/test-schemas-domain-classes/src/main/scala/testdomains/hierarchical/Hierarchical.scala @@ -1,5 +1,5 @@ package testdomains.hierarchical -import flatgraph.DiffGraphBuilder +import flatgraph.{DiffGraphApplier, DiffGraphBuilder} import flatgraph.help.DocSearchPackages import flatgraph.help.Table.AvailableWidthProvider import Language.* @@ -25,6 +25,12 @@ object Hierarchical { def empty: Hierarchical = new Hierarchical(new flatgraph.Graph(GraphSchema)) + def from(initialElements: DiffGraphBuilder => DiffGraphBuilder): Hierarchical = { + val graph = new flatgraph.Graph(GraphSchema) + DiffGraphApplier.applyDiff(graph, initialElements(new DiffGraphBuilder(GraphSchema))) + new Hierarchical(graph) + } + /** Instantiate a new graph with storage. If the file already exists, this will deserialize the given file into memory. `Graph.close` will * serialise graph to that given file (and override whatever was there before), unless you specify `persistOnClose = false`. */ diff --git a/tests/src/test/scala/flatgraph/TestGraphs.scala b/tests/src/test/scala/flatgraph/TestGraphs.scala index 3bb731ae..91027fd0 100644 --- a/tests/src/test/scala/flatgraph/TestGraphs.scala +++ b/tests/src/test/scala/flatgraph/TestGraphs.scala @@ -35,9 +35,6 @@ object TestGraphs { /** L3 <- L2 <- L1 <- Center -> R1 -> R2 -> R3 -> R4 -> R5 */ def createFlatlineGraph(): GenericDomain = { - val genericDomain = GenericDomain.empty - val diffGraph = GenericDomain.newDiffGraphBuilder - val center = NewNodeA().stringMandatory("Center") val l1 = NewNodeA().stringMandatory("L1") val l2 = NewNodeA().stringMandatory("L2") @@ -58,10 +55,8 @@ object TestGraphs { // r3 --- (Connection.Label, Connection.Properties.Distance.of(13)) --> r4 // r4 --- (Connection.Label, Connection.Properties.Distance.of(14)) --> r5 - DiffGraphApplier.applyDiff( - genericDomain.graph, - GenericDomain.newDiffGraphBuilder - .addEdge(center, l1, ConnectedTo.Label) + GenericDomain.from( + _.addEdge(center, l1, ConnectedTo.Label) .addEdge(l1, l2, ConnectedTo.Label) .addEdge(l2, l3, ConnectedTo.Label) .addEdge(center, r1, ConnectedTo.Label) @@ -70,8 +65,6 @@ object TestGraphs { .addEdge(r3, r4, ConnectedTo.Label) .addEdge(r4, r5, ConnectedTo.Label) ) - - genericDomain } /** L3 <- L2 <- L1 <- Center -> R1 -> R2 -> R3 -> R4 -> R5 */ diff --git a/tests/src/test/scala/flatgraph/formats/graphml/GraphMLTests.scala b/tests/src/test/scala/flatgraph/formats/graphml/GraphMLTests.scala index 98db9303..551a3a7f 100644 --- a/tests/src/test/scala/flatgraph/formats/graphml/GraphMLTests.scala +++ b/tests/src/test/scala/flatgraph/formats/graphml/GraphMLTests.scala @@ -46,17 +46,16 @@ class GraphMLTests extends AnyWordSpec { import testdomains.generic.nodes.NewNodeA "not using (unsupported) list properties" in { - val graph = GenericDomain.empty.graph val node1 = NewNodeA().stringOptional("node 1 opt") val node2 = NewNodeA().stringMandatory("node 2 mandatory").stringOptional("node 2 opt") val node3 = NewNodeA().intMandatory(1).intOptional(2) - DiffGraphApplier.applyDiff( - graph, - GenericDomain.newDiffGraphBuilder - .addEdge(node1, node2, ConnectedTo.Label) - .addEdge(node2, node3, ConnectedTo.Label) - ) + val graph = GenericDomain + .from( + _.addEdge(node1, node2, ConnectedTo.Label) + .addEdge(node2, node3, ConnectedTo.Label) + ) + .graph File.usingTemporaryDirectory(this.getClass.getName) { exportRootDirectory => val exportResult = GraphMLExporter.runExport(graph, exportRootDirectory.pathAsString) @@ -79,17 +78,11 @@ class GraphMLTests extends AnyWordSpec { } "using list properties" in { - val graph = GenericDomain.empty.graph - // exporter will discard the list properties, but inform the user about it val node1 = NewNodeA().stringMandatory("node 2 a").stringOptional("node 2 b").stringList(Seq("node 3 c1", "node 3 c2")) val node2 = NewNodeA().intMandatory(1).intOptional(2).intList(Seq(10, 11, 12)) - DiffGraphApplier.applyDiff( - graph, - GenericDomain.newDiffGraphBuilder - .addEdge(node1, node2, ConnectedTo.Label) - ) + val graph = GenericDomain.from(_.addEdge(node1, node2, ConnectedTo.Label)).graph File.usingTemporaryDirectory(this.getClass.getName) { exportRootDirectory => val exportResult = GraphMLExporter.runExport(graph, exportRootDirectory.pathAsString) diff --git a/tests/src/test/scala/flatgraph/traversal/RepeatTraversalTests.scala b/tests/src/test/scala/flatgraph/traversal/RepeatTraversalTests.scala index 7d74d682..68bb4737 100644 --- a/tests/src/test/scala/flatgraph/traversal/RepeatTraversalTests.scala +++ b/tests/src/test/scala/flatgraph/traversal/RepeatTraversalTests.scala @@ -1,6 +1,6 @@ package flatgraph.traversal -import flatgraph.{DiffGraphApplier, DiffGraphBuilder, GNode} +import flatgraph.GNode import flatgraph.TestGraphs.FlatlineGraphFixture import testdomains.generic.GenericDomain import testdomains.generic.Language.* @@ -317,19 +317,15 @@ class RepeatTraversalTests extends AnyWordSpec with FlatlineGraphFixture { "supports large amount of iterations" in { // create circular graph so that we can repeat any number of times - val genericDomain = GenericDomain.empty - val graph = genericDomain.graph - val diffGraph = GenericDomain.newDiffGraphBuilder - val newA = NewNodeA().stringMandatory("a") val newB = NewNodeA().stringMandatory("b") val newC = NewNodeA().stringMandatory("c") - val diff = DiffGraphBuilder(graph.schema) - .addEdge(newA, newB, ConnectedTo.Label) - .addEdge(newB, newC, ConnectedTo.Label) - .addEdge(newC, newA, ConnectedTo.Label) - DiffGraphApplier.applyDiff(graph, diff) + val genericDomain = GenericDomain.from( + _.addEdge(newA, newB, ConnectedTo.Label) + .addEdge(newB, newC, ConnectedTo.Label) + .addEdge(newC, newA, ConnectedTo.Label) + ) val repeatCount = 100000 @@ -413,17 +409,13 @@ class RepeatTraversalTests extends AnyWordSpec with FlatlineGraphFixture { /** Using hierarchical domain to verify that repeat derives the correct types. Graph setup: NodeX <: BaseType NodeY <: BaseType X1 --> * Y2 --> X3 --> X4 */ + val newNodeX1 = NewNodeX().name("X1") + val newNodeY2 = NewNodeY().name("Y2") + val newNodeX3 = NewNodeX().name("X3") + val newNodeY4 = NewNodeY().name("Y4") - val hierarchical = Hierarchical.empty - val newNodeX1 = NewNodeX().name("X1") - val newNodeY2 = NewNodeY().name("Y2") - val newNodeX3 = NewNodeX().name("X3") - val newNodeY4 = NewNodeY().name("Y4") - - DiffGraphApplier.applyDiff( - hierarchical.graph, - Hierarchical.newDiffGraphBuilder - .addEdge(newNodeX1, newNodeY2, testdomains.hierarchical.EdgeTypes.connected_to) + val hierarchical = Hierarchical.from( + _.addEdge(newNodeX1, newNodeY2, testdomains.hierarchical.EdgeTypes.connected_to) .addEdge(newNodeY2, newNodeX3, testdomains.hierarchical.EdgeTypes.connected_to) .addEdge(newNodeX3, newNodeY4, testdomains.hierarchical.EdgeTypes.connected_to) )