Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ package flatgraph.codegen
import java.nio.file.Path
import flatgraph.codegen.CodeSnippets.{FilterSteps, NewNodeInserters}
import flatgraph.codegen.Helpers._
import flatgraph.schema.{AbstractNodeType, AdjacentNode, Direction, EdgeType, MarkerTrait, NodeBaseType, NodeType, Property, Schema}
import flatgraph.schema.{
AbstractNodeType,
AdjacentNode,
ContainedNode,
Direction,
EdgeType,
MarkerTrait,
NodeBaseType,
NodeType,
Property,
Schema
}
import flatgraph.schema.Helpers._
import flatgraph.schema.Property.{Cardinality, Default, ValueType}

Expand Down Expand Up @@ -158,12 +169,6 @@ class DomainClassesGenerator(schema: Schema) {
val mixinsNew =
List("NewNode", s"${baseType.className}Base") ++ baseType.extendz.map(_.className + "New") ++ baseType.markerTraits.map(_.name)
val newProperties = newPropsAtNodeList(baseType)
val propertyDefaults = newProperties
.collect {
case p if p.hasDefault =>
s"""val ${p.className} = ${Helpers.defaultValueImpl(p.default.get)}"""
}
.mkString("\n")
val mixinsEMT =
(List("AnyRef") ++ newExtendz.map { p => s"${p.className}EMT" } ++ newProperties.map { p => s"Has${p.className}EMT" })
.mkString(" with ")
Expand Down Expand Up @@ -206,12 +211,6 @@ class DomainClassesGenerator(schema: Schema) {
.mkString(", ")}
|trait ${baseType.className} extends ${mixinsStored.mkString(" with ")} with StaticType[${baseType.className}EMT]
|
|object ${baseType.className} {
| object PropertyDefaults {
| $propertyDefaults
| }
|}
|
|trait ${baseType.className}New extends ${mixinsNew.mkString(" with ")} with StaticType[${baseType.className}EMT]{
| ${newNodeDefs.mkString("\n")}
|}
Expand Down Expand Up @@ -391,32 +390,6 @@ class DomainClassesGenerator(schema: Schema) {
val productElementAccessors = productElements.zipWithIndex.map { case (name, index) =>
s"case $index => this.$name"
}.mkString("\n")

val propertyNames = {
val sourceLines = Seq.newBuilder[String]
nodeType.properties.map { property =>
s"""${scaladocMaybe(property.comment)}
|val ${camelCaseCaps(property.name)} = "${property.name}" """.stripMargin
}.map(sourceLines.addOne)
nodeType.containedNodes.map { containedNode =>
s"""${scaladocMaybe(containedNode.comment)}
|val ${camelCaseCaps(containedNode.localName)} = "${containedNode.localName}" """.stripMargin.trim
}.map(sourceLines.addOne)
sourceLines.result().mkString("\n")
}

val properties = {
val sourceLines = Seq.newBuilder[String]
nodeType.properties.map { property =>
propertyKeySource(property, propertyKindByProperty(property))
}.map(sourceLines.addOne)
sourceLines.result().mkString("\n")
}

val propertyDefaults = nodeType.properties.collect {
case p if p.hasDefault =>
s"""val ${p.className} = ${Helpers.defaultValueImpl(p.default.get)}"""
}.mkString("\n")
// format: on

def neighborEdgeStr(es: Map[String, Set[String]]): String =
Expand Down Expand Up @@ -517,6 +490,7 @@ class DomainClassesGenerator(schema: Schema) {
"\n",
"\n res\n}"
)

val nodeSource = {
s"""package $basePackage.nodes
|
Expand All @@ -533,17 +507,9 @@ class DomainClassesGenerator(schema: Schema) {
|
|object ${nodeType.className} {
| val Label = "${nodeType.name}"
| object PropertyNames {
| $propertyNames
| }
| object Properties {
| $properties
| }
| object PropertyDefaults {
| $propertyDefaults
| }
|}
|
|${commentForNodeType(nodeType)}
|$storedNode {
| ${storedNodeProps.mkString("\n")}
|
Expand Down Expand Up @@ -1152,16 +1118,6 @@ class DomainClassesGenerator(schema: Schema) {
results.addOne(file)
}

writeConstants(
"PropertyNames",
schema.properties.map { property =>
ConstantContext(
property.name.toUpperCase,
s"""public static final String ${property.name.toUpperCase} = "${property.name}";""",
property.comment
)
}
)
writeConstants(
"NodeTypes",
schema.nodeTypes.map { nodeType =>
Expand Down Expand Up @@ -1196,16 +1152,58 @@ class DomainClassesGenerator(schema: Schema) {
propertyKeySource(property, propertyKind = kindContexts.propertyKindByProperty(property))
}
}.mkString("\n\n")
val file = outputDir / "Properties.scala"
val propertiesFile = outputDir / "Properties.scala"
os.write(
file,
propertiesFile,
s"""package ${schema.basePackage}
|
|object Properties {
|$propertyKeysConstantsSource
|}""".stripMargin
)
results.addOne(file)
results.addOne(propertiesFile)
val propertyNamesSource = schema.properties.map(p => propertyNameConstantDef(p.comment, p.name)).mkString("\n")
val containedNodeTypes = schema.nodeTypes.flatMap(_.containedNodes).map(_.nodeType)
val containedNodesSource = containedNodeTypes.map(n => propertyNameConstantDef(n.comment, n.name)).toSet.mkString("\n")
val allNames =
(schema.properties.map(p => p.name) ++ containedNodeTypes.map(n => n.name)).map(camelCaseCaps).mkString(",\n")
val propertyNamesFile = outputDir / "PropertyNames.scala"
os.write(
propertyNamesFile,
s"""package ${schema.basePackage}
|
|import java.util.{HashSet, Set}
|import scala.jdk.CollectionConverters.SeqHasAsJava
|
|object PropertyNames {
|$propertyNamesSource
|
|$containedNodesSource
|
|val All: Set[String] = new HashSet[String](Seq(
|$allNames
|).asJava)
|}
|""".stripMargin
)
results.addOne(propertyNamesFile)
val propertyDefaultsSource = schema.properties
.collect {
case p if p.hasDefault =>
s"""val ${camelCaseCaps(p.name)} = ${Helpers.defaultValueImpl(p.default.get)}"""
}
.mkString("\n")
val propertyDefaultsFile = outputDir / "PropertyDefaults.scala"
os.write(
propertyDefaultsFile,
s"""package ${schema.basePackage}
|
|object PropertyDefaults {
|$propertyDefaultsSource
|}
|""".stripMargin
)
results.addOne(propertyDefaultsFile)

results.result()
}
Expand Down Expand Up @@ -1555,6 +1553,49 @@ class DomainClassesGenerator(schema: Schema) {
PropertyContexts(relevantPropertiesSet.toArray.sortBy(_.name), containingByName.view.toMap)
}

private def commentForNodeType(nodeType: NodeType): String = {
val commentLines = Seq.newBuilder[String]
def documentCardinality(cardinality: Cardinality): String = {
cardinality match {
case Cardinality.One(default) => s"Cardinality `one` (mandatory with default value `${default.value}`)"
case Cardinality.ZeroOrOne => "Cardinality `ZeroOrOne` (optional)"
case Cardinality.List => "Cardinality `List` (many)"
}
}
val propertiesInfos = nodeType.properties.map { property =>
val typeInfo = property.valueType.getClass.getSimpleName.stripSuffix("$")
val commentMaybe = property.comment.map(comment => s"; $comment").getOrElse("")
s"▸ ${camelCaseCaps(property.name)} ($typeInfo); ${documentCardinality(property.cardinality)}$commentMaybe"
}
if (propertiesInfos.nonEmpty) {
commentLines += "* NODE PROPERTIES:"
commentLines.addAll(propertiesInfos)
}

val containedNodesInfo = nodeType.containedNodes.map { containedNode =>
val nodeType = camelCaseCaps(containedNode.nodeType.name)
val commentMaybe = containedNode.comment.map(comment => s"; $comment").getOrElse("")
s"▸ ${containedNode.localName} ($nodeType); ${documentCardinality(containedNode.cardinality)}$commentMaybe"
}
if (containedNodesInfo.nonEmpty) {
commentLines += "* CONTAINED NODES:"
commentLines.addAll(containedNodesInfo)
}

// TODO tame scalafmt - we'd rather only have one \n here:
commentLines.result().mkString(start = "/** ", sep = "\n\n*", end = "*/")
}

private def propertyNameConstantDef[A](comment: Option[String], propertyName: String): String = {
val commentString = comment.map { comment =>
s"/** $comment */"
}.toList

val defString = s"""val ${camelCaseCaps(propertyName)}: String = "$propertyName""""

(commentString :+ defString).mkString("\n")
}

lazy val allNodeTypes = schema.allNodeTypes.toSet

def flattenNeighbors(x: AbstractNodeType) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testdomains.codepropertygraphminified
object Properties {
val DispatchType = flatgraph.SinglePropertyKey[String](kind = 0, name = "DISPATCH_TYPE", default = "<empty>")

/** The name of a thing. */
val Name = flatgraph.SinglePropertyKey[String](kind = 1, name = "NAME", default = "<empty>")

val Order = flatgraph.SinglePropertyKey[Int](kind = 2, name = "ORDER", default = -1: Int)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testdomains.codepropertygraphminified

object PropertyDefaults {
val DispatchType = "<empty>"
val Name = "<empty>"
val Order = -1: Int
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package testdomains.codepropertygraphminified

import java.util.{HashSet, Set}
import scala.jdk.CollectionConverters.SeqHasAsJava

object PropertyNames {
val DispatchType: String = "DISPATCH_TYPE"

/** The name of a thing. */
val Name: String = "NAME"
val Order: String = "ORDER"

val All: Set[String] = new HashSet[String](Seq(DispatchType, Name, Order).asJava)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ trait CallReprBase extends AbstractNode with StaticType[CallReprEMT]
// implementing nodes: CALL
trait CallRepr extends StoredNode with CallReprBase with StaticType[CallReprEMT]

object CallRepr {
object PropertyDefaults {
val Name = "<empty>"
val Order = -1: Int
}
}

trait CallReprNew extends NewNode with CallReprBase with StaticType[CallReprEMT] {
def name: String
def name_=(value: String): Unit
Expand All @@ -34,12 +27,6 @@ trait DeclarationBase extends AbstractNode with StaticType[DeclarationEMT]
// implementing nodes: METHOD
trait Declaration extends StoredNode with DeclarationBase with StaticType[DeclarationEMT]

object Declaration {
object PropertyDefaults {
val Name = "<empty>"
}
}

trait DeclarationNew extends NewNode with DeclarationBase with StaticType[DeclarationEMT] {
def name: String
def name_=(value: String): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,16 @@ trait CallBase extends AbstractNode with CallReprBase with StaticType[CallEMT] {

object Call {
val Label = "CALL"
object PropertyNames {

val DispatchType = "DISPATCH_TYPE"

val Name = "NAME"

val Order = "ORDER"
}
object Properties {
val DispatchType = flatgraph.SinglePropertyKey[String](kind = 0, name = "DISPATCH_TYPE", default = "<empty>")
val Name = flatgraph.SinglePropertyKey[String](kind = 1, name = "NAME", default = "<empty>")
val Order = flatgraph.SinglePropertyKey[Int](kind = 2, name = "ORDER", default = -1: Int)
}
object PropertyDefaults {
val DispatchType = "<empty>"
val Name = "<empty>"
val Order = -1: Int
}
}

/** * NODE PROPERTIES:
*
* ▸ DispatchType (String); Cardinality `one` (mandatory with default value `<empty>`)
*
* ▸ Name (String); Cardinality `one` (mandatory with default value `<empty>`); The name of a thing.
*
* ▸ Order (Int); Cardinality `one` (mandatory with default value `-1`)
*/
class Call(graph_4762: flatgraph.Graph, seq_4762: Int)
extends StoredNode(graph_4762, 0, seq_4762)
with CallBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ trait MethodBase extends AbstractNode with DeclarationBase with StaticType[Metho

object Method {
val Label = "METHOD"
object PropertyNames {

val Name = "NAME"
}
object Properties {
val Name = flatgraph.SinglePropertyKey[String](kind = 1, name = "NAME", default = "<empty>")
}
object PropertyDefaults {
val Name = "<empty>"
}
}

/** * NODE PROPERTIES:
*
* ▸ Name (String); Cardinality `one` (mandatory with default value `<empty>`); The name of a thing.
*/
class Method(graph_4762: flatgraph.Graph, seq_4762: Int)
extends StoredNode(graph_4762, 1, seq_4762)
with MethodBase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package testdomains.empty

object PropertyDefaults {}

This file was deleted.

Loading