Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate MyDomain.from(DiffGraphBuilder => DiffGraphBuilder) convenience #200

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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`. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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`.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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`.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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`.
*/
Expand Down
11 changes: 2 additions & 9 deletions tests/src/test/scala/flatgraph/TestGraphs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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 */
Expand Down
21 changes: 7 additions & 14 deletions tests/src/test/scala/flatgraph/formats/graphml/GraphMLTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
32 changes: 12 additions & 20 deletions tests/src/test/scala/flatgraph/traversal/RepeatTraversalTests.scala
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
)
Expand Down
Loading