You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2021/04/06 13:42:37 UTC
[GitHub] [daffodil] stevedlawrence commented on a change in pull request #510: New variable instances should inherit external values
stevedlawrence commented on a change in pull request #510:
URL: https://github.com/apache/daffodil/pull/510#discussion_r607841906
##########
File path: daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/ExpressionEvaluatingUnparsers.scala
##########
@@ -103,13 +102,22 @@ class NewVariableInstanceStartUnparser(override val context: VariableRuntimeData
override lazy val childProcessors = Vector()
override def unparse(state: UState) = {
- state.variableMap.newVariableInstance(context)
+ val v = state.variableMap.newVariableInstance(context)
+
if (context.maybeDefaultValueExpr.isDefined) {
- val maybeVar = state.variableMap.find(context.globalQName)
- Assert.invariant(maybeVar.isDefined)
- maybeVar.get.setState(VariableInProcess)
- val suspendableExpression = new NewVariableInstanceSuspendableExpression(context.maybeDefaultValueExpr.get, context)
- suspendableExpression.run(state)
+ val dve = context.maybeDefaultValueExpr.get
+ dve match {
+ case constExpr: ConstantExpression[_] => // Do nothing, value has already been computed at schema compile time
+ case _ => {
+ v.setState(VariableInProcess)
+ val suspendableExpression = new NewVariableInstanceDefaultValueSuspendableExpression(dve, context)
Review comment:
Can we pass in the variable instance instance the NewVariableInstanceDefaultValue.... so that we don't have to do a variableMap.find in processExpressionResult?
##########
File path: daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/ExpressionEvaluatingUnparsers.scala
##########
@@ -103,13 +102,22 @@ class NewVariableInstanceStartUnparser(override val context: VariableRuntimeData
override lazy val childProcessors = Vector()
override def unparse(state: UState) = {
- state.variableMap.newVariableInstance(context)
+ val v = state.variableMap.newVariableInstance(context)
+
if (context.maybeDefaultValueExpr.isDefined) {
- val maybeVar = state.variableMap.find(context.globalQName)
- Assert.invariant(maybeVar.isDefined)
- maybeVar.get.setState(VariableInProcess)
- val suspendableExpression = new NewVariableInstanceSuspendableExpression(context.maybeDefaultValueExpr.get, context)
- suspendableExpression.run(state)
+ val dve = context.maybeDefaultValueExpr.get
+ dve match {
+ case constExpr: ConstantExpression[_] => // Do nothing, value has already been computed at schema compile time
+ case _ => {
+ v.setState(VariableInProcess)
+ val suspendableExpression = new NewVariableInstanceDefaultValueSuspendableExpression(dve, context)
+ suspendableExpression.run(state)
+ }
+ }
+ } else {
+ // The NVI will inherit the default value of the original variable instance
+ // This will also inherit any externally provided bindings.
+ v.setDefaultValue(v.firstInstanceInitialValue)
Review comment:
Just had a though, what if a variable did not have a default value and was not set externaly? Then firstInstanceInitialValue is NoValue, and so we are going to set the default value to not value. Which changes the state to Defined. So we could get in a state where we are Defined but with NoValue. Do we want to make it so firstInstanceInitialValue is an Option, and this becomes something like:
```scala
} else if (v.firstInstanceInitialValue.isDefined) {
v.setDefaultValue(v.firstInstanceInitialValue.get)
}
```
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/VariableMap1.scala
##########
@@ -148,22 +168,26 @@ class VariableInstance private (
this._value = Some(v)
}
- def setDefaultValue(v: DataValuePrimitiveNullable) = this._value = Some(v)
+ /* This is used to set a default value without assigning a priorValue */
+ def setDefaultValue(v: DataValuePrimitiveNullable) = {
+ Assert.invariant(this.state == VariableUndefined || this.state == VariableInProcess)
Review comment:
Should we assert the ``v != NoValue`` too?
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/VariableMap1.scala
##########
@@ -324,17 +348,31 @@ class VariableMap private(vTable: Map[GlobalQName, ArrayBuffer[VariableInstance]
// have default value expressions or are defined externally.
def forceExpressionEvaluations(state: ParseOrUnparseState): Unit = {
vTable.foreach { case (_, variableInstances) => { variableInstances.foreach { inst => {
- (inst.state, inst.value, inst.defaultValueExpr.isDefined) match {
+ (inst.state, inst.defaultValueExpr.isDefined) match {
// Evaluate defineVariable statements with non-constant default value expressions
- case (VariableDefined, DataValue.NoValue, true) => {
+ case (VariableUndefined, true) => {
val res = inst.defaultValueExpr.get.evaluate(state)
- inst.setValue(DataValue.unsafeFromAnyRef(res))
+ inst.setDefaultValue(DataValue.unsafeFromAnyRef(res))
}
- case (_, _, _) => // Do nothing
+ case (_, _) => // Do nothing
}
}}}}
}
+
+ /**
+ * This function is called immediately after forceExpressionEvaluations in
+ * order to set the firstInstanceInitialValue for each variable instance.
+ * These initial values will be inherited by future new variable instances if
+ * the newVariableInstance statement does not provide a default value
+ * expression
+ */
+ def setFirstInstanceInitialValues(): Unit = {
+ vTable.foreach { case (_, variableInstances) => {
+ variableInstances.foreach { inst => inst.firstInstanceInitialValue = inst.value }
Review comment:
variableInstances should always have a length of 1 here, right? Since this should only ever be called before NVI's are evaluated? Thoughts on making this
```scala
Assert.invariant(variableInstances.length == 1)
variablesInstances(0).firstInstanceInitialValue = inst._value
```
Note that if firstInstanceInitialValue is turned into an Option mentioend above, then this is just a straigt copy of the private _value. So it keeps the Some/None aspect of the value as well if a instance was never set by a default.
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/RuntimeData.scala
##########
@@ -978,17 +978,12 @@ final class VariableRuntimeData(
@throws(classOf[java.io.IOException])
final private def writeObject(out: java.io.ObjectOutputStream): Unit = serializeObject(out)
- private lazy val state =
- if (!maybeDefaultValueExpr.isDefined) VariableUndefined
- else VariableDefined
-
- private lazy val value: DataValuePrimitiveNullable =
- if (maybeDefaultValueExpr.isEmpty) DataValue.NoValue
- else {
- val defaultValueExpr = maybeDefaultValueExpr.get
- defaultValueExpr match {
- case constExpr: ConstantExpression[_] => DataValue.unsafeFromAnyRef(constExpr.constant)
- case _ => DataValue.NoValue
+ private lazy val (state, value) = {
+ if (!maybeDefaultValueExpr.isDefined)
+ (VariableUndefined, DataValue.NoValue)
+ else maybeDefaultValueExpr.get match {
+ case constExpr: ConstantExpression[_] => (VariableDefined, DataValue.unsafeFromAnyRef(constExpr.constant))
+ case _ => (VariableUndefined, DataValue.NoValue)
Review comment:
Ah, so this is how you handle constants? I'm curious what others say, but I'd rather not have this special case here. It's maybe a little more performant when it is a constant, but it adds extra special cases and spreads out the NVI default logic in separate places. And I suspect NVI default values will rarely be constants, so we probably won't even hit this case very often.
I'd rather we just delete this state/value thing entirely, change the createVariableInstance function always creates a new instances with VariableUndefinedState and NoValue, and then the parsers/unparsers can mutate the state in a single place.
##########
File path: daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/ExpressionEvaluatingUnparsers.scala
##########
@@ -103,13 +102,22 @@ class NewVariableInstanceStartUnparser(override val context: VariableRuntimeData
override lazy val childProcessors = Vector()
override def unparse(state: UState) = {
- state.variableMap.newVariableInstance(context)
+ val v = state.variableMap.newVariableInstance(context)
+
if (context.maybeDefaultValueExpr.isDefined) {
- val maybeVar = state.variableMap.find(context.globalQName)
- Assert.invariant(maybeVar.isDefined)
- maybeVar.get.setState(VariableInProcess)
- val suspendableExpression = new NewVariableInstanceSuspendableExpression(context.maybeDefaultValueExpr.get, context)
- suspendableExpression.run(state)
+ val dve = context.maybeDefaultValueExpr.get
+ dve match {
+ case constExpr: ConstantExpression[_] => // Do nothing, value has already been computed at schema compile time
Review comment:
Not sure I understand this. If the defaultValue for the NVI is constant, we still need to get that constant value and set it as the defalutValue, right? Seems like this should be something like
```scala
v.setDefalutValue(constExpr.evaluate(state))
```
Though, this is essentially what the NewVariableInstanceSuspendableExpression will do, so I'm not sure if we need a special case for constant. I guess it does avoid allocating this suspendable thing, but that's probably not too big of a deal? And since the expression is constant, it shouldn't even suspend, so we will avoid that overhead regardless.
Can we remove this special case and just always do the below?
--
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:
users@infra.apache.org