Exemplo n.º 1
0
        private static EvaluateExpressionDelegate Evaluator(Func <IReadOnlyList <object>, bool> function, FunctionUtils.VerifyExpression verify)
        {
            return((expression, state, options) =>
            {
                var result = false;
                string error = null;
                IReadOnlyList <object> args;
                (args, error) = FunctionUtils.EvaluateChildren(expression, state, new Options(options)
                {
                    NullSubstitution = null
                }, verify);
                if (error == null)
                {
                    // Ensure args are all of same type
                    bool?isNumber = null;
                    foreach (var arg in args)
                    {
                        var obj = arg;
                        if (isNumber.HasValue)
                        {
                            if (obj != null && obj.IsNumber() != isNumber.Value)
                            {
                                error = $"Arguments must either all be numbers or strings in {expression}";
                                break;
                            }
                        }
                        else
                        {
                            isNumber = obj.IsNumber();
                        }
                    }

                    if (error == null)
                    {
                        try
                        {
                            result = function(args);
                        }
#pragma warning disable CA1031 // Do not catch general exception types (we are capturing the exception and returning it)
                        catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                        {
                            // NOTE: This should not happen in normal execution
                            error = e.Message;
                        }
                    }
                }
                else
                {
                    // Swallow errors and treat as false
                    error = null;
                }

                return (result, error);
            });
        }
Exemplo n.º 2
0
 public ComparisonEvaluator(string type, Func <IReadOnlyList <object>, bool> function, ValidateExpressionDelegate validator, FunctionUtils.VerifyExpression verify = null)
     : base(type, Evaluator(function, verify), ReturnType.Boolean, validator)
 {
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MultivariateNumericEvaluator"/> class.
 /// </summary>
 /// <param name="type">Name of the function.</param>
 /// <param name="function"> The multivariate numeric function, it takes a list of objects as input and returns an object.</param>
 /// <param name = "verify" > Optional function to verify each child's result.</param>
 public MultivariateNumericEvaluator(string type, Func <IReadOnlyList <object>, object> function, FunctionUtils.VerifyExpression verify = null)
     : base(type, Evaluator(function, verify), ReturnType.Number, FunctionUtils.ValidateTwoOrMoreThanTwoNumbers)
 {
 }
Exemplo n.º 4
0
 private static EvaluateExpressionDelegate Evaluator(Func <IReadOnlyList <object>, object> function, FunctionUtils.VerifyExpression verify = null)
 {
     return(FunctionUtils.ApplySequence(function, verify ?? FunctionUtils.VerifyNumber));
 }
        private static EvaluateExpressionDelegate Evaluator(Func <IReadOnlyList <object>, bool> function, FunctionUtils.VerifyExpression verify)
        {
            return((expression, state, options) =>
            {
                var result = false;
                string error = null;
                IReadOnlyList <object> args;
                (args, error) = FunctionUtils.EvaluateChildren(expression, state, new Options(options)
                {
                    NullSubstitution = null
                }, verify);
                if (error == null)
                {
                    try
                    {
                        result = function(args);
                    }
#pragma warning disable CA1031 // Do not catch general exception types (we are capturing the exception and returning it)
                    catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                    {
                        // NOTE: This should not happen in normal execution
                        error = e.Message;
                    }
                }
                else
                {
                    // Swallow errors and treat as false
                    error = null;
                }

                return (result, error);
            });
        }