Example #1
0
 public EvaluationVisitor(EvaluateOptions options, ushort?time, uint?step, float?deltaTime, EvaluateParameterAsyncHandler evaluateParameterAsync, EvaluateFunctionAsyncHandler evaluateFunctionAsync)
 {
     _options   = options;
     _time      = time;
     _step      = step;
     _deltaTime = deltaTime;
     _evaluateParameterAsync = evaluateParameterAsync;
     _evaluateFunctionAsync  = evaluateFunctionAsync;
 }
        /// <summary>
        /// Evaluate the expression asynchronously.
        /// </summary>
        /// <param name="evaluateParameterAsync">Override the value of <see cref="EvaluateParameterAsync"/></param>
        /// <param name="evaluateFunctionAsync">Override the value of <see cref="EvaluateFunctionAsync"/></param>
        /// <returns>A task that resolves to the result of the expression.</returns>
        public async Task <object> EvaluateAsync(EvaluateParameterAsyncHandler evaluateParameterAsync, EvaluateFunctionAsyncHandler evaluateFunctionAsync)
        {
            if (HasErrors())
            {
                throw new EvaluationException(Error);
            }

            if (ParsedExpression == null)
            {
                ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache);
            }

            var visitor = new EvaluationVisitor(Options, evaluateParameterAsync, evaluateFunctionAsync)
            {
                Parameters = Parameters
            };

            // if array evaluation, execute the same expression multiple times
            if ((Options & EvaluateOptions.IterateParameters) == EvaluateOptions.IterateParameters)
            {
                int size = -1;
                ParametersBackup = new Dictionary <string, object>();
                foreach (string key in Parameters.Keys)
                {
                    ParametersBackup.Add(key, Parameters[key]);
                }

                ParameterEnumerators = new Dictionary <string, IEnumerator>();

                foreach (object parameter in Parameters.Values)
                {
                    if (parameter is IEnumerable)
                    {
                        int localsize = 0;
                        foreach (object o in (IEnumerable)parameter)
                        {
                            localsize++;
                        }

                        if (size == -1)
                        {
                            size = localsize;
                        }
                        else if (localsize != size)
                        {
                            throw new EvaluationException("When IterateParameters option is used, IEnumerable parameters must have the same number of items");
                        }
                    }
                }

                foreach (string key in Parameters.Keys)
                {
                    var parameter = Parameters[key] as IEnumerable;
                    if (parameter != null)
                    {
                        ParameterEnumerators.Add(key, parameter.GetEnumerator());
                    }
                }

                var results = new List <object>();
                for (int i = 0; i < size; i++)
                {
                    foreach (string key in ParameterEnumerators.Keys)
                    {
                        IEnumerator enumerator = ParameterEnumerators[key];
                        enumerator.MoveNext();
                        Parameters[key] = enumerator.Current;
                    }

                    await ParsedExpression.AcceptAsync(visitor);

                    results.Add(visitor.Result);
                }

                return(results);
            }

            await ParsedExpression.AcceptAsync(visitor);

            return(visitor.Result);
        }
 public EvaluationVisitor(EvaluateOptions options, EvaluateParameterAsyncHandler evaluateParameterAsync, EvaluateFunctionAsyncHandler evaluateFunctionAsync)
 {
     _options = options;
     _evaluateParameterAsync = evaluateParameterAsync;
     _evaluateFunctionAsync  = evaluateFunctionAsync;
 }