You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by mb...@apache.org on 2017/11/17 16:12:14 UTC

[5/6] incubator-daffodil git commit: Property Resolution - part of fixing schema compilation

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDecl.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDecl.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDecl.scala
index 347233d..7221cd9 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDecl.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDecl.scala
@@ -32,63 +32,26 @@
 
 package edu.illinois.ncsa.daffodil.dsom
 
-import edu.illinois.ncsa.daffodil.grammar._
+import scala.xml.Node
 
-/**
- * A global element decl uses LocalElementBase because it behaves like a local
- * element when you consider that except for the root case, it has to be combined
- * with an ElementRef that references it. The ElementRef can carry the things
- * like min/maxOccurs and such that aren't allowed on a GlobalElementDecl. The
- * combination of an ElementRef plus its GlobalElementDecl behaves like a LocalElementDecl.
- */
 final class GlobalElementDecl(
-  val factory: GlobalElementDeclFactory,
-  override val elementRef: Option[ElementRef])
-  extends LocalElementBase(factory.xml, factory.schemaDocument, 0)
+  xmlArg: Node,
+  schemaDocument: SchemaDocument,
+  elementRefArg: => AbstractElementRef)
+  extends AnnotatedSchemaComponentImpl(xmlArg, schemaDocument)
   with GlobalElementComponentMixin
   with ElementDeclMixin
-  with GlobalElementDeclGrammarMixin
-  with NestingTraversesToReferenceMixin
-  /*
-   * global elements combined with element references referring to them can
-   * be multiple occurring (aka arrays) hence, we have to have things
-   * that take root and referenced situation into account.
-   */
-  with RequiredOptionalMixin {
-
-  requiredEvaluations(validateChoiceBranchKey)
-
-  override lazy val maxOccurs = elementRef match {
-    case Some(er) => er.maxOccurs
-    case None => 1
-  }
-
-  override lazy val minOccurs = elementRef match {
-    case Some(er) => er.minOccurs
-    case None => 1
-  }
-
-  lazy val isRoot = elementRef == None
-
-  override lazy val isHidden = if (isRoot) false else elementRef.get.isHidden
+  with NestingTraversesToReferenceMixin {
+  //   global elements combined with element references referring to them can
+  //   be multiple occurring (aka arrays) hence, we have to have things
+  //   that take root and referenced situation into account.
 
-  // final override protected def enclosingComponentDef = elementRef.flatMap { _.enclosingComponent }
+  lazy val elementRef = elementRefArg
+  override lazy val dpathCompileInfo = elementRef.dpathElementCompileInfo
 
-  override lazy val referringComponent: Option[SchemaComponent] = elementRef
-
-  // GlobalElementDecls need to have access to elementRef's local properties.
-
-  // We inherit the requirement for these attributes from Term. It all gets
-  // too complicated in DSOM if you try to make GlobalElementDecl share with the other
-  // element structures but not be a Term.
-  //
-  // But a GlobalElementDecl isn't really a Term except in a degenerate sense
-  // that the root element is sort of a Term.
-  //
-  // In other words, we shouldn't be treating this as a term.
-  //
+  requiredEvaluations(validateChoiceBranchKey)
 
-  lazy val rootParseUnparsePolicy = defaultParseUnparsePolicy
+  override lazy val referringComponent: Option[SchemaComponent] = Some(elementRef) // optElementRef
 
   def validateChoiceBranchKey(): Unit = {
     // Ensure that the global element decl does not have choiceBranchKey set.

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDeclFactory.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDeclFactory.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDeclFactory.scala
index 114200b..f698012 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDeclFactory.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GlobalElementDeclFactory.scala
@@ -50,10 +50,15 @@ class GlobalElementDeclFactory(xmlArg: Node, schemaDocumentArg: SchemaDocument)
   with GlobalElementComponentMixin {
 
   def forRoot() = asRoot // cache. Not a new one every time.
-  lazy val asRoot = new GlobalElementDecl(this, None)
 
-  def forElementRef(eRef: ElementRef) = {
-    new GlobalElementDecl(this, Some(eRef))
+  private lazy val asRoot = {
+    lazy val ged = new GlobalElementDecl(xml, parent, root)
+    lazy val root: Root = new Root(xml, schemaDocument, namedQName, ged)
+    root
+  }
+
+  def forElementRef(eRef: AbstractElementRef) = {
+    new GlobalElementDecl(xml, schemaDocument, eRef)
   }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupBase.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupBase.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupBase.scala
deleted file mode 100644
index 6b39c36..0000000
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupBase.scala
+++ /dev/null
@@ -1,92 +0,0 @@
-/* Copyright (c) 2012-2015 Tresys Technology, LLC. All rights reserved.
- *
- * Developed by: Tresys Technology, LLC
- *               http://www.tresys.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal with
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
- * of the Software, and to permit persons to whom the Software is furnished to do
- * so, subject to the following conditions:
- *
- *  1. Redistributions of source code must retain the above copyright notice,
- *     this list of conditions and the following disclaimers.
- *
- *  2. Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimers in the
- *     documentation and/or other materials provided with the distribution.
- *
- *  3. Neither the names of Tresys Technology, nor the names of its contributors
- *     may be used to endorse or promote products derived from this Software
- *     without specific prior written permission.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
- * SOFTWARE.
- */
-
-package edu.illinois.ncsa.daffodil.dsom
-
-import scala.xml.Node
-import scala.xml._
-import edu.illinois.ncsa.daffodil.schema.annotation.props.AlignmentType
-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.AlignmentUnits
-import java.lang.{ Integer => JInt }
-
-abstract class GroupBase(xmlArg: Node, parentArg: SchemaComponent, position: Int)
-  extends Term(xmlArg, parentArg, position) {
-
-  final override def isScalar = true
-  final override def isOptional = false
-  final override def isRequired = true
-  final override def isArray = false
-
-  private def prettyIndex = {
-    myPeers.map { peers =>
-      {
-        if (peers.length == 1) "" // no index expression if we are the only one
-        else "[" + (peers.indexOf(this) + 1) + "]" // 1-based indexing in XML/XSD
-      }
-    }.getOrElse("")
-  }
-
-  override lazy val diagnosticDebugName = prettyBaseName + prettyIndex
-  protected def prettyBaseName: String
-
-  /**
-   * This is only the immediately enclosing model group. It doesn't walk outward.
-   */
-  final lazy val enclosingComponentModelGroup = enclosingComponent.collect { case mg: ModelGroup => mg }
-  final lazy val sequencePeers = enclosingComponentModelGroup.map { _.sequenceChildren }
-  final lazy val choicePeers = enclosingComponentModelGroup.map { _.choiceChildren }
-  final lazy val groupRefPeers = enclosingComponentModelGroup.map { _.groupRefChildren }
-
-  protected def myPeers: Option[Seq[GroupBase]]
-
-  def group: ModelGroup
-
-  final lazy val immediateGroup: Option[ModelGroup] = {
-    val res: Option[ModelGroup] = this.group match {
-      case (s: Sequence) => Some(s)
-      case (c: Choice) => Some(c)
-      case _ => None
-    }
-    res
-  }
-
-  final lazy val alignmentValueInBits: JInt = {
-    this.alignment match {
-      case AlignmentType.Implicit => 1
-      case align: JInt => this.alignmentUnits match {
-        case AlignmentUnits.Bits => align
-        case AlignmentUnits.Bytes => 8 * align
-      }
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupDef.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupDef.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupDef.scala
index 0bd984a..5992095 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupDef.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupDef.scala
@@ -34,70 +34,128 @@ package edu.illinois.ncsa.daffodil.dsom
 
 import scala.xml.Node
 import edu.illinois.ncsa.daffodil.exceptions.Assert
-import edu.illinois.ncsa.daffodil.xml.QName
+import scala.xml.Text
+import scala.xml.Comment
 
-class GlobalGroupDefFactory(xmlArg: Node, schemaDocumentArg: SchemaDocument)
-  extends SchemaComponentFactory(xmlArg, schemaDocumentArg)
+final class GlobalGroupDefFactory(defXML: Node, schemaDocument: SchemaDocument)
+  extends SchemaComponentFactory(defXML, schemaDocument)
   with GlobalNonElementComponentMixin {
 
-  private lazy val trimmedXml = scala.xml.Utility.trim(xmlArg)
-
-  def forGroupRef(gref: GroupRef, position: Int) = {
+  def forGroupRef(refXML: Node, refLexicalParent: SchemaComponent, position: Int,
+    isHidden: Boolean): (GroupRef, GlobalGroupDef) = {
+    val trimmedXml = scala.xml.Utility.trim(defXML)
     trimmedXml match {
       case <group>{ contents @ _* }</group> => {
-        val ggd = contents.collect {
-          case <sequence>{ _* }</sequence> => new GlobalSequenceGroupDef(xml, schemaDocument, gref, position)
-          case <choice>{ _* }</choice> => new GlobalChoiceGroupDef(xml, schemaDocument, gref, position)
+        val list = contents.collect {
+          case groupXML @ <sequence>{ _* }</sequence> => {
+            lazy val gref: SequenceGroupRef = new SequenceGroupRef(gdef, refXML, refLexicalParent, position, isHidden)
+            lazy val gdef: GlobalSequenceGroupDef = new GlobalSequenceGroupDef(defXML, groupXML, schemaDocument, gref)
+            gref.groupDef
+            gdef.groupRef
+            (gref, gdef)
+          }
+          case groupXML @ <choice>{ _* }</choice> =>
+            lazy val gref: ChoiceGroupRef = new ChoiceGroupRef(gdef, refXML, refLexicalParent, position, isHidden)
+            lazy val gdef: GlobalChoiceGroupDef = new GlobalChoiceGroupDef(defXML, groupXML, schemaDocument, gref)
+            gref.groupDef
+            gdef.groupRef
+            (gref, gdef)
         }
-        Assert.invariant(ggd.length == 1)
-        ggd(0)
+        val res = list(0)
+        res
       }
       case _ => Assert.invariantFailed("not a group")
     }
   }
+}
+
+/**
+ * Common concepts for components that define groups.
+ *
+ * This includes both global group definitions, and local sequence
+ * and choice groups.
+ */
+trait GroupDefLike
+  extends AnnotatedSchemaComponent
+  with ProvidesDFDLStatementMixin {
 
-  override lazy val namedQName = QName.createGlobal(name, targetNamespace, xml.scope)
+  def xmlChildren: Seq[Node]
 
+  /** Returns the group members that are elements or model groups. */
+  final lazy val groupMembers: Seq[Term] = LV('groupMembers) {
+    //
+    // So we have to flatMap, so that we can tolerate annotation objects (like documentation objects).
+    // and our ModelGroup factory has to return Nil for annotations and Text nodes.
+    //
+    val goodXmlChildren = LV('goodXMLChildren) { xmlChildren.flatMap { removeNonInteresting(_) } }.value
+    val positions = List.range(1, goodXmlChildren.length + 1) // range is exclusive on 2nd arg. So +1.
+    val pairs = goodXmlChildren zip positions
+    pairs.flatMap {
+      case (n, i) =>
+        TermFactory(n, this, i)
+    }
+  }.value
+
+  /**
+   * XML is full of uninteresting text nodes. We just want the element children, not all children.
+   */
+  private def removeNonInteresting(child: Node) = {
+    val childList: List[Node] = child match {
+      case _: Text => Nil
+      case _: Comment => Nil
+      case <annotation>{ _* }</annotation> => Nil
+      case _ => List(child)
+    }
+    childList
+  }
 }
 
-sealed abstract class GlobalGroupDef(xmlArg: Node, schemaDocumentArg: SchemaDocument, val groupRef: GroupRef, position: Int)
-  extends SchemaComponent(xmlArg, schemaDocumentArg)
-  with GlobalNonElementComponentMixin 
+/**
+ * Global Group Defs carry annotations that are combined wiht those
+ * of the corresponding group reference that refers to them.
+ *
+ * These are not carried on the xs:group element itself, but the xs:sequence
+ * or xs:choice XML child. When we refer to the annotations on a global group
+ * definition, we are referring to the annotations on the xs:sequence or xs:choice.
+ */
+sealed abstract class GlobalGroupDef(
+  defXML: Node,
+  groupXML: Node,
+  schemaDocumentArg: SchemaDocument,
+  grefArg: => GroupRef)
+  extends AnnotatedSchemaComponentImpl(groupXML, schemaDocumentArg)
+  with GroupDefLike
+  with GlobalNonElementComponentMixin
   with NestingTraversesToReferenceMixin {
 
-  requiredEvaluations(modelGroup)
+  requiredEvaluations(groupMembers)
   requiredEvaluations(validateChoiceBranchKey)
 
-  final lazy val referringComponent = {
-    val res = Some(groupRef)
-    res
-  }
-
-  // final override protected def enclosingComponentDef = groupRef.enclosingComponent
-
-  //
-  // Note: Dealing with XML can be fragile. It's easy to forget some of these children
-  // might be annotations and Text nodes. Even if you trim the text nodes out, there are
-  // places where annotations can be.
-  //
-  final lazy val <group>{ xmlChildren @ _* }</group> = xml
-  //
-  // So we have to flatMap, so that we can tolerate annotation objects (like documentation objects).
-  // and our ModelGroup factory has to return Nil for annotations and Text nodes.
-  //
-  final lazy val Seq(modelGroup: ModelGroup) = xmlChildren.flatMap { ModelGroupFactory(_, this, position) }
+  lazy val groupRef = grefArg // once only
 
   def validateChoiceBranchKey(): Unit = {
     // Ensure the model group of a global group def do not define choiceBranchKey.
-    val found = modelGroup.findPropertyOptionThisComponentOnly("choiceBranchKey")
+    val found = findPropertyOptionThisComponentOnly("choiceBranchKey")
     if (found.isDefined) {
       SDE("dfdl:choiceBranchKey cannot be specified on the choice/sequence child of a global group definition")
     }
   }
+
+  final override lazy val name = defXML.attribute("name").map { _.text }.getOrElse(
+    Assert.invariantFailed("Global group def without name attribute."))
+
+  final override protected def optReferredToComponent = None
+
+  final override lazy val referringComponent = Some(groupRef.asModelGroup)
 }
 
-final class GlobalSequenceGroupDef(xmlArg: Node, schemaDocument: SchemaDocument, groupRef: GroupRef, position: Int)
-  extends GlobalGroupDef(xmlArg, schemaDocument, groupRef, position)
+final class GlobalSequenceGroupDef(defXMLArg: Node, seqXML: Node, schemaDocument: SchemaDocument, gref: => SequenceGroupRef)
+  extends GlobalGroupDef(defXMLArg, seqXML, schemaDocument, gref)
+  with SequenceDefMixin
+
+final class GlobalChoiceGroupDef(defXMLArg: Node, choiceXML: Node, schemaDocument: SchemaDocument, gref: => ChoiceGroupRef)
+  extends GlobalGroupDef(defXMLArg, choiceXML, schemaDocument, gref)
+  with ChoiceDefMixin {
+
+}
 
-final class GlobalChoiceGroupDef(xmlArg: Node, schemaDocument: SchemaDocument, groupRef: GroupRef, position: Int)
-  extends GlobalGroupDef(xmlArg, schemaDocument, groupRef, position)

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupRef.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupRef.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupRef.scala
index 5803c32..9222711 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupRef.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/GroupRef.scala
@@ -34,82 +34,99 @@ package edu.illinois.ncsa.daffodil.dsom
 
 import scala.xml.Node
 import scala.xml._
-import edu.illinois.ncsa.daffodil.schema.annotation.props.SeparatorSuppressionPolicyMixin
-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.Group_AnnotationMixin
-import edu.illinois.ncsa.daffodil.grammar.GroupRefGrammarMixin
 import edu.illinois.ncsa.daffodil.xml.HasRefMixin
-import edu.illinois.ncsa.daffodil.schema.annotation.props.PropertyLookupResult
+import edu.illinois.ncsa.daffodil.schema.annotation.props.NotFound
 
-/**
- * A GroupRef (group reference) is a term, but most everything is delgated to the
- * referred-to Global Group Definition object.
- */
-final class GroupRef(xmlArg: Node, parent: SchemaComponent, position: Int)
-  extends GroupBase(xmlArg, parent, position)
-  with DFDLStatementMixin
-  with GroupRefGrammarMixin
-  with Group_AnnotationMixin
-  with SeparatorSuppressionPolicyMixin
-  with SequenceRuntimeValuedPropertiesMixin
-  with HasRefMixin
-  with NestingLexicalMixin {
+trait GroupRef { self: ModelGroup =>
 
-  requiredEvaluations(groupDef)
+  final def asModelGroup: ModelGroup = self
 
-  override lazy val elementChildren = group.elementChildren
+  def groupDef: GlobalGroupDef
 
-  // delegate to the model group object. It assembles properties from
-  // the group ref and the group def
-  override def findPropertyOption(pname: String): PropertyLookupResult = {
-    val res = group.findPropertyOption(pname)
-    res
-  }
-  lazy val nonDefaultPropertySources = group.nonDefaultPropertySources
-  lazy val defaultPropertySources = group.defaultPropertySources
+  final def referredToComponent = groupDef
 
-  protected lazy val prettyBaseName = "group.ref." + refQName.local
+  final override lazy val optReferredToComponent = Some(referredToComponent)
 
-  protected lazy val myPeers = groupRefPeers
+  final override lazy val groupMembers = groupDef.groupMembers
 
-  override lazy val termChildren: Seq[Term] = {
-    group.termChildren
+  override protected def annotationFactory(node: Node): Option[DFDLAnnotation] = {
+    node match {
+      case <dfdl:group>{ contents @ _* }</dfdl:group> => Some(new DFDLGroup(node, this))
+      case _ => annotationFactoryForDFDLStatement(node, self)
+    }
   }
 
-  final lazy val qname = resolveQName(ref)
+  override protected final def emptyFormatFactory: DFDLFormatAnnotation = new DFDLGroup(newDFDLAnnotationXML("group"), this)
 
-  protected def annotationFactory(node: Node): Option[DFDLAnnotation] = {
-    node match {
-      case <dfdl:group>{ contents @ _* }</dfdl:group> => Some(new DFDLGroup(node, this))
-      case _ => annotationFactoryForDFDLStatement(node, this)
+  override protected final def isMyFormatAnnotation(a: DFDLAnnotation) = a.isInstanceOf[DFDLAnnotation]
+
+}
+
+/**
+ * A GroupRefFactory (group reference) is an indirection to
+ * factories to create a SequenceGroupRef, or ChoiceGroupRef.
+ *
+ * The refXMLArg is the xml for the group reference.
+ *
+ * This factory exists in order to make error messages refer to the
+ * right part of the schema.
+ */
+final class GroupRefFactory(refXMLArg: Node, val refLexicalParent: SchemaComponent, position: Int, isHidden: Boolean)
+  extends SchemaComponentFactory(refXMLArg, refLexicalParent.schemaDocument)
+  with HasRefMixin {
+
+  final def qname = this.refQName
+
+  lazy val groupRef = {
+    val gdefFactory = parent.schemaSet.getGlobalGroupDef(qname).getOrElse {
+      SDE("Referenced group definition not found: %s", this.ref)
     }
+    val (gref, _) = gdefFactory.forGroupRef(refXMLArg, refLexicalParent, position, isHidden)
+    gref
   }
 
-  protected final def emptyFormatFactory = new DFDLGroup(newDFDLAnnotationXML("group"), this)
-  protected final def isMyFormatAnnotation(a: DFDLAnnotation) = a.isInstanceOf[DFDLGroup]
+}
 
-  def hasStaticallyRequiredInstances = group.hasStaticallyRequiredInstances
+final class SequenceGroupRef(globalGroupDef: => GlobalSequenceGroupDef,
+  refXML: Node,
+  refLexicalParent: SchemaComponent,
+  positionArg: Int,
+  isHiddenArg: Boolean)
+  extends SequenceTermBase(refXML, refLexicalParent, positionArg)
+  with GroupRef {
 
-  override lazy val group = groupDef.modelGroup
+  override def isHidden = isHiddenArg
 
-  override lazy val termRuntimeData = group.termRuntimeData
+  private lazy val sgd = groupDef.asInstanceOf[GlobalSequenceGroupDef]
 
-  override lazy val couldHaveText = group.couldHaveText
+  override def xmlChildren = sgd.xmlChildren
 
-  override lazy val referredToComponent = group
+  override def apparentXMLChildren = sgd.apparentXMLChildren
 
-  override lazy val encodingInfo = group.encodingInfo
+  private val nf = NotFound(Nil, Nil, "hiddenGroupRef")
 
-  lazy val groupDef = LV('groupDef) {
-    this.schemaSet.getGlobalGroupDef(qname) match {
-      case None => SDE("Referenced group definition not found: %s", this.ref)
-      case Some(x) => x.forGroupRef(this, position)
-    }
-  }.value
+  override def hiddenGroupRefOption = nf
+
+  override lazy val groupDef = globalGroupDef
+
+}
+
+final class ChoiceGroupRef(globalGroupDef: => GlobalChoiceGroupDef,
+  refXML: Node,
+  refLexicalParent: SchemaComponent,
+  positionArg: Int,
+  isHiddenArg: Boolean)
+  extends ChoiceTermBase(refXML, refLexicalParent, positionArg)
+  with GroupRef {
+
+  requiredEvaluations(groupDef)
+
+  override def isHidden = isHiddenArg
+
+  override lazy val groupDef = globalGroupDef
+
+  private lazy val cgd = groupDef.asInstanceOf[GlobalChoiceGroupDef]
 
-  lazy val statements = localStatements
-  lazy val newVariableInstanceStatements = localNewVariableInstanceStatements
-  lazy val assertStatements = localAssertStatements
-  lazy val discriminatorStatements = localDiscriminatorStatements
-  lazy val setVariableStatements = localSetVariableStatements
+  override lazy val xmlChildren = cgd.xmlChildren
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/IIBase.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/IIBase.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/IIBase.scala
index 8160656..bfba75d 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/IIBase.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/IIBase.scala
@@ -137,9 +137,10 @@ object IIUtils {
 /**
  * Include/Import = "II" for short
  */
-abstract class IIBase(xml: Node, xsdArg: XMLSchemaDocument, val seenBefore: IIMap)
-  extends SchemaComponent(xml, xsdArg)
+abstract class IIBase( final override val xml: Node, xsdArg: XMLSchemaDocument, val seenBefore: IIMap)
+  extends SchemaComponent
   with NestingLexicalMixin {
+  final override def parent = xsdArg
 
   /**
    * An import/include requires only that we can access the

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/InitiatedTerminatedMixin.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/InitiatedTerminatedMixin.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/InitiatedTerminatedMixin.scala
index 6d76814..e5bad64 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/InitiatedTerminatedMixin.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/InitiatedTerminatedMixin.scala
@@ -60,7 +60,8 @@ trait InitiatedTerminatedMixin
   }
 
   final lazy val hasTerminator = {
-    terminatorExpr.isKnownNonEmpty
+    val res = terminatorExpr.isKnownNonEmpty
+    res
   }
 
   lazy val initiatorDiscriminator = prod("initiatorDiscriminator", parentSaysInitiatedContent) { InitiatedContent(this) }
@@ -75,7 +76,7 @@ trait InitiatedTerminatedMixin
    * present, or there is an expression. (Such expressions are not allowed to evaluate to "" - you
    * can't turn off a delimiter by providing "" at runtime. Minimum length is 1 for these at runtime.
    * <p>
-   * Override in Sequence to also check for separator.
+   * Override in SequenceTermBase to also check for separator.
    */
   lazy val hasDelimiters = hasInitiator || hasTerminator
 

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementBase.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementBase.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementBase.scala
deleted file mode 100644
index 910b3d4..0000000
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementBase.scala
+++ /dev/null
@@ -1,80 +0,0 @@
-/* Copyright (c) 2012-2015 Tresys Technology, LLC. All rights reserved.
- *
- * Developed by: Tresys Technology, LLC
- *               http://www.tresys.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal with
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
- * of the Software, and to permit persons to whom the Software is furnished to do
- * so, subject to the following conditions:
- *
- *  1. Redistributions of source code must retain the above copyright notice,
- *     this list of conditions and the following disclaimers.
- *
- *  2. Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimers in the
- *     documentation and/or other materials provided with the distribution.
- *
- *  3. Neither the names of Tresys Technology, nor the names of its contributors
- *     may be used to endorse or promote products derived from this Software
- *     without specific prior written permission.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
- * SOFTWARE.
- */
-
-package edu.illinois.ncsa.daffodil.dsom
-
-import scala.xml.Node
-
-abstract class LocalElementBase(xmlArg: Node, parent: SchemaComponent, position: Int)
-  extends ElementBase(xmlArg, parent, position)
-  with LocalElementMixin {
-
-  requiredEvaluations(checkForAlignmentAmbiguity)
-
-  private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
-  //  private def lcm(a: Int, b: Int): Int = math.abs(a * b) / gcd(a, b)
-  //  private def isXAMultipleOfY(x: Int, y: Int): Boolean = (x % y) == 0
-
-  //  private def isAlignmentCompatible(current: Int, next: Int): Boolean = {
-  //    isXAMultipleOfY(current, next)
-  //  }
-
-  /**
-   * Changed to a warning - DFDL WG decided to make this check optional, but it
-   * is still useful as a warning.
-   *
-   * Turns out that MIL STD 2045 header format needs to pad out to a byte boundary
-   * at the end of the structure. An optional, non-byte aligned field precedes
-   * the end of the structure; hence, putting a zero-length byte-aligned field
-   * at the end was crashing into this error. I couldn't think of a work-around,
-   * so changed this into a warning.
-   *
-   * The old requirement was:
-   *   To avoid ambiguity when parsing, optional elements and variable-occurrence arrays
-   *   where the minimum number of occurrences is zero cannot have alignment properties
-   *   different from the items that follow them. It is a schema definition error otherwise.
-   *
-   * Part of the required evaluations for LocalElementBase.
-   */
-  final def checkForAlignmentAmbiguity: Unit = {
-    if (isOptional) {
-      this.possibleNextTerms.filterNot(m => m == thisTermNoRefs).foreach { that =>
-        val isSame = this.alignmentValueInBits == that.alignmentValueInBits
-        if (!isSame) {
-          this.SDW("%s is an optional element or a variable-occurrence array and its alignment (%s) is not the same as %s's alignment (%s).",
-            this.toString, this.alignmentValueInBits, that.toString, that.alignmentValueInBits)
-        }
-      }
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementDecl.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementDecl.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementDecl.scala
index 1b12568..abd9868 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementDecl.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementDecl.scala
@@ -34,27 +34,15 @@ package edu.illinois.ncsa.daffodil.dsom
 
 import scala.xml.Node
 
-final class LocalElementDeclFactory(override val xml: Node, sd: SchemaDocument)
-  extends SchemaComponentFactory(xml, sd)
-  with LocalElementComponentMixin {
-
-  def forModelGroup(parent: ModelGroup, position: Int) =
-    new LocalElementDecl(this, parent, position)
-
-}
-
-final class LocalElementDecl(factory: LocalElementDeclFactory,
-  override val parent: ModelGroup,
-  override val position: Int)
-  extends LocalElementBase(factory.xml, parent, position)
+final class LocalElementDecl(
+  final override val xml: Node,
+  final override val parent: GroupDefLike,
+  final override val position: Int)
+  extends ElementBase
   with LocalElementComponentMixin
   with ElementDeclMixin
   with NestingLexicalMixin {
 
-  override lazy val elementRef = None
-
   requiredEvaluations(minOccurs, maxOccurs)
 
-  final override lazy val namedQName = factory.namedQName
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementMixin.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementMixin.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementMixin.scala
index e83b78c..ec78d8a 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementMixin.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/LocalElementMixin.scala
@@ -45,7 +45,7 @@ import java.math.{ BigInteger => JBigInt }
  */
 trait LocalElementMixin
   extends ParticleMixin
-  with LocalElementGrammarMixin { self: LocalElementBase =>
+  with LocalElementGrammarMixin { self: ElementBase =>
 
   final def hasSep = LV('hasSep) {
     nearestEnclosingSequence match {
@@ -120,7 +120,7 @@ trait LocalElementMixin
       val res = es match {
         case None => true
         case Some(s) => {
-          val allRequired = s.groupMembersNoRefs.filter(_.isRequired)
+          val allRequired = s.groupMembers.filter(_.isRequired)
           val lastDeclaredRequired = allRequired.last
           if (lastDeclaredRequired _eq_ this) true
           else false

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ModelGroup.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ModelGroup.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ModelGroup.scala
index 6f80714..97dc397 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ModelGroup.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ModelGroup.scala
@@ -32,7 +32,6 @@
 
 package edu.illinois.ncsa.daffodil.dsom
 
-import scala.Option.option2Iterable
 import scala.xml.Node
 import scala.xml.NodeSeq.seqToNodeSeq
 import scala.xml.Text
@@ -45,26 +44,49 @@ import edu.illinois.ncsa.daffodil.grammar.ModelGroupGrammarMixin
 import edu.illinois.ncsa.daffodil.infoset.ChoiceBranchEvent
 import edu.illinois.ncsa.daffodil.infoset.ChoiceBranchStartEvent
 import edu.illinois.ncsa.daffodil.infoset.ChoiceBranchEndEvent
+import java.lang.{ Integer => JInt }
+import edu.illinois.ncsa.daffodil.schema.annotation.props.AlignmentType
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.AlignmentUnits
 
 /**
  * A factory for model groups.
+ *
+ * Takes care of detecting group references, and constructing the
+ * proper SequenceGroupRef or ChoiceGroupRef object.
  */
-sealed abstract class ModelGroupFactory private () {
+object ModelGroupFactory {
 
   /**
    * Because of the contexts where this is used, we return a list. That lets users
    * flatmap it to get a collection of model groups. Nil for non-model groups, non-Nil for the model group
    * object. There should be only one non-Nil.
    */
-  def apply(child: Node, parent: SchemaComponent, position: Int) = {
-    val childList: List[GroupBase] = child match {
-      case <sequence>{ _* }</sequence> => List(new Sequence(child, parent, position))
+  def apply(child: Node, parent: SchemaComponent, position: Int, isHidden: Boolean): List[ModelGroup] = {
+    val childList: List[ModelGroup] = child match {
+      case <sequence>{ _* }</sequence> => {
+        val seq = new Sequence(child, parent, position)
+        if (seq.hiddenGroupRefOption.isDefined) {
+          //
+          // construct the group ref XML, then recursively process that,
+          // but set flag so it will be hidden.
+          //
+          val hgrXML = seq.hiddenGroupRefXML
+          ModelGroupFactory(hgrXML, parent, position, true)
+        } else {
+          List(seq)
+        }
+      }
       case <choice>{ _* }</choice> => List(new Choice(child, parent, position))
       case <group>{ _* }</group> => {
-        parent match {
-          case ct: ComplexTypeBase => List(new GroupRef(child, ct, 1))
-          case mg: ModelGroup => List(new GroupRef(child, mg, position))
+        val pos = parent match {
+          case ct: ComplexTypeBase => 1
+          case mg: ModelGroup => position
+          case gd: GlobalGroupDef => position
         }
+        val isH = isHidden || parent.isHidden
+        val groupRefFactory = new GroupRefFactory(child, parent, pos, isH)
+        val groupRefInstance = groupRefFactory.groupRef
+        List(groupRefInstance.asModelGroup)
       }
       case <annotation>{ _* }</annotation> => Nil
       case textNode: Text => Nil
@@ -78,25 +100,96 @@ sealed abstract class ModelGroupFactory private () {
 
 }
 
-object ModelGroupFactory extends ModelGroupFactory()
+/**
+ * Factory for Terms
+ */
+object TermFactory {
+
+  /**
+   * Returns a List of Term. There should be exactly one Term in the list.
+   *
+   * List, not one term, because of the context where this is used, non-Nil for
+   * an actual term. There should be only one non-Nil allows flattening to
+   * remove all the parts of the schema that are not relevant.
+   *
+   */
+  def apply(child: Node, parent: GroupDefLike, position: Int) = {
+    val childList: List[Term] = child match {
+      case <element>{ _* }</element> => {
+        val refProp = child.attribute("ref").map { _.text }
+        // must get an unprefixed attribute name, i.e. ref='foo:bar', and not
+        // be tripped up by dfdl:ref="fmt:fooey" which is a format reference.
+        refProp match {
+          case None => {
+            val eDecl = new LocalElementDecl(child, parent, position)
+            List(eDecl)
+          }
+          case Some(_) => List(new ElementRef(child, parent, position))
+        }
+      }
+      case <annotation>{ _* }</annotation> => Nil
+      case textNode: Text => Nil
+      case _ => ModelGroupFactory(child, parent, position, false)
+    }
+    childList
+  }
+}
 
 /**
  * Base class for all model groups, which are term containers.
+ *
+ * There are ultimately 4 concrete classes that implement this:
+ * Sequence, Choice, SequenceGroupRef, and ChoiceGroupRef
  */
-abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: Int)
-  extends GroupBase(xmlArg, parentArg, position)
-  with DFDLStatementMixin
+abstract class ModelGroup
+  extends Term
   with ModelGroupGrammarMixin
   with OverlapCheckMixin
-  with RealTermMixin
   with NestingLexicalMixin {
 
   requiredEvaluations(groupMembers)
 
+  def groupMembers: Seq[Term]
+  def xmlChildren: Seq[Node]
+  protected def myPeers: Option[Seq[ModelGroup]]
+
+  final override def isScalar = true
+  final override def isOptional = false
+  final override def isRequired = true
+  final override def isArray = false
+
+  private def prettyIndex = LV('prettyIndex) {
+    myPeers.map { peers =>
+      {
+        if (peers.length == 1) "" // no index expression if we are the only one
+        else "[" + (peers.indexOf(this) + 1) + "]" // 1-based indexing in XML/XSD
+      }
+    }.getOrElse("")
+  }.value
+
+  override lazy val diagnosticDebugName = prettyBaseName + prettyIndex
+
+  /**
+   * This is only the immediately enclosing model group. It doesn't walk outward.
+   */
+  final lazy val enclosingComponentModelGroup = enclosingComponent.collect { case mg: ModelGroup => mg }
+  final lazy val sequencePeers = enclosingComponentModelGroup.map { _.sequenceChildren }
+  final lazy val choicePeers = enclosingComponentModelGroup.map { _.choiceChildren }
+
+  override lazy val alignmentValueInBits: JInt = {
+    this.alignment match {
+      case AlignmentType.Implicit => 1
+      case align: JInt => this.alignmentUnits match {
+        case AlignmentUnits.Bits => align
+        case AlignmentUnits.Bytes => 8 * align
+      }
+    }
+  }
+
   final lazy val elementChildren: Seq[ElementBase] =
     groupMembers.flatMap {
       case eb: ElementBase => Seq(eb)
-      case gb: GroupBase => gb.group.elementChildren
+      case gb: ModelGroup => gb.elementChildren
     }
 
   final override lazy val runtimeData: RuntimeData = modelGroupRuntimeData
@@ -115,118 +208,25 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
 
   def modelGroupRuntimeData: ModelGroupRuntimeData
 
-  final lazy val gRefNonDefault: Option[ChainPropProvider] = groupRef.map { _.nonDefaultFormatChain }
-  final lazy val gRefDefault: Option[ChainPropProvider] = groupRef.map { _.defaultFormatChain }
-
-  final def nonDefaultPropertySources = LV('nonDefaultPropertySources) {
-    val seq = (gRefNonDefault.toSeq ++ Seq(this.nonDefaultFormatChain)).distinct
-    checkNonOverlap(seq)
-    seq
-  }.value
-
-  final def defaultPropertySources = LV('defaultPropertySources) {
-    val seq = (gRefDefault.toSeq ++ Seq(this.defaultFormatChain)).distinct
-    seq
-  }.value
-
-  protected final lazy val prettyBaseName = xmlArg.label
+  protected final lazy val prettyBaseName = xml.label
 
-  protected def xmlChildren: Seq[Node]
-
-  private def goodXmlChildren = LV('goodXMLChildren) { xmlChildren.flatMap { removeNonInteresting(_) } }.value
-  private lazy val positions = List.range(1, goodXmlChildren.length + 1) // range is exclusive on 2nd arg. So +1.
-  private lazy val pairs = goodXmlChildren zip positions
-
-  final lazy val sequenceChildren = groupMembers.collect { case s: Sequence => s }
-  final lazy val choiceChildren = groupMembers.collect { case s: Choice => s }
+  final lazy val sequenceChildren = groupMembers.collect { case s: SequenceTermBase => s }
+  final lazy val choiceChildren = groupMembers.collect { case s: ChoiceTermBase => s }
   final lazy val groupRefChildren = groupMembers.collect { case s: GroupRef => s }
 
-  final def group = this
-
-  final lazy val groupMembers = {
-    pairs.flatMap {
-      case (n, i) =>
-        termFactory(n, this, i)
-    }
-  }
-
   final override lazy val termChildren = groupMembers
 
-  final lazy val groupMembersNoRefs = groupMembers.map {
-    case eRef: ElementRef => eRef.referencedElement
-    case gb: GroupBase => gb.group
-    case x => x
-  }
-
-  /**
-   * Factory for Terms
-   *
-   * Because of the context where this is used, this returns a list. Nil for non-terms, non-Nil for
-   * an actual term. There should be only one non-Nil.
-   *
-   * This could be static code in an object. It doesn't reference any of the state of the ModelGroup,
-   * it's here so that type-specific overrides are possible in Sequence or Choice
-   */
-  private def termFactory(child: Node, parent: ModelGroup, position: Int) = {
-    val childList: List[Term] = child match {
-      case <element>{ _* }</element> => {
-        val refProp = child.attribute("ref").map { _.text }
-        // must get an unprefixed attribute name, i.e. ref='foo:bar', and not
-        // be tripped up by dfdl:ref="fmt:fooey" which is a format reference.
-        refProp match {
-          case None => List(schemaSet.LocalElementDeclFactory(child, schemaDocument).forModelGroup(parent, position))
-          case Some(_) => List(new ElementRef(child, parent, position))
-        }
-      }
-      case <annotation>{ _* }</annotation> => Nil
-      case textNode: Text => Nil
-      case _ => ModelGroupFactory(child, parent, position)
-    }
-    childList
-  }
-
   /**
-   * XML is full of uninteresting text nodes. We just want the element children, not all children.
+   * Returns tuple, where the first is children that could be last, and the
+   * second is a boolean if all children could be optional, and thus this could
+   * be last
    */
-  private def removeNonInteresting(child: Node) = {
-    val childList: List[Node] = child match {
-      case _: Text => Nil
-      case _: Comment => Nil
-      case <annotation>{ _* }</annotation> => Nil
-      case _ => List(child)
-    }
-    childList
-  }
-
-  /**
-   * Combine our statements with those of the group ref that is referencing us (if there is one)
-   */
-  final lazy val statements: Seq[DFDLStatement] = localStatements ++ groupRef.map { _.statements }.getOrElse(Nil)
-  final lazy val newVariableInstanceStatements: Seq[DFDLNewVariableInstance] =
-    localNewVariableInstanceStatements ++ groupRef.map { _.newVariableInstanceStatements }.getOrElse(Nil)
-  final lazy val (discriminatorStatements, assertStatements) = checkDiscriminatorsAssertsDisjoint(combinedDiscrims, combinedAsserts)
-  private lazy val combinedAsserts: Seq[DFDLAssert] = localAssertStatements ++ groupRef.map { _.assertStatements }.getOrElse(Nil)
-  private lazy val combinedDiscrims: Seq[DFDLDiscriminator] = localDiscriminatorStatements ++ groupRef.map { _.discriminatorStatements }.getOrElse(Nil)
-
-  final lazy val setVariableStatements: Seq[DFDLSetVariable] = {
-    val combinedSvs = localSetVariableStatements ++ groupRef.map { _.setVariableStatements }.getOrElse(Nil)
-    checkDistinctVariableNames(combinedSvs)
-  }
-
-  final lazy val groupRef = parent match {
-    case ggd: GlobalGroupDef => Some(ggd.groupRef)
-    case _ => None
-  }
-
-  // returns tuple, where the first is children that could be last, and the
-  // second is a boolean if all children could be optional, and thus this could
-  // be last
   lazy val potentialLastChildren: (Seq[Term], Boolean) = {
     val (potentialLast, allOptional) = this match {
-      case ch: Choice => (ch.groupMembersNoRefs, false)
-      case sq: Sequence if !sq.isOrdered => (sq.groupMembersNoRefs, true) // TBD: is true correct? Are all children optional in unordered sequence?
-      case sq: Sequence => {
-        val maybeLast = sq.groupMembersNoRefs.lastOption
+      case ch: ChoiceTermBase => (ch.groupMembers, false)
+      case sq: SequenceTermBase if !sq.isOrdered => (sq.groupMembers, true) // TBD: is true correct? Are all children optional in unordered sequence?
+      case sq: SequenceTermBase => {
+        val maybeLast = sq.groupMembers.lastOption
         if (maybeLast.isDefined) {
           val last = maybeLast.get
           val lastIsOptional = last match {
@@ -255,10 +255,10 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
 
   final def allSelfContainedTermsTerminatedByRequiredElement: Seq[Term] =
     LV('allSelfContainedTermsTerminatedByRequiredElement) {
-      val listOfTerms = groupMembersNoRefs.map(m => {
+      val listOfTerms = groupMembers.map(m => {
         m match {
-          case e: LocalElementBase if e.isOptional => (Seq(e) ++ e.possibleNextTerms) // A LocalElement or ElementRef
-          case e: LocalElementBase => Seq(e)
+          case e: ElementBase if e.isOptional => (Seq(e) ++ e.possibleNextTerms) // A LocalElement or ElementRef
+          case e: ElementBase => Seq(e)
           case mg: ModelGroup => Seq(mg)
         }
       }).flatten
@@ -266,7 +266,7 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
     }.value
 
   final def identifyingEventsForChoiceBranch: Seq[ChoiceBranchEvent] = LV('identifyingEventsForChoiceBranch) {
-    Assert.usage(enclosingTerm.isDefined && enclosingTerm.get.isInstanceOf[Choice], "identifyingElementsForChoiceBranch must only be called on children of choices")
+    Assert.usage(enclosingTerm.isDefined && enclosingTerm.get.isInstanceOf[ChoiceTermBase], "identifyingElementsForChoiceBranch must only be called on children of choices")
 
     val childrenIdentifiers = possibleFirstChildElementsInInfoset
     val parentNextIdentifiers =
@@ -314,10 +314,10 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
    */
   protected final def possibleFirstChildTerms: Seq[Term] = LV('possibleFirstChildTerms) {
     val firstTerms = this match {
-      case c: Choice => groupMembersNoRefs
-      case s: Sequence if !s.isOrdered => groupMembersNoRefs
-      case s: Sequence => {
-        groupMembersNoRefs.headOption match {
+      case c: ChoiceTermBase => groupMembers
+      case s: SequenceTermBase if !s.isOrdered => groupMembers
+      case s: SequenceTermBase => {
+        groupMembers.headOption match {
           case None => Nil
           case Some(e: ElementBase) if e.canBeAbsentFromUnparseInfoset => {
             // this case covers optional elements, arrrays with minOccurs = 0,
@@ -326,7 +326,7 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
             // follow it
             Seq(e) ++ e.possibleNextSiblingTerms
           }
-          case Some(s: Sequence) if s.hiddenGroupRefOption.isDefined => s.possibleNextSiblingTerms
+          case Some(s: SequenceTermBase) if s.isHidden => s.possibleNextSiblingTerms
           case Some(mg: ModelGroup) if !mg.mustHaveRequiredElement => Seq(mg) ++ mg.possibleNextSiblingTerms
           case Some(e: ElementBase) => Seq(e)
           case Some(mg: ModelGroup) => Seq(mg)
@@ -351,7 +351,7 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
     }
   }
 
-  // model groups can't be elements.
+  /** Always false as model groups can't be elements.*/
   protected final def couldBeLastElementInModelGroup: Boolean = false
 
   /*
@@ -362,7 +362,7 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
   private def hasRequiredNextSiblingElement: Boolean = LV('hasRequiredNextSiblingElement) {
     val hasRequired = enclosingTerm match {
       case None => false
-      case Some(s: Sequence) if s.isOrdered => {
+      case Some(s: SequenceTermBase) if s.isOrdered => {
         // possibleNextSiblingTerms is either all optional/does not have a
         // required element, or the last one is required. Thus, this has a
         // required next sibling if the last sibling element is required
@@ -386,14 +386,14 @@ abstract class ModelGroup(xmlArg: Node, parentArg: SchemaComponent, position: In
    */
   final def mustHaveRequiredElement: Boolean = LV('mustHaveRequiredElement) {
     this match {
-      case s: Sequence if s.hiddenGroupRefOption.isDefined => false
-      case s: Sequence if s.isOrdered =>
-        groupMembersNoRefs.exists {
+      case s: SequenceTermBase if s.isHidden => false
+      case s: SequenceTermBase if s.isOrdered =>
+        groupMembers.exists {
           case e: ElementBase => !e.canBeAbsentFromUnparseInfoset
           case mg: ModelGroup => mg.mustHaveRequiredElement
         }
       case _ =>
-        groupMembersNoRefs.forall {
+        groupMembers.forall {
           case e: ElementBase => !e.canBeAbsentFromUnparseInfoset
           case mg: ModelGroup => mg.mustHaveRequiredElement
         }

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Nesting.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Nesting.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Nesting.scala
index 1536645..4ad0633 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Nesting.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Nesting.scala
@@ -40,12 +40,12 @@ trait NestingMixin {
   def parent: SchemaComponent
 
   /**
-   * Define this for schema components that have back-references to ref 
+   * Define this for schema components that have back-references to ref
    * objects.
    * So group def to group ref, globalelementdecl to element ref,
    * type to element, base type to derived type.
-   * 
-   * Not for format annotations however. We don't backpoint those to 
+   *
+   * Not for format annotations however. We don't backpoint those to
    * other format annotations that ref them.
    */
   protected def enclosingComponentDef: Option[SchemaComponent]
@@ -60,14 +60,7 @@ trait NestingMixin {
    *  referenced from a element ref or group ref, is NOT the ref object, but the
    *  component that contains the ref object
    */
-  final lazy val enclosingComponent: Option[SchemaComponent] = {
-    val optECD = enclosingComponentDef
-    optECD match {
-      case Some(eref: ElementRef) => eref.enclosingComponent
-      case Some(gref: GroupRef) => gref.enclosingComponent
-      case _ => optECD
-    }
-  }
+  final lazy val enclosingComponent: Option[SchemaComponent] = enclosingComponentDef
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ParticleMixin.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ParticleMixin.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ParticleMixin.scala
index 737d07d..066bc0e 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ParticleMixin.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/ParticleMixin.scala
@@ -113,8 +113,8 @@ trait RequiredOptionalMixin { self: ElementBase =>
 // A Particle is something that can be repeating.
 trait ParticleMixin extends RequiredOptionalMixin { self: ElementBase =>
 
-  final override lazy val optMinOccurs: Option[Int] = Some(minOccurs)
-  final override lazy val optMaxOccurs: Option[Int] = Some(maxOccurs)
+  final lazy val optMinOccurs: Option[Int] = Some(minOccurs)
+  final lazy val optMaxOccurs: Option[Int] = Some(maxOccurs)
 
   lazy val minOccurs = {
     val min = (self.xml \ "@minOccurs").text.toString

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/PropProviders.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/PropProviders.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/PropProviders.scala
index 9d1da6c..f7a6642 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/PropProviders.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/PropProviders.scala
@@ -120,6 +120,8 @@ trait LeafPropProvider
 final class ChainPropProvider(leafProvidersArg: Seq[LeafPropProvider], forAnnotation: String)
   extends Logging with PropTypes {
 
+  override def toString() = Misc.getNameFromClass(this) + "(" + forAnnotation + ")"
+
   /**
    * for debug/test only
    */

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RealTermMixin.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RealTermMixin.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RealTermMixin.scala
index 4cfbbac..1f7e629 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RealTermMixin.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RealTermMixin.scala
@@ -32,7 +32,6 @@
 
 package edu.illinois.ncsa.daffodil.dsom
 
-import edu.illinois.ncsa.daffodil.exceptions.Assert
 import edu.illinois.ncsa.daffodil.schema.annotation.props.PropertyLookupResult
 
 trait PropertyReferencedElementInfosMixin {
@@ -49,9 +48,6 @@ trait PropertyReferencedElementInfosMixin {
     if (rawProp.isDefined) f(ev) else Set()
   }
 
-  final protected def creis(rei: ContentValueReferencedElementInfoMixin) = rei.contentReferencedElementInfos
-  final protected def vreis(rei: ContentValueReferencedElementInfoMixin) = rei.valueReferencedElementInfos
-
   protected def propertyContentReferencedElementInfos: Set[DPathElementCompileInfo]
   protected def propertyValueReferencedElementInfos: Set[DPathElementCompileInfo]
 
@@ -69,226 +65,3 @@ trait PropertyReferencedElementInfosMixin {
 
 }
 
-/**
- * A RealTerm is an element or a sequence or a choice. Group references
- * are excluded as they go away and really have no realization.
- *
- * Things that apply to real terms, but not group references, go in this trait.
- */
-trait RealTermMixin { self: Term =>
-
-  final lazy val possibleNextTerms: Seq[Term] = LV('possibleNextTerms) {
-    val es = this.nearestEnclosingSequence
-    val eus = this.nearestEnclosingUnorderedSequenceBeforeSequence
-    val ec = this.nearestEnclosingChoiceBeforeSequence
-
-    val enclosingUnorderedGroup = {
-      (ec, eus) match {
-        case (None, None) => None
-        case (Some(choice), _) => Some(choice)
-        case (None, Some(uoSeq)) => Some(uoSeq)
-      }
-    }
-    val listOfNextTerm = (enclosingUnorderedGroup, es) match {
-      case (None, None) => Seq.empty
-      case (Some(unorderedGroup), _) => {
-        // We're in a choice or unordered sequence
-        //
-        // List must be all of our peers since (as well as our self)
-        // we could be followed by any of them plus
-        // whatever follows the unordered group.
-        val peersCouldBeNext = unorderedGroup.groupMembersNoRefs
-
-        // Note: There was a tiny difference here between the possibleNextTerms of LocalElementBase
-        // and the possibleNextTerms of model group. This difference appears to be that this
-        // version is more correct (from LocalElementBase).
-        val termsUntilFirstRequiredTerm = peersCouldBeNext ++ unorderedGroup.possibleNextTerms
-        termsUntilFirstRequiredTerm
-      }
-      case (None, Some(oSeq)) => {
-        // We're in an ordered sequence
-
-        val termsUntilFirstRequiredTerm =
-          isDeclaredLastInSequence match {
-            case true => oSeq.possibleNextTerms
-            case false => {
-
-              val members = oSeq.groupMembersNoRefs
-
-              val nextMember =
-                members.dropWhile(m => m != thisTermNoRefs).filterNot(m => m == thisTermNoRefs).headOption
-
-              val nextMembers =
-                nextMember match {
-                  case Some(e: LocalElementBase) if e.isOptional => Seq(e) ++ e.possibleNextTerms
-                  case Some(e: LocalElementBase) => Seq(e)
-                  case Some(gb: GroupBase) => Seq(gb.group)
-                  case None => Assert.impossibleCase // Difference: model group has Nil here.
-                }
-              nextMembers
-            }
-          }
-        termsUntilFirstRequiredTerm
-      }
-    }
-    listOfNextTerm
-  }.value
-
-  final def isDeclaredLastInSequence = LV('isDeclaredLastInSequence) {
-    val es = nearestEnclosingSequence
-    // how do we determine what child node we are? We search.
-    // TODO: better structure for O(1) answer to this.
-    es match {
-      case None => Assert.invariantFailed("We are not in a sequence therefore isDeclaredLastInSequence is an invalid question.")
-      case Some(s) => {
-        val members = s.groupMembersNoRefs
-        if (members.last eq thisTermNoRefs) true // we want object identity comparison here, not equality.
-        else false
-      }
-    }
-  }.value
-
-  protected def possibleFirstChildTerms: Seq[Term]
-
-  /*
-   * Returns list of Elements that could be the first child in the infoset of this model group or element.
-   */
-  final def possibleFirstChildElementsInInfoset: Seq[ElementBase] = LV('possibleFirstChildElementsInInfoset) {
-    val firstChildren = possibleFirstChildTerms.flatMap {
-      case e: ElementBase if e.isHidden => Nil
-      case e: ElementBase => Seq(e)
-      case s: Sequence if s.isHidden || s.hiddenGroupRefOption.isDefined => Nil
-      case mg: ModelGroup => mg.possibleFirstChildElementsInInfoset
-    }
-    firstChildren
-  }.value
-
-  /*
-   * Returns a list of Elements that could follow this Term, including
-   * siblings, children of siblings, and siblings of the parent and their children.
-   *
-   * What stops this is when the end of an enclosing element has to be next.
-   */
-  final def possibleNextChildElementsInInfoset: Seq[ElementBase] = LV('possibleNextChildElementsInInfoset) {
-    val arrayNext = if (isArray) Seq(this.asInstanceOf[ElementBase]) else Nil
-
-    val nextSiblingElements = {
-      val poss = possibleNextSiblingTerms
-      val res = poss.flatMap {
-        possible =>
-          possible match {
-            case e: ElementBase => Seq(e)
-            case mg: ModelGroup => mg.possibleFirstChildElementsInInfoset
-          }
-      }
-      res
-    }
-
-    val nextParentElts = nextParentElements
-    val res = arrayNext ++ nextSiblingElements ++ nextParentElts
-    res
-  }.value
-
-  def nextParentElements: Seq[ElementBase]
-
-  protected def couldBeLastElementInModelGroup: Boolean
-
-  /*
-   * Returns a list of sibling Terms that could follow this term. This will not
-   * return any children of sibling Terms, or any siblings of the parent.
-   */
-  final def possibleNextSiblingTerms: Seq[Term] = LV('possibleNextSiblingTerms) {
-    val listOfNextTerm = enclosingTerm match {
-      case None => Nil // root element, has no siblings
-      case Some(e: ElementBase) => Nil // complex element, cannot have another model group other than this one
-      case Some(c: Choice) => Nil // in choice, no other siblings could come after this one
-      case Some(s: Sequence) if !s.isOrdered => s.groupMembersNoRefs // unorderd sequence, all siblings (and myself) could be next
-      case Some(s: Sequence) => {
-        // in a sequence, the next term could be any later sibling that is not
-        // or does not have a required element, up to and including the first
-        // term that is/has a required element
-        //        def isOutputValueCalc(term: Term) =
-        //          term match { case eb: ElementBase if eb.isOutputValueCalc => true; case _ => false }
-        val allNextSiblings = s.groupMembersNoRefs.dropWhile(_ != thisTermNoRefs).tail
-        val nextSiblings = allNextSiblings // .dropWhile(isOutputValueCalc(_))
-        val (optional, firstRequiredAndLater) = nextSiblings.span {
-          case e: ElementBase => e.canBeAbsentFromUnparseInfoset
-          case mg: ModelGroup => !mg.mustHaveRequiredElement
-        }
-        optional ++ firstRequiredAndLater.take(1)
-      }
-    }
-    listOfNextTerm
-  }.value
-
-  /**
-   * Set of elements referenced from an expression in the scope of this term.
-   *
-   * Specific to certain function call contexts e.g., only elements referenced
-   * by dfdl:valueLength or dfdl:contentLength.
-   *
-   * Separated by parser/unparser since parsers have to derive from
-   * dfdl:inputValueCalc, and must include discriminators and assert test
-   * expressions. Unparsers must derive from dfdl:outputValueCalc and exclude
-   * discriminators and asserts. Both must include setVariable/newVariableInstance,
-   * and property expressions are nearly the same. There are some unparser-specfic
-   * properties that take runtime-valued expressions - dfdl:outputNewLine is
-   * one example.
-   */
-  final lazy val contentLengthParserReferencedElementInfos: Set[DPathElementCompileInfo] = {
-    val propRefs = propertyContentReferencedElementInfos
-    val stmtRefs = statementContentParserReferencedElementInfos
-    val calcRefs = calcContentParserReferencedElementInfos
-    val locRefs = propRefs ++ stmtRefs ++ calcRefs
-    val res = realChildren.foldLeft(locRefs) { (s, i) => s.union(i.contentLengthParserReferencedElementInfos) }
-    res
-  }
-
-  /**
-   * Any element referenced from an expression in the scope of this term
-   * is in this set.
-   */
-  final lazy val contentLengthUnparserReferencedElementInfos: Set[DPathElementCompileInfo] = {
-    val propRefs = propertyContentReferencedElementInfos
-    val stmtRefs = statementContentUnparserReferencedElementInfos
-    val calcRefs = calcContentUnparserReferencedElementInfos
-    val locRefs = propRefs ++ stmtRefs ++ calcRefs
-    val res = realChildren.foldLeft(locRefs) { (s, i) => s.union(i.contentLengthUnparserReferencedElementInfos) }
-    res
-  }
-
-  /**
-   * Any element referenced from an expression in the scope of this term
-   * is in this set.
-   */
-  final lazy val valueLengthParserReferencedElementInfos: Set[DPathElementCompileInfo] = {
-    val propRefs = propertyValueReferencedElementInfos
-    val stmtRefs = statementValueParserReferencedElementInfos
-    val calcRefs = calcValueParserReferencedElementInfos
-    val locRefs = propRefs ++ stmtRefs ++ calcRefs
-    val res = realChildren.foldLeft(locRefs) { (s, i) => s.union(i.valueLengthParserReferencedElementInfos) }
-    res
-  }
-
-  /**
-   * Any element referenced from an expression in the scope of this term
-   * is in this set.
-   */
-  final lazy val valueLengthUnparserReferencedElementInfos: Set[DPathElementCompileInfo] = {
-    val propRefs = propertyValueReferencedElementInfos
-    val stmtRefs = statementValueUnparserReferencedElementInfos
-    val calcRefs = calcValueUnparserReferencedElementInfos
-    val locRefs = propRefs ++ stmtRefs ++ calcRefs
-    val res = realChildren.foldLeft(locRefs) { (s, i) => s.union(i.valueLengthUnparserReferencedElementInfos) }
-    res
-  }
-
-  private lazy val realChildren: Seq[RealTermMixin] = {
-    this match {
-      case mg: ModelGroup => mg.groupMembersNoRefs.asInstanceOf[Seq[RealTermMixin]]
-      case eb: ElementBase if (eb.isComplexType) => Seq(eb.complexType.group)
-      case eb: ElementBase => Seq()
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RestrictionUnion.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RestrictionUnion.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RestrictionUnion.scala
index cdb69b9..85a6618 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RestrictionUnion.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RestrictionUnion.scala
@@ -49,7 +49,7 @@ import edu.illinois.ncsa.daffodil.dpath.NodeInfo.PrimType
  * A schema component for simple type restrictions
  */
 final class Restriction(xmlArg: Node, val simpleType: SimpleTypeDefBase)
-  extends SchemaComponent(xmlArg, simpleType.schemaDocument)
+  extends SchemaComponentImpl(xmlArg, simpleType.schemaDocument)
   with NestingLexicalMixin
   with Facets
   with TypeChecks {
@@ -187,7 +187,7 @@ final class Restriction(xmlArg: Node, val simpleType: SimpleTypeDefBase)
  * A schema component for simple type unions
  */
 final class Union(xmlArg: Node, simpleType: SimpleTypeDefBase)
-  extends SchemaComponent(xmlArg, simpleType.schemaDocument)
+  extends SchemaComponentImpl(xmlArg, simpleType.schemaDocument)
   with NestingLexicalMixin {
 
   Assert.invariant(xmlArg.asInstanceOf[scala.xml.Elem].label == "union")
@@ -213,7 +213,7 @@ final class Union(xmlArg: Node, simpleType: SimpleTypeDefBase)
   private lazy val immediateTypeXMLs = xml \ "simpleType"
   private lazy val immediateTypes = immediateTypeXMLs.map { node =>
     {
-      schemaSet.LocalSimpleTypeDefFactory(node, schemaDocument).forElement(simpleType.element)
+      new LocalSimpleTypeDef(node, simpleType.elementDecl)
     }
   }
 
@@ -226,7 +226,7 @@ final class Union(xmlArg: Node, simpleType: SimpleTypeDefBase)
   }
   private lazy val namedTypeQNames = namedTypeQNameStrings.map { qns => resolveQName(qns) }
   private lazy val namedTypes = namedTypeQNames.map {
-    qn => schemaSet.getGlobalSimpleTypeDef(qn).get.forElement(simpleType.element)
+    qn => schemaSet.getGlobalSimpleTypeDef(qn).get.forElement(simpleType.elementDecl)
   }
   private lazy val directMemberTypes: Seq[SimpleTypeDefBase] = namedTypes ++ immediateTypes
 

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Root.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Root.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Root.scala
new file mode 100644
index 0000000..a385cb0
--- /dev/null
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/Root.scala
@@ -0,0 +1,31 @@
+package edu.illinois.ncsa.daffodil.dsom
+
+import edu.illinois.ncsa.daffodil.grammar.RootGrammarMixin
+import edu.illinois.ncsa.daffodil.xml.NamedQName
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import scala.xml.Node
+import scala.xml.UnprefixedAttribute
+
+/**
+ * Root is a special kind of ElementRef that has no enclosing group.
+ *
+ * This is the entity that is compiled by the schema compiler.
+ */
+final class Root(defXML: Node, parentArg: SchemaDocument,
+  namedQNameArg: NamedQName,
+  globalElementDecl: => GlobalElementDecl)
+  extends AbstractElementRef(null, parentArg, 1)
+  with RootGrammarMixin {
+
+  final override lazy val xml = {
+    val elem = XMLUtils.getXSDElement(defXML.scope)
+    val res = elem % new UnprefixedAttribute("ref", refQName.toQNameString, scala.xml.Null)
+    res
+  }
+
+  override lazy val refQName = namedQNameArg.toRefQName
+
+  override lazy val referencedElement = globalElementDecl
+
+  lazy val rootParseUnparsePolicy = defaultParseUnparsePolicy
+}

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RuntimePropertyMixins.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RuntimePropertyMixins.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RuntimePropertyMixins.scala
index 4b1a32b..a5104bd 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RuntimePropertyMixins.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/RuntimePropertyMixins.scala
@@ -184,7 +184,7 @@ trait DelimitedRuntimeValuedPropertiesMixin
   with RawDelimitedRuntimeValuedPropertiesMixin { decl: Term =>
 
   lazy val isLengthKindDelimited = {
-    decl.referredToComponent match {
+    this match {
       case mg: ModelGroup => mg.enclosingElement.get.lengthKind == LengthKind.Delimited
       case eb: ElementBase => eb.lengthKind == LengthKind.Delimited
     }
@@ -214,7 +214,8 @@ trait DelimitedRuntimeValuedPropertiesMixin
     val qn = this.qNameForProperty("terminator")
     val typeIfStaticallyKnown = NodeInfo.String
     val typeIfRuntimeKnown = NodeInfo.NonEmptyString
-    ExpressionCompilers.String.compile(qn, typeIfStaticallyKnown, typeIfRuntimeKnown, terminatorRaw)
+    val raw = terminatorRaw
+    ExpressionCompilers.String.compile(qn, typeIfStaticallyKnown, typeIfRuntimeKnown, raw)
   }.value
 
   final def terminatorLoc = (this.diagnosticDebugName, this.path)
@@ -525,7 +526,7 @@ trait ElementRuntimeValuedPropertiesMixin
 trait SequenceRuntimeValuedPropertiesMixin
   extends DelimitedRuntimeValuedPropertiesMixin
   with Sequence_AnnotationMixin
-  with RawSequenceRuntimeValuedPropertiesMixin { decl: GroupBase =>
+  with RawSequenceRuntimeValuedPropertiesMixin { decl: SequenceTermBase =>
 
   private lazy val separatorExpr = {
     val qn = this.qNameForProperty("separator")

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponent.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponent.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponent.scala
index cad3119..663b604 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponent.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponent.scala
@@ -33,7 +33,6 @@
 package edu.illinois.ncsa.daffodil.dsom
 
 import scala.xml.Node
-import edu.illinois.ncsa.daffodil.ExecutionMode
 import edu.illinois.ncsa.daffodil.exceptions.Assert
 import edu.illinois.ncsa.daffodil.xml.GetAttributesMixin
 import edu.illinois.ncsa.daffodil.xml.NS
@@ -45,36 +44,36 @@ import edu.illinois.ncsa.daffodil.processors.VariableMap
 import edu.illinois.ncsa.daffodil.processors.NonTermRuntimeData
 import edu.illinois.ncsa.daffodil.xml.ResolvesQNames
 import edu.illinois.ncsa.daffodil.schema.annotation.props.LookupLocation
-import edu.illinois.ncsa.daffodil.schema.annotation.props.PropertyLookupResult
-import edu.illinois.ncsa.daffodil.schema.annotation.props.FindPropertyMixin
 import edu.illinois.ncsa.daffodil.schema.annotation.props.PropTypes
-import edu.illinois.ncsa.daffodil.schema.annotation.props.NotFound
-import edu.illinois.ncsa.daffodil.oolag.OOLAG.OOLAGHost
+import edu.illinois.ncsa.daffodil.oolag.OOLAG._
 import edu.illinois.ncsa.daffodil.api.DaffodilTunables
-import edu.illinois.ncsa.daffodil.schema.annotation.props.PropertyLookupResult
-import edu.illinois.ncsa.daffodil.schema.annotation.props.FindPropertyMixin
 import edu.illinois.ncsa.daffodil.xml.GetAttributesMixin
 import edu.illinois.ncsa.daffodil.schema.annotation.props.PropTypes
 
+abstract class SchemaComponentImpl( final override val xml: Node,
+  final override val parent: SchemaComponent)
+  extends SchemaComponent
+
 /**
  * The core root class of the DFDL Schema object model.
  *
  * Every schema component has a schema document, and a schema, and a namespace.
  */
-abstract class SchemaComponent(xmlArg: Node, val parent: SchemaComponent)
-  extends OOLAGHost(parent)
+trait SchemaComponent
+  extends OOLAGHost
   with ImplementsThrowsOrSavesSDE
   with GetAttributesMixin
   with SchemaComponentIncludesAndImportsMixin
   with ResolvesQNames
-  with FindPropertyMixin
   with SchemaFileLocatableImpl
   with PropTypes {
-  
-  def tunable: DaffodilTunables = parent.tunable
 
-  val xml = xmlArg
-  val aaa_xml = xml // for debugging, so we don't have to scroll down.
+  def xml: Node
+  def parent: SchemaComponent
+
+  final override def oolagContextViaArgs = Some(parent)
+
+  def tunable: DaffodilTunables = parent.tunable
 
   lazy val dpathCompileInfo: DPathCompileInfo =
     new DPathCompileInfo(
@@ -116,28 +115,21 @@ abstract class SchemaComponent(xmlArg: Node, val parent: SchemaComponent)
     schemaSet.variableMap
   }.value
 
-  /*
-   * Anything non-annotated always returns property not found
+  /**
+   * Whether the component is hidden.
    *
-   * Override in annotated components
+   * Override this in the components that can hide - SequenceGroupRef
+   * and ChoiceGroupRef
    */
-  def findPropertyOption(pname: String): PropertyLookupResult = {
-    ExecutionMode.requireCompilerMode
-    NotFound(Nil, Nil, pname)
-  }
-  // Q: not sure why non-annotated schema components need to have findProperty
-  // on them at all. Who would call it polymorphically, not knowing whether they
-  // have an annotated schema component or not?
-  // A: DFDLAnnotation - the annotation objects themselves - aren't annotated
-  // schema components - they have a relationship to an annotated schema component
-  // but clearly they carry properties.
-
-  def isHidden: Boolean = LV('isHidden) {
-    enclosingComponent match {
-      case None => Assert.invariantFailed("Root global element should be overriding this.")
+  def isHidden: Boolean = isHiddenLV
+
+  private lazy val isHiddenLV = {
+    val optEC = enclosingComponent
+    optEC match {
+      case None => false
       case Some(ec) => ec.isHidden
     }
-  }.value
+  }
 
   lazy val schemaComponent: LookupLocation = this
 
@@ -149,19 +141,23 @@ abstract class SchemaComponent(xmlArg: Node, val parent: SchemaComponent)
     val ee = et match {
       case None => None
       case Some(eb: ElementBase) => Some(eb)
-      case Some(sc: SchemaComponent) => sc.enclosingElement
+      case Some(sc: SchemaComponent) => {
+        val scee = sc.enclosingElement
+        scee
+      }
     }
     ee
   }.value
 
-  final lazy val rootElement: Option[GlobalElementDecl] = {
+  final lazy val rootElementRef = rootElementDecl.map { _.elementRef }
+
+  final lazy val rootElementDecl: Option[GlobalElementDecl] = {
     enclosingElement match {
-      case Some(e) => e.rootElement
-      case None => {
-        if (this.isInstanceOf[GlobalElementDecl])
-          Some(this.asInstanceOf[GlobalElementDecl])
-        else
-          None
+      case Some(e) => e.rootElementDecl
+      case None => this match {
+        case ged: GlobalElementDecl => Some(ged)
+        case root: Root => root.optReferredToComponent
+        case _ => Assert.invariantFailed("No global element decl")
       }
     }
   }
@@ -254,7 +250,7 @@ abstract class SchemaComponent(xmlArg: Node, val parent: SchemaComponent)
  * the 'schema'.
  */
 final class Schema(val namespace: NS, schemaDocs: Seq[SchemaDocument], schemaSetArg: SchemaSet)
-  extends SchemaComponent(<fake/>, schemaSetArg) {
+  extends SchemaComponentImpl(<fake/>, schemaSetArg) {
 
   requiredEvaluations(schemaDocuments)
 
@@ -278,7 +274,6 @@ final class Schema(val namespace: NS, schemaDocs: Seq[SchemaDocument], schemaSet
             thing match {
               case df: DFDLDefiningAnnotation => df.asAnnotation.locationDescription
               case sc: SchemaComponent => sc.locationDescription
-              case sf: SchemaComponentFactory => sf.locationDescription
               case _ => Assert.impossibleCase(thing)
             }
           }.mkString("\n"))

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponentFactory.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponentFactory.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponentFactory.scala
index 5d36925..343a514 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponentFactory.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaComponentFactory.scala
@@ -1,7 +1,7 @@
 /* Copyright (c) 2017 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
- *               http://www.tresys.com
+ *               http://www.troverride val esys.com
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  * this software and associated documentation files (the "Software"), to deal with
@@ -33,29 +33,33 @@
 package edu.illinois.ncsa.daffodil.dsom
 
 import edu.illinois.ncsa.daffodil.exceptions.SchemaFileLocatable
-import edu.illinois.ncsa.daffodil.xml.GetAttributesMixin
 import edu.illinois.ncsa.daffodil.xml.XMLUtils
 import edu.illinois.ncsa.daffodil.exceptions.ThrowsSDE
 import scala.xml.NamespaceBinding
 import edu.illinois.ncsa.daffodil.xml.NS
 import edu.illinois.ncsa.daffodil.oolag.OOLAG.OOLAGHost
+import scala.xml.Node
 
 /**
  * Anything that can be computed without reference to the point of use
  * or point of reference can be computed here on these factory objects.
  */
-class SchemaComponentFactory(override val xml: scala.xml.Node,
-  override val schemaDocument: SchemaDocument)
-  extends OOLAGHost(schemaDocument)
-  with SchemaFileLocatableImpl
-  with CommonContextMixin
-  with GetAttributesMixin
-  with ImplementsThrowsSDE
+abstract class SchemaComponentFactory( final override val xml: Node,
+  final override val schemaDocument: SchemaDocument)
+  extends SchemaComponent
   with NestingLexicalMixin {
 
-  override def parent = schemaDocument
-  
-  final override def tunable = schemaDocument.tunable
+  final override def parent = schemaDocument
+
+}
+
+abstract class AnnotatedSchemaComponentFactory( final override val xml: Node,
+  final override val schemaDocument: SchemaDocument)
+  extends AnnotatedSchemaComponent
+  with NestingLexicalMixin {
+
+  final override def parent = schemaDocument
+
 }
 
 trait SchemaFileLocatableImpl

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaDocument.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaDocument.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaDocument.scala
index b14e4c8..317a7cf 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaDocument.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaDocument.scala
@@ -89,7 +89,7 @@ final class XMLSchemaDocument(xmlArg: Node,
    * even though it does not have a namespace
    */
   override val isBootStrapSD: Boolean)
-  extends SchemaComponent(xmlArg, sfArg.getOrElse(schemaSetArg))
+  extends SchemaComponentImpl(xmlArg, sfArg.getOrElse(schemaSetArg))
   with SchemaDocumentMixin
   with SchemaDocIncludesAndImportsMixin {
 
@@ -147,12 +147,18 @@ final class XMLSchemaDocument(xmlArg: Node,
  * I.e., default format properties, named format properties, etc.
  */
 final class SchemaDocument(xmlSDoc: XMLSchemaDocument)
-  extends AnnotatedSchemaComponent(xmlSDoc.xml, xmlSDoc)
+  extends AnnotatedSchemaComponent
   with SchemaDocumentMixin
+  with ResolvesProperties
   with Format_AnnotationMixin
   with SeparatorSuppressionPolicyMixin {
 
-  override def term = Assert.usageError("not to be called on SchemaDocument")
+  final override val xml = xmlSDoc.xml
+  final override def parent = xmlSDoc
+
+  override lazy val optReferredToComponent = None
+
+  // override def term = Assert.usageError("not to be called on SchemaDocument")
 
   /**
    * Implements the selectivity so that if you specify a root element
@@ -196,12 +202,12 @@ final class SchemaDocument(xmlSDoc: XMLSchemaDocument)
   //    val dfdlAttrs = attrs.filter{ a => a.isPrefixed && a.}
   //  }
 
-  def nonDefaultPropertySources = Seq()
-
-  def defaultPropertySources = LV('defaultPropertySources) {
-    val seq = Seq(this.defaultFormatChain)
-    seq
-  }.value
+  //  def nonDefaultPropertySources = Seq()
+  //
+  //  def defaultPropertySources = LV('defaultPropertySources) {
+  //    val seq = Seq(this.defaultFormatChain)
+  //    seq
+  //  }.value
 
   protected def annotationFactory(node: Node): Option[DFDLAnnotation] = {
     val res = node match {
@@ -251,7 +257,7 @@ final class SchemaDocument(xmlSDoc: XMLSchemaDocument)
    */
   lazy val globalElementDecls = {
     val xmlelts = (xml \ "element")
-    val factories = xmlelts.map { new edu.illinois.ncsa.daffodil.dsom.GlobalElementDeclFactory(_, this) }
+    val factories = xmlelts.map { new GlobalElementDeclFactory(_, this) }
     factories
   }
   lazy val globalSimpleTypeDefs = (xml \ "simpleType").map { new GlobalSimpleTypeDefFactory(_, this) }

http://git-wip-us.apache.org/repos/asf/incubator-daffodil/blob/89517717/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaSet.scala
----------------------------------------------------------------------
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaSet.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaSet.scala
index aa7d1cd..23583b4 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaSet.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/SchemaSet.scala
@@ -51,7 +51,6 @@ import edu.illinois.ncsa.daffodil.api.DaffodilSchemaSource
 import edu.illinois.ncsa.daffodil.api.UnitTestSchemaSource
 import edu.illinois.ncsa.daffodil.util.Misc
 import edu.illinois.ncsa.daffodil.schema.annotation.props.LookupLocation
-import edu.illinois.ncsa.daffodil.util.Memoize1
 import edu.illinois.ncsa.daffodil.api.DaffodilTunables
 
 /**
@@ -83,7 +82,7 @@ final class SchemaSet(
   checkAllTopLevelArg: Boolean,
   parent: SchemaComponent,
   override val tunable: DaffodilTunables)
-  extends SchemaComponent(<schemaSet/>, parent) // a fake schema component
+  extends SchemaComponentImpl(<schemaSet/>, parent) // a fake schema component
   with SchemaSetIncludesAndImportsMixin {
 
   requiredEvaluations(isValid)
@@ -333,7 +332,7 @@ final class SchemaSet(
    * grabs the first element declaration of the first schema file
    * to use as the root.
    */
-  def rootElement(rootSpecFromProcessorFactory: Option[RootSpec]): GlobalElementDecl = {
+  def rootElement(rootSpecFromProcessorFactory: Option[RootSpec]): Root = {
     val rootSpecFromCompiler = rootSpec
     val re =
       (rootSpecFromCompiler, rootSpecFromProcessorFactory) match {
@@ -352,8 +351,8 @@ final class SchemaSet(
           val gdeclf = firstSchemaDocument.globalElementDecls
           val firstElement = {
             schemaDefinitionUnless(gdeclf.length >= 1, "No global elements in: " + firstSchemaDocument.uriString)
-            val rootElement = gdeclf(0).forRoot()
-            rootElement
+            val root = gdeclf(0).forRoot()
+            root
           }
           firstElement
         }
@@ -531,26 +530,6 @@ final class SchemaSet(
 
     finalVMap
   }.value
-
-  object LocalSimpleTypeDefFactory {
-
-    private val f =
-      Memoize1((n: Node) =>
-        Memoize1((sd: SchemaDocument) =>
-          new LocalSimpleTypeDefFactory(n, sd)))
-
-    def apply(xml: Node, sd: SchemaDocument) = f(xml)(sd)
-  }
-
-  object LocalElementDeclFactory {
-
-    private val f =
-      Memoize1((xml: Node) =>
-        Memoize1((sd: SchemaDocument) =>
-          new LocalElementDeclFactory(xml, sd)))
-
-    def apply(xml: Node, sd: SchemaDocument) = f(xml)(sd)
-  }
 }
 
 class ValidateSchemasErrorHandler(sset: SchemaSet) extends org.xml.sax.ErrorHandler {