stevedlawrence commented on a change in pull request #262: Unordered sequences
URL: https://github.com/apache/incubator-daffodil/pull/262#discussion_r324000876
 
 

 ##########
 File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/InfosetImpl.scala
 ##########
 @@ -1415,6 +1423,50 @@ sealed class DIComplex(override val erd: 
ElementRuntimeData, val tunable: Daffod
     }
   }
 
+  final def sortChildNodesByPosition(): Unit = {
+    // TODO: Once we upgrade to Scala 2.13 we can call the inplace sort 
method: DAFFODIL-2152
+    // childNodes = childNodes.sortInPlace(_.erd.position)
+    childNodes = childNodes.sortBy(_.erd.position)
+  }
+
+  /*
+   * When parsing unordered sequences it is possible to have non-contiguous 
arrays.
+   * When parsed, these arrays show up as separate DIArrays in the infoset. We 
need
+   * to combine these separate arrays.
+   * */
+  final def flattenAndValidateChildNodes(): Maybe[List[ElementRuntimeData]] = {
+    var ret: List[ElementRuntimeData] = List()
+    var groups = childNodes.groupBy(_.erd)
+    groups foreach {
+      case (erd, nodes) => {
+        // Check min/maxOccurs validity while iterating over childNodes
+        val min0 = erd.minOccurs
+        val max0 = erd.maxOccurs
+        val isUnbounded = max0 == -1
+
+        // Flatten multiple DIArrays into the first one
+        if (erd.isArray) {
+          nodes.reduceLeft((a, b) => 
a.asInstanceOf[DIArray].concat(b.asInstanceOf[DIArray]))
+          nodes.reduceToSize(1)
+        }
+
+        val occurrence = if (erd.isArray) nodes(0).contents.length else 
nodes.length
+        if (isUnbounded && occurrence < min0)
+          ret = ret :+ erd
+        else if (!isUnbounded && (occurrence < min0 || occurrence > max0))
+          ret = ret :+ erd
+      }
+    }
+
+    val (erds, nodes) = groups.unzip
+    childNodes.clear()
+    nodes.foreach( childNodes ++= _ )
+    if (ret.isEmpty)
+      Maybe.Nope
+    else
+      Maybe(ret)
+  }
+
 
 Review comment:
   I'm wonder if this function can be made without the vars? Maybe something 
like this:
   ```
   val groups = childNodes.groupBy(_.erd)
   val flattenedChildren = groups.map { case (erd, nodes) =>
     if (erd.isArray) {
       // concat logic here
       // check min/max of array and create validation error
    } else {
       // must be scalar, parse error if nodes.length > 1
     } 
     // eithere it's an array and we conacated to the first node, or it's a 
scalar and there's only one. so we can map to the first node
     nodes(0)
   }
   childNodes.clear()
   flattedChildren = nodes.foreach { childNodes ++= _ }
   ```
   Avoids unziping and vars.

----------------------------------------------------------------
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

Reply via email to