You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by "stevedlawrence (via GitHub)" <gi...@apache.org> on 2025/05/07 16:42:57 UTC

Re: [PR] Fix warning about MultipleChildElementsWithSameName for hidden Elements [daffodil]

stevedlawrence commented on code in PR #1415:
URL: https://github.com/apache/daffodil/pull/1415#discussion_r1935640706


##########
daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ModelGroup.scala:
##########
@@ -341,19 +341,30 @@ abstract class ModelGroup protected (index: Int)
     }
   }
 
-  final lazy val elementChildrenInNonHiddenContext: Seq[ElementBase] =
+  final def elementChildrenInNonHiddenContext: Seq[ElementBase] =

Review Comment:
   Why make this a def? Multiple things call `elementChildrenInNonHiddenContext`, so seems like we should store the save the result so we don't have to recompute this.



##########
daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ModelGroup.scala:
##########
@@ -341,19 +341,30 @@ abstract class ModelGroup protected (index: Int)
     }
   }
 
-  final lazy val elementChildrenInNonHiddenContext: Seq[ElementBase] =
+  final def elementChildrenInNonHiddenContext: Seq[ElementBase] =
     LV('elementChildrenInNonHiddenContext) {
-      val ebs = groupMembers
-        .filter {
-          case gr: GroupRef => !gr.isHidden
-          case _ => true
-        }
-        .flatMap { gm =>
-          gm match {
-            case eb: ElementBase => Seq(eb)
-            case gb: ModelGroup => gb.elementChildren
-          }
+      val ebs = this match {
+        case gr: GroupRef if gr.isHidden => Seq.empty
+        case stb: SequenceTermBase if stb.isHidden => Seq.empty
+        case _ => {
+          groupMembers
+            .filter {
+              case gr: GroupRef => !gr.isHidden
+              case stb: SequenceTermBase => !stb.isHidden
+              case eb: ElementBase =>
+                eb.optLexicalParent.forall {

Review Comment:
   This only looks at the first lexical parent, but really any parent up the DSOM chain could be a hidden group that would hide this model group and should prevent these elements from being considered. I'm wondering if that indicates there's an alternative?
   
   I think the core issue is that when doing things involving `isHidden`, you really need to do checks top down. You can't really look at parents to determine `isHidden` because you would have to look up the entire chain, which I'm not even sure is possible since we can only look at lexical parents.
   
   This sort of implies that anything using `elementChildrenInNonHiddenContext` (currently `checkIfMultipleChildrenWithSameName` and `checkUnorderedSequences...InNamespace`) should not be requiredEvaluations on a model group, because they really shouldn't be evaluated at all if some parent up the DSOM chain is a hidden group ref.
   
   So I'm wondering if instead, there should really only be a single requiredEvaluation on the Root DSOM (or GlobalElementDecl with an isRoot check or something), and it would recursively call the checks. For example something like:
   
   ```scala
   class Root {
     requiredEvaluation(globalElementDecl.checkIfMultipleChildrenWithSameName)
     ...
   }
   
   class Element {
     ...
     lazy val checkIfMultipleChildrenWithSameName = LV(...) {
       if (_.isComplex) {
        val nonHiddenElems = complexType.modelGroup.elementChildrenInNonHiddenContext
        nonHiddenElems.groupBy(...).filter(...)...foreach(SDW(...))
        nonHiddenElems.foreach {  _.checkIfMulitipleChildrenWithSameName }
     }
   }
   ```
   
   Note that I've moved the check to an Element, I think that might make the check simpler? I'm not positive. In fact, maybe there want's to be a `elementChildrenInNonHiddenContext` implementation for ElementBase that does the is isComplex check and returns empty sequence if not complex, and returns `ct.group.elementChildrenNonHiddenContext` if it is..
   
   Doing it this way, when we recursively call `checkIfMultipleChildren...` we already know this element is not hidden so `elementChildrenInNonHiddenContext` doesn't need to check if parents are hidden--it just needs to assume anytime it is called that it is called in a know-to-not-be-hidden context if parent hidden-ness matters (probably needs a big comment making that assumption clear). And with that assumption, it only needs to look at the group members and filter out members that are hidden. I think that would make the logic of `elementChildrenInNonHiddenContext` similar to what you had before. Though, I think the ModelGroup case needs to be `gb.elementChildrenInNonHiddenContext` like you've changed in this one? And I think you can also combine the filter into the flatMap to simplify the logic a little, e.g.
   
   ```scala
   final lazy val elementChildrenInNonHiddenContext = {
     val ebs = groupMembers.flatMap { _ match {
       case eb: ElementBase => Seq(eb)
       case gr: GroupRef.isHidden => Nil
       case mg: ModelGroup => mg.elementChildrenInNonHiddenContext
     }
   }
   ``` 
   This makes it basically the same as `elementChildren`, just with an extra `GroupRef.isHidden` case. That being the only difference sort of feels right and that maybe that is the right approach.



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

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org