private void EvaluateFunction(string name, NCalc.FunctionArgs args)
        {
            if (String.Equals(name, FunctionIsNull, StringComparison.OrdinalIgnoreCase))
            {
                args.HasResult = true;
                args.Result    = args.EvaluateParameters().Any(x => x == null);
            }
            else if (String.Equals(name, FunctionCount, StringComparison.OrdinalIgnoreCase))
            {
                args.HasResult = true;
                args.Result    = AggregationFunctions.Count(_descriptor.Value, _dataset.Culture, AggregationFunctions.Unpack(args.EvaluateParameters()));
            }
            else if (String.Equals(name, FunctionAverage, StringComparison.OrdinalIgnoreCase))
            {
                args.HasResult = true;
                args.Result    = AggregationFunctions.Average(_descriptor.Value, _dataset.Culture, AggregationFunctions.Unpack(args.EvaluateParameters()));
            }
            else if (String.Equals(name, FunctionLet, StringComparison.OrdinalIgnoreCase))
            {
                if (args.Parameters.Length != 2)
                {
                    throw new ArgumentException($"Invalid number of arguments for {FunctionLet}().");
                }

                var result = args.Parameters[1].Evaluate();
                _currentExpression.Parameters.Add(Convert.ToString(args.Parameters[0].Evaluate()), result);

                args.HasResult = true;
                args.Result    = result;
            }
            else if (String.Equals(name, FunctionLastOf, StringComparison.OrdinalIgnoreCase))
            {
                args.HasResult = true;
                args.Result    = args.EvaluateParameters().LastOrDefault();
            }
            else if (String.Equals(name, FunctionSum, StringComparison.OrdinalIgnoreCase))
            {
                if (args.Parameters.Length == 0 || args.Parameters.Length > 2)
                {
                    throw new ArgumentException($"Invalid number of arguments for {FunctionSum}().");
                }

                var set = (IEnumerable <object>)args.Parameters[0].Evaluate();
                if (args.Parameters.Length == 2)
                {
                    set = AggregationFunctions.Project(_descriptor.Value, _dataset.Culture, set, args.Parameters[1]);
                }

                args.HasResult = true;
                args.Result    = AggregationFunctions.Sum(_descriptor.Value, _dataset.Culture, set);
            }
        }
예제 #2
0
        private void Exp_EvaluateFunction(string name, NCalc.FunctionArgs args)
        {
            var p = args.EvaluateParameters();

            args.HasResult = false;
            switch (name)
            {
            case "_":
                if (p.Length > 0)
                {
                    args.HasResult = true;
                    if (_isStateDependent && _internalState != null)
                    {
                        args.Result = GetValueByPath(_internalState, p[0].ToString());
                    }
                    else
                    {
                        args.Result = Dispatcher.Invoke <object>(() => GetValueByPath(State, p[0].ToString()));
                    }
                }
                break;

            //string functions
            case "split":
                if (p.Length > 1 && p[0] is string && p[1] is string)
                {
                    args.HasResult = true;
                    args.Result    = ((string)p[0]).Split(new string[] { (string)p[1] }, StringSplitOptions.RemoveEmptyEntries);
                }
                break;

            case "trim":
                if (p.Length > 0 && p[0] is string)
                {
                    args.HasResult = true;
                    args.Result    = ((string)p[0]).Trim();
                }
                break;

            //vMix functions
            case "API":
                //state.SendFunction(string.Format(cmd.Action.FormatString, cmd.InputKey, CalculateExpression<int>(cmd.Parameter), Dispatcher.Invoke(() => CalculateObjectParameter(cmd)), CalculateExpression<int>(cmd.Parameter) - 1, input.HasValue ? input.Value : 0), false);
                break;

            //array functions
            case "getvalue":
                if (p.Length > 1 && p[0] is Array && p[1] is int)
                {
                    args.HasResult = true;
                    args.Result    = ((Array)p[0]).GetValue((int)p[1]);
                }
                break;
            }
        }