mbeckerle commented on a change in pull request #228: Incremental progress on
Daffodil 1444 schema compiler space/speed issue.
URL: https://github.com/apache/incubator-daffodil/pull/228#discussion_r291751591
##########
File path: daffodil-core/src/main/scala/org/apache/daffodil/dsom/Root.scala
##########
@@ -45,4 +47,111 @@ final class Root(defXML: Node, parentArg: SchemaDocument,
override lazy val referencedElement = globalElementDecl
lazy val rootParseUnparsePolicy = defaultParseUnparsePolicy
+
+ /**
+ * For any given global schema component, tells us what schema components
contain
+ * references to it, and if that reference is from a sequence or choice, the
index of
+ * the group member within that sequence/choice.
+ *
+ * This is intended to be used when compilation needs to understand
+ * the context where an object is referenced. This allows the various
referencing contexts
+ * to be known, without making copies of schema components for each such
context.
+ */
+ lazy val refMap: Map[SchemaComponentFactory, Seq[(String, Seq[RefSpec])]] = {
+ val refEntries: Seq[(SchemaComponentFactory, Seq[RefSpec])] =
+ refTargets.groupBy { _.to }.toSeq
+ val m: Seq[(SchemaComponentFactory, Seq[(String, Seq[RefSpec])])] =
refEntries.map {
+ case (to, seq) => (to, seq.groupBy { _.from.sscd }.toSeq)
+ }
+ m.toMap
+ }
+
+ lazy val refPairsMap: Map[SchemaComponentFactory, Seq[String]] = {
+ refMap.toSeq.map {
+ case (to, seq: Seq[(String, _)]) => (to, seq.map { case (sscd, _) =>
sscd }.toSeq)
+ }.toMap
+ }
+
+ // lazy val refMapLexicalPairs = refMap.toSeq.map{
+ // case (to, pairs) => pairs.groupBy{case (sc, i) => sc.path}
+ // }.map{
+ // case (fromPath, pairs) => fromPath
+ // }.distinct
+ // }
+
+ private lazy val allComponentsSet = new mutable.HashSet[SchemaComponent]
+
+ private def allSchemaComponents(component: SchemaComponent, optIndex:
Option[Int]): Unit = {
+ if (allComponentsSet.contains(component)) {
+ // ok
+ } else {
+ allComponentsSet.add(component)
+ component match {
+ case er: ElementBase => er.typeDef match {
+ case std: SimpleTypeDefBase => //ok
+ case ctd: ComplexTypeBase => allSchemaComponents(ctd, None)
+ case _ => // ok
+ }
+ case ct: ComplexTypeBase => allSchemaComponents(ct.modelGroup, None)
+ case mg: ModelGroup => mg.groupMembers.foreach { gm =>
+ allSchemaComponents(gm, Some(gm.position))
+ }
+ }
+ }
+ }
+
+ final lazy val allComponents = {
+ allSchemaComponents(this, None)
+ allComponentsSet.toSeq
+ }
+
+ final lazy val numComponents =
+ allComponents.length
+
+ final lazy val allComponentSSCDs =
+ allComponents.map { _.sscd }.distinct
+
+ final lazy val numUniqueComponents =
+ allComponentSSCDs.length
+
+ final lazy val refTargets: Seq[RefSpec] = {
+ allComponents.collect {
+ case er: AbstractElementRef => {
+ val ed = er.referencedElement
+ RefSpec(er, ed.factory, er.position) +:
+ ed.optNamedComplexType.map { gctd => RefSpec(ed, gctd.factory, 1)
}.toSeq
+ }
+ case ed: LocalElementDecl => {
+ ed.optNamedComplexType.map { gctd => RefSpec(ed, gctd.factory, 1)
}.toSeq
+ }
+ case gr: GroupRef => Seq(RefSpec(gr, gr.groupDef.factory,
gr.asModelGroup.position))
+ }.flatten
+ }
+
+ lazy val allERefs = allComponents.filter {
+ case er: ElementRef => true
+ case _ => false
+ }.map { _.sscd }.distinct
+
+ lazy val allGRefs = allComponents.filter {
+ case _: GroupRef => true
+ case _ => false
+ }.map { _.sscd }.distinct
+
+ lazy val allCTRefs = {
+ val cts = allComponents.collect {
+ case e: ElementBase if (e.optComplexType.isDefined &&
e.complexType.isInstanceOf[GlobalComplexTypeDef]) => e.complexType
+ }
+ val ctsIDs = cts.map { _.sscd }.distinct
+ ctsIDs
+ }
+}
+
+case class RefSpec(from: SchemaComponent, to: SchemaComponentFactory, index:
Int) {
+
+ override def toString = "RefSpec(" +
Review comment:
really undesirable toString method. Use sscd instead now?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services