stevedlawrence commented on a change in pull request #207: Added support for enumerations and TypeValueCalc URL: https://github.com/apache/incubator-daffodil/pull/207#discussion_r279773322
########## File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/TypeCalculator.scala ########## @@ -0,0 +1,453 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.daffodil.processors + +import scala.math.BigInt +import scala.collection.immutable.HashSet +import org.apache.daffodil.dsom.CompiledExpression +import org.apache.daffodil.util.Maybe +import org.apache.daffodil.oolag.OOLAG.OOLAGHost +import org.apache.daffodil.dsom.CompiledExpression +import org.apache.daffodil.dsom.CompiledExpression +import scala.collection.immutable.HashMap +import org.apache.daffodil.processors.parsers.PState +import org.apache.daffodil.processors.parsers.ParseError +import org.apache.daffodil.processors.parsers.GeneralParseFailure +import org.apache.daffodil.exceptions.Assert +import java.lang.{ Long => JLong } +import org.apache.daffodil.processors.unparsers.UState +import org.apache.daffodil.util.Maybe.One +import org.apache.daffodil.dsom.CompiledExpression +import org.apache.daffodil.dpath.DState +import org.apache.daffodil.processors.parsers.ParseError +import org.apache.daffodil.util.Maybe.One +import org.apache.daffodil.dpath.NodeInfo.Kind +import org.apache.daffodil.dpath.NodeInfo + +abstract class TypeCalculator[A <: AnyRef, B <: AnyRef](val srcType: NodeInfo.Kind, val dstType: NodeInfo.Kind) + extends Serializable { + type Error = String + + /* + * We can be used from both a parser directly, and as part of a DPath expression. + * There are 2 main differences that require handling these cases seperatly + * + * 1) A (un)parser expects errors to be reported via state.setFailed, while DPath expects subexpressions to report + * error by throwing + * 2) DPathCompiledExpressions provides a top level interface to initiate evaluation which accepts a ParseOrUnparseState, + * and a second interface to evaluate subexpressions which accepts a DState + */ + + def inputTypeCalc(x: A, xType: NodeInfo.Kind): (Maybe[B], Maybe[Error]) + def outputTypeCalc(x: B, xType: NodeInfo.Kind): (Maybe[A], Maybe[Error]) + + def inputTypeCalcParse(pstate: PState, context: RuntimeData, x: A, xType: NodeInfo.Kind): Maybe[B] = { + val (ans, err) = inputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), Maybe(pstate.currentLocation), err.get) + pstate.setFailed(diag) + } + + //In event of an error, we still want to return Maybe.Nope, which happens + //to be what ans would have + ans + } + def outputTypeCalcUnparse(ustate: UState, context: RuntimeData, x: B, xType: NodeInfo.Kind): Maybe[A] = { + val (ans, err) = outputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), Maybe(ustate.currentLocation), err.get) + ustate.setFailed(diag) + } + + //In event of an error, we still want to return Maybe.Nope, which happens + //to be what ans would have + ans + } + + /* + * It appears that dpath expressions actually throw errors. + * See the definition of DAFError + */ + def inputTypeCalcRun(dstate: DState, x: A, xType: NodeInfo.Kind): Unit = { + val context = dstate.runtimeData.get + val (ans, err) = inputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), dstate.contextLocation, err.get) + throw diag + // throw FNErrorFunctionException(Maybe(context.schemaFileLocation), dstate.contextLocation, err.get) + } + + dstate.setCurrentValue(ans) + + } + def outputTypeCalcRun(dstate: DState, x: B, xType: NodeInfo.Kind): Unit = { + val context = dstate.runtimeData.get + val (ans, err) = outputTypeCalc(x, xType) + Assert.invariant(ans.isDefined ^ err.isDefined) + + if (err.isDefined) { + val diag = new ParseError(Maybe(context.schemaFileLocation), dstate.contextLocation, err.get) + throw diag + } + + dstate.setCurrentValue(ans) + + } + + def supportsParse: Boolean = true + def supportsUnparse: Boolean = true +} + +class KeysetValueTypeCalculatorOrdered[A <: AnyRef, B <: AnyRef](valueMap: HashMap[A, B], rangeTable: Seq[(RangeBound[A], RangeBound[A], B)], unparseMap: HashMap[B, A], + srcType: NodeInfo.Kind, dstType: NodeInfo.Kind) + extends TypeCalculator[A, B](srcType, dstType) { + + override def inputTypeCalc(x: A, xType: NodeInfo.Kind): (Maybe[B], Maybe[Error]) = { + if (valueMap.contains(x)) { + (One(valueMap.get(x).get), Maybe.Nope) + } else { + val ans1: Option[(RangeBound[A], RangeBound[A], B)] = rangeTable.find({ + case (min, max, _) => { + min.testAsLower(x) && max.testAsUpper(x) + } + }) + ans1 match { + case None => { + (Maybe.Nope, One(s"Key ${x} not found in keyset-value mapping")) + } + case Some((_, _, v)) => (Maybe(v), Maybe.Nope) + } + } + } + + override def outputTypeCalc(x: B, xType: NodeInfo.Kind): (Maybe[A], Maybe[Error]) = { + unparseMap.get(x) match { + case Some(v) => (Maybe(v), Maybe.Nope) + case None => (Maybe.Nope, One(s"Value ${x} not found in keyset-value mapping")) + } + } + +} + +class KeysetValueTypeCalculatorUnordered[A <: AnyRef, B <: AnyRef](valueMap: HashMap[A, B], unparseMap: HashMap[B, A], srcType: NodeInfo.Kind, dstType: NodeInfo.Kind) + extends TypeCalculator[A, B](srcType, dstType) { + + override def inputTypeCalc(x: A, xType: NodeInfo.Kind): (Maybe[B], Maybe[Error]) = { + if (valueMap.contains(x)) { + valueMap.get(x) match { + case Some(a) => (Maybe(a), Maybe.Nope) + case None => (Maybe.Nope, One(s"Value ${x} not found in keyset-value mapping")) + } + } else { + (Maybe.Nope, One(s"Key ${x} not found in keyset-value mapping")) + + } + } + override def outputTypeCalc(x: B, xType: NodeInfo.Kind): (Maybe[A], Maybe[Error]) = { + unparseMap.get(x) match { + case Some(v) => (Maybe(v), Maybe.Nope) + case None => (Maybe.Nope, One(s"Value ${x} not found in keyset-value mapping")) + } + } + +} + +class ExpressionTypeCalculator[A <: AnyRef, B <: AnyRef](maybeInputTypeCalc: Maybe[CompiledExpression[B]], maybeOutputTypeCalc: Maybe[CompiledExpression[A]], + srcType: NodeInfo.Kind, dstType: NodeInfo.Kind) + extends TypeCalculator[A, B](srcType, dstType) { + override def supportsParse = maybeInputTypeCalc.isDefined + override def supportsUnparse = maybeOutputTypeCalc.isDefined + + //The class TypeValueCalc will verify that supports(Un)Parse is true when nessasary + //Therefore, if we ever call the below functions, we know that the relevent Maybe object is defined. + + //TODO, pass x into DPath state Review comment: Can this TODO be removed? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
