/// <summary>
        /// Set the parameters for the expression specified by exp that are associated with a curve equation.
        /// </summary>
        protected void SetExpressionCurveParameters(EvaluationCurveInput curveEvalInput, Expression exp)
        {
            if (exp != null)
            {
                exp.Parameters["d1"]      = curveEvalInput.RelData1;
                exp.Parameters["chan"]    = curveEvalInput.RelData1;
                exp.Parameters["channel"] = curveEvalInput.RelData1;

                exp.Parameters["d2"]      = curveEvalInput.RelData2;
                exp.Parameters["notenum"] = curveEvalInput.RelData2;
                exp.Parameters["ccnum"]   = curveEvalInput.RelData2;

                exp.Parameters["d3"]       = curveEvalInput.RelData3;
                exp.Parameters["vel"]      = curveEvalInput.RelData3;
                exp.Parameters["velocity"] = curveEvalInput.RelData3;
                exp.Parameters["ccval"]    = curveEvalInput.RelData3;

                double primaryInput = curveEvalInput.getPrimaryInputVal();
                exp.Parameters["x"] = primaryInput;
                exp.Parameters[DEFAULT_INPUT_STRING] = primaryInput;
            }
            else
            {
                //This function should not be called when the expression is null.
                Debug.Assert(false);
            }
        }
        /// <summary>
        ///  Uses the primary input in evalInput to depermine which curve equation needs to be
        ///  evaluated. Then evaluates the appropriate curve equation using the given input.
        /// </summary>
        /// <param name="evalInput">Stores info neede to evaluate an expression</param>
        /// <returns>
        /// Returns the output of the evaluated equation. The return status will not be valid if
        /// the equation could not be evaluated.
        /// </returns>
        public ReturnStatus <double> Evaluate(EvaluationCurveInput evalInput)
        {
            var erroneousPoints          = new HashSet <int>();
            var controlPointValuesStatus = CalculateControlPointValues(evalInput.VariableParamInfoList, evalInput.TransformParamInfoList, evalInput.PointEquations, ref erroneousPoints);

            if (controlPointValuesStatus.IsValid)
            {
                EvaluationCurveJob evalCurveJob = new EvaluationCurveJob();

                string expressionStr      = GetCurveExpressionString(evalInput.getPrimaryInputVal(), controlPointValuesStatus.Value, evalInput.CurveEquations);
                var    returnedExpression = CreateExpressionFromString(expressionStr, EvalType.Curve);

                if (returnedExpression.IsValid)
                {
                    evalCurveJob.Configure(evalInput, controlPointValuesStatus.Value, returnedExpression.Value);

                    evalCurveJob.Execute();

                    if (evalCurveJob.OutputIsValid)
                    {
                        return(new ReturnStatus <double>(evalCurveJob.OutputVal, true));
                    }
                }
            }

            return(new ReturnStatus <double>(double.NaN, false));
        }
Example #3
0
        /// <summary>
        ///  Uses the primary input in evalInput to depermine which curve equation needs to be 
        ///  evaluated. Then evaluates the appropriate curve equation using the given input.
        /// </summary>
        /// <param name="evalInput">Stores info neede to evaluate an expression</param>
        /// <returns>
        /// Returns the output of the evaluated equation. The return status will not be valid if 
        /// the equation could not be evaluated.
        /// </returns>
        public ReturnStatus<double> Evaluate(EvaluationCurveInput evalInput)
        {
            var erroneousPoints = new HashSet<int>();
            var controlPointValuesStatus = CalculateControlPointValues(evalInput.VariableParamInfoList, evalInput.TransformParamInfoList, evalInput.PointEquations, ref erroneousPoints);

            if (controlPointValuesStatus.IsValid) {
                EvaluationCurveJob evalCurveJob = new EvaluationCurveJob();

                string expressionStr = GetCurveExpressionString(evalInput.getPrimaryInputVal(), controlPointValuesStatus.Value, evalInput.CurveEquations);
                var returnedExpression = CreateExpressionFromString(expressionStr, EvalType.Curve);

                if (returnedExpression.IsValid) {
                    evalCurveJob.Configure(evalInput, controlPointValuesStatus.Value, returnedExpression.Value);

                    evalCurveJob.Execute();

                    if (evalCurveJob.OutputIsValid)
                    {
                        return new ReturnStatus<double>(evalCurveJob.OutputVal, true);
                    }

                }

            }

            return new ReturnStatus<double>(double.NaN, false);
        }
        /// <summary>
        /// Initializes this class.
        /// </summary>
        public void Configure(EvaluationCurveInput evalInput, List<XyPoint<double>> controlPointValues, Expression expression)
        {
            this.OutputIsValid = false;

            this.evalInput = evalInput;

            this.controlPointValues = controlPointValues;
            this.expression = expression;

            this.expression.EvaluateFunction += FunctionHandler;
            SetExpressionBaseParameters((EvaluationInput)evalInput);
            SetExpressionCurveParameters(this.evalInput, this.expression);
        }
        /// <summary>
        /// Initializes this class.
        /// </summary>
        public void Configure(EvaluationCurveInput evalInput, List <XyPoint <double> > controlPointValues, Expression expression)
        {
            this.OutputIsValid = false;

            this.evalInput = evalInput;

            this.controlPointValues = controlPointValues;
            this.expression         = expression;

            this.expression.EvaluateFunction += FunctionHandler;
            SetExpressionBaseParameters((EvaluationInput)evalInput);
            SetExpressionCurveParameters(this.evalInput, this.expression);
        }
        /// <summary>
        /// Set the parameters for the expression specified by exp that are associated with a curve equation.
        /// </summary>
        protected void SetExpressionCurveParameters(EvaluationCurveInput curveEvalInput, Expression exp)
        {
            if (exp != null)
            {
                exp.Parameters["d1"] = curveEvalInput.RelData1;
                exp.Parameters["chan"] = curveEvalInput.RelData1;
                exp.Parameters["channel"] = curveEvalInput.RelData1;

                exp.Parameters["d2"] = curveEvalInput.RelData2;
                exp.Parameters["notenum"] = curveEvalInput.RelData2;
                exp.Parameters["ccnum"] = curveEvalInput.RelData2;

                exp.Parameters["d3"] = curveEvalInput.RelData3;
                exp.Parameters["vel"] = curveEvalInput.RelData3;
                exp.Parameters["velocity"] = curveEvalInput.RelData3;
                exp.Parameters["ccval"] = curveEvalInput.RelData3;

                double primaryInput = curveEvalInput.getPrimaryInputVal();
                exp.Parameters["x"] = primaryInput;
                exp.Parameters[DEFAULT_INPUT_STRING] = primaryInput;
            }
            else
            {
                //This function should not be called when the expression is null.
                Debug.Assert(false);
            }
        }
        /// <summary>
        ///     Applies to <paramref name="mssMsg"/> any mappings in the MappingManager that are associated with it.
        /// </summary>
        /// <param name="mssMsg">A MSS message to process.</param>
        /// <returns> A list resulting messages.</returns>
        public IEnumerable<MssMsg> ProcessMssMsg(MssMsg mssMsg)
        {
            //Apply the pre mapping query processing. If this causes the msg type to change then
            //apply it again for the new type.
            MssMsgType curMsgType;
            do
            {
                curMsgType = mssMsg.Type;
                IStaticMssMsgInfo staticMsgInfo = Factory_StaticMssMsgInfo.Create(mssMsg.Type);
                staticMsgInfo.ApplyPreMappingQueryProcessing(mssMsg);
            } while(curMsgType != mssMsg.Type);

            //Retrieves mappings from the MappingManager that will affect mssMsg
            IEnumerable<IMappingEntry> mappingEntries = this.mappingMgr.GetCopiesOfMappingEntriesForMsg(mssMsg);

            List<MssMsg> outMessages = new List<MssMsg>();
            if (mappingEntries.Any() == false)
            {
                outMessages.Add(mssMsg);
            }
            else
            {
                foreach (IMappingEntry entry in mappingEntries)
                {
                    MssMsg inMsg = (MssMsg)mssMsg.Clone();
                    IStaticMssMsgInfo inMsgInfo = Factory_StaticMssMsgInfo.Create(entry.InMssMsgRange.MsgType);
                    inMsgInfo.ApplyPreProcessing(inMsg);
                    MssMsg preProcessedMsg = (MssMsg)inMsg.Clone();

                    EvaluationCurveInput evalInput = new EvaluationCurveInput();
                    evalInput.Init(inMsg, this.mssParameterViewer.GetVariableParamInfoList(), entry);
                    ReturnStatus<double> evalReturnStatus = this.evaluator.Evaluate(evalInput);

                    if (evalReturnStatus.IsValid == false)
                    {
                        continue;
                    }

                    double mappedRelativeData3 = evalReturnStatus.Value;
                    IStaticMssMsgInfo outMsgInfo = Factory_StaticMssMsgInfo.Create(entry.OutMssMsgRange.MsgType);

                    double data3RangeSize = outMsgInfo.MaxData3Value - outMsgInfo.MinData3Value;
                    double mappedData3 = mappedRelativeData3 * data3RangeSize + outMsgInfo.MinData3Value;

                    //If data3 has been mapped outside of the range of values for its message type then this
                    //mapping will not output anything.
                    if (mappedData3 >= outMsgInfo.MinData3Value &&
                        mappedData3 <= outMsgInfo.MaxData3Value)
                    {
                        //Calculate what mssMsg.Data1 will be mapped to.
                        double mappedData1 = CalculateLinearMapping(entry.InMssMsgRange.Data1RangeBottom,
                                                                 entry.InMssMsgRange.Data1RangeTop,
                                                                 entry.OutMssMsgRange.Data1RangeBottom,
                                                                 entry.OutMssMsgRange.Data1RangeTop,
                                                                 mssMsg.Data1);
                        //Calculate what mssMsg.Data2 will be mapped to.
                        double mappedData2 = CalculateLinearMapping(entry.InMssMsgRange.Data2RangeBottom,
                                                                 entry.InMssMsgRange.Data2RangeTop,
                                                                 entry.OutMssMsgRange.Data2RangeBottom,
                                                                 entry.OutMssMsgRange.Data2RangeTop,
                                                                 mssMsg.Data2);

                        MssMsg outMsg = new MssMsg(entry.OutMssMsgRange.MsgType, mappedData1, mappedData2, mappedData3);

                        //Apply the post processing. If this causes the msg type to change then
                        //apply it again for the new type.
                        do
                        {
                            curMsgType = mssMsg.Type;
                            IStaticMssMsgInfo staticMsgInfo = Factory_StaticMssMsgInfo.Create(outMsg.Type);
                            staticMsgInfo.ApplyPostProcessing(preProcessedMsg, outMsg);
                        } while (curMsgType != mssMsg.Type);

                        yield return outMsg;
                    }
                }
            }
        }
        /// <summary>
        /// The snap function will transform a curve so that it starts and ends at the control points on either side of it.
        /// </summary>
        /// <param name="args">args should have 1 parameter that stores an equation to apply snap to.</param>
        protected bool HandleSnapFunc(FunctionArgs args, List <double> evaluatedArgs)
        {
            if (evaluatedArgs.Count != 1)
            {
                return(false);
            }

            //Get the index of the curve to apply snap to.
            int curveIndex = GetCurveIndex();

            //True if there is a control point before the curve.
            bool pointBeforeCurveExists = (curveIndex > 0);
            //True if there is a control point after the curve.
            bool pointAfterCurveExists = (curveIndex < this.controlPointValues.Count);

            Expression remainingExpression = args.Parameters[0];

            //Store the raw output of the argument.
            double snapOutputValue = evaluatedArgs[0];

            bool errorEncountered = false;
            ReturnStatus <double> evaluateStatus;

            //If there is a control point on either side of the equation then snap will need to
            //modify the output.
            if (pointBeforeCurveExists || pointAfterCurveExists)
            {
                XyPoint <double> pointBeforeCurve = null;
                XyPoint <double> rawCurveStart    = null;

                XyPoint <double> pointAfterCurve = null;
                XyPoint <double> rawCurveEnd     = null;

                EvaluationCurveInput endpointInput = (EvaluationCurveInput)this.evalInput.Clone();

                //If there is a control point before the equation then we need to evaluate the
                //equation at this point.
                if (pointBeforeCurveExists)
                {
                    pointBeforeCurve = controlPointValues[curveIndex - 1];
                    endpointInput.setPrimaryInputVal(pointBeforeCurve.X);
                    SetExpressionCurveParameters(endpointInput, remainingExpression);

                    evaluateStatus    = EvaluateExpression(remainingExpression);
                    errorEncountered |= !evaluateStatus.IsValid;
                    rawCurveStart     = new XyPoint <double>(pointBeforeCurve.X, evaluateStatus.Value);
                }

                //If there is a control point after the equation then we need to evaluate the
                //equation at this point.
                if (pointAfterCurveExists && errorEncountered == false)
                {
                    pointAfterCurve = controlPointValues[curveIndex];
                    endpointInput.setPrimaryInputVal(pointAfterCurve.X);
                    SetExpressionCurveParameters(endpointInput, remainingExpression);

                    evaluateStatus    = EvaluateExpression(remainingExpression);
                    errorEncountered |= !evaluateStatus.IsValid;

                    rawCurveEnd = new XyPoint <double>(pointAfterCurve.X, evaluateStatus.Value);
                }

                if (errorEncountered == false)
                {
                    if (pointBeforeCurveExists && pointAfterCurveExists)
                    {
                        snapOutputValue += GetEndPointSnapOffset(pointBeforeCurve.Y, rawCurveStart.Y,
                                                                 pointBeforeCurve.X, pointAfterCurve.X, this.evalInput.getPrimaryInputVal());

                        snapOutputValue += GetEndPointSnapOffset(pointAfterCurve.Y, rawCurveEnd.Y,
                                                                 pointAfterCurve.X, pointBeforeCurve.X, this.evalInput.getPrimaryInputVal());
                    }
                    else if (pointBeforeCurveExists)
                    {
                        double yOffset = pointBeforeCurve.Y - rawCurveStart.Y;
                        snapOutputValue += yOffset;
                    }
                    else //pointAfterCurveExists
                    {
                        Debug.Assert(pointAfterCurveExists);
                        double yOffset = pointAfterCurve.Y - rawCurveEnd.Y;
                        snapOutputValue += yOffset;
                    }
                }
            }

            if (errorEncountered)
            {
                return(false);
            }
            else
            {
                args.Result = snapOutputValue;
                return(true);
            }
        }
        protected ReturnStatus <XyPoint <double> > evaluateCurveAtXVal(double inputXVal, int curveIndex, IMappingEntry mappingEntry, List <MssParamInfo> variableParamInfoList, List <XyPoint <double> > controlPointList)
        {
            //For each sample point data1 data2 and data3 will be set to the X value of the
            //sample point.
            double relData1 = inputXVal;
            double relData2 = inputXVal;
            double relData3 = inputXVal;

            IStaticMssMsgInfo inMsgInfo =
                Factory_StaticMssMsgInfo.Create(mappingEntry.InMssMsgRange.MsgType);

            //If curXVal is outside of the relative input range for data 1 then set
            //relData1 to NaN
            double max    = (double)inMsgInfo.MaxData1Value;
            double min    = (double)inMsgInfo.MinData1Value;
            double bottom = (double)mappingEntry.InMssMsgRange.Data1RangeBottom;
            double top    = (double)mappingEntry.InMssMsgRange.Data1RangeTop;

            if (inputXVal < ((bottom - min) / (max - min + 1)) ||
                inputXVal > ((top - min) / (max - min + 1)))
            {
                relData1 = double.NaN;
            }

            //If curXVal is outside of the relative input range for data 2 then set relData2
            //to NaN
            max    = (double)inMsgInfo.MaxData2Value;
            min    = (double)inMsgInfo.MinData2Value;
            bottom = (double)mappingEntry.InMssMsgRange.Data2RangeBottom;
            top    = (double)mappingEntry.InMssMsgRange.Data2RangeTop;
            if (inputXVal < ((bottom - min) / (max - min + 1)) ||
                inputXVal > ((top - min) / (max - min + 1)))
            {
                relData2 = double.NaN;
            }

            var evalInput = new EvaluationCurveInput();

            evalInput.Init(
                relData1,
                relData2,
                relData3,
                variableParamInfoList,
                mappingEntry);

            var evalJob = new EvaluationCurveJob();

            var returnedExpression = CreateExpressionFromString(mappingEntry.CurveShapeInfo.CurveEquations[curveIndex], EvalType.Curve);

            if (returnedExpression.IsValid == false)
            {
                return(new ReturnStatus <XyPoint <double> >());
            }

            evalJob.Configure(evalInput, controlPointList, returnedExpression.Value);

            evalJob.Execute();

            if (evalJob.OutputIsValid)
            {
                var curPoint = new XyPoint <double>(inputXVal, evalJob.OutputVal);
                return(new ReturnStatus <XyPoint <double> >(curPoint));
            }
            else
            {
                return(new ReturnStatus <XyPoint <double> >());
            }
        }
Example #10
0
        protected ReturnStatus<XyPoint<double>> evaluateCurveAtXVal(double inputXVal, int curveIndex, IMappingEntry mappingEntry, List<MssParamInfo> variableParamInfoList, List<XyPoint<double>> controlPointList)
        {
            //For each sample point data1 data2 and data3 will be set to the X value of the
            //sample point.
            double relData1 = inputXVal;
            double relData2 = inputXVal;
            double relData3 = inputXVal;

            IStaticMssMsgInfo inMsgInfo =
                Factory_StaticMssMsgInfo.Create(mappingEntry.InMssMsgRange.MsgType);

            //If curXVal is outside of the relative input range for data 1 then set
            //relData1 to NaN
            double max = (double)inMsgInfo.MaxData1Value;
            double min = (double)inMsgInfo.MinData1Value;
            double bottom = (double)mappingEntry.InMssMsgRange.Data1RangeBottom;
            double top = (double)mappingEntry.InMssMsgRange.Data1RangeTop;
            if (inputXVal < ((bottom - min) / (max - min + 1)) ||
                inputXVal > ((top - min) / (max - min + 1)))
            {
                relData1 = double.NaN;
            }

            //If curXVal is outside of the relative input range for data 2 then set relData2
            //to NaN
            max = (double)inMsgInfo.MaxData2Value;
            min = (double)inMsgInfo.MinData2Value;
            bottom = (double)mappingEntry.InMssMsgRange.Data2RangeBottom;
            top = (double)mappingEntry.InMssMsgRange.Data2RangeTop;
            if (inputXVal < ((bottom - min) / (max - min + 1)) ||
                inputXVal > ((top - min) / (max - min + 1)))
            {
                relData2 = double.NaN;
            }

            var evalInput = new EvaluationCurveInput();
            evalInput.Init(
                relData1,
                relData2,
                relData3,
                variableParamInfoList,
                mappingEntry);

            var evalJob = new EvaluationCurveJob();

            var returnedExpression = CreateExpressionFromString(mappingEntry.CurveShapeInfo.CurveEquations[curveIndex], EvalType.Curve);

            if (returnedExpression.IsValid == false)
            {
                return new ReturnStatus<XyPoint<double>>();
            }

            evalJob.Configure(evalInput, controlPointList, returnedExpression.Value);

            evalJob.Execute();

            if (evalJob.OutputIsValid)
            {
                var curPoint = new XyPoint<double>(inputXVal, evalJob.OutputVal);
                return new ReturnStatus<XyPoint<double>>(curPoint);
            }
            else
            {
                return new ReturnStatus<XyPoint<double>>();
            }
        }