protected void EncapsulateNoValue(LogicalExpression expression)
        {
            if (expression is BinaryExpression)
            {
                var type       = (expression as BinaryExpression).Type;
                var isNewBlock = type != this.ops.Peek() && (type == BinaryExpressionType.And || type == BinaryExpressionType.Or);
                if (isNewBlock)
                {
                    Result.Append("(");
                }

                expression.Accept(this);

                if (isNewBlock)
                {
                    // trim spaces before adding a closing paren
                    while (Result[Result.Length - 1] == ' ')
                    {
                        Result.Remove(Result.Length - 1, 1);
                    }

                    Result.Append(") ");
                }
            }
            else
            {
                expression.Accept(this);
            }
        }
Esempio n. 2
0
        public Func <TResult> ToLambda <TResult>()
        {
            if (HasErrors())
            {
                throw new EvaluationException(Error, ErrorException);
            }

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

            var visitor = new LambdaExpressionVistor(Parameters, Options);

            ParsedExpression.Accept(visitor);

            var body = visitor.Result;

            if (body.Type != typeof(TResult))
            {
                body = System.Linq.Expressions.Expression.Convert(body, typeof(TResult));
            }

            var lambda = System.Linq.Expressions.Expression.Lambda <Func <TResult> >(body);

            return(lambda.Compile());
        }
Esempio n. 3
0
        public HashSet <string> UsedParameters()
        {
            Check();
            var visitor = new ParameterExtractionVisitor();

            ParsedExpression.Accept(visitor);
            return(visitor.Parameters);
        }
Esempio n. 4
0
        public bool ParseInt(out int value)
        {
            bool parsed = false;

            value = 0;
            if (IsValid)
            {
                try
                {
                    NCalc.Expression  expr  = new NCalc.Expression(Expression);
                    LogicalExpression pexpr = NCalc.Expression.Compile(Expression, false);
                    ExprVisitor       v     = new ExprVisitor();

                    pexpr.Accept(v);

                    if (!v.ComplexExpression)
                    {
                        value  = Convert.ToInt32(expr.Evaluate());
                        parsed = true;
                    }
                }
                catch (Exception)
                {
                }
            }

            return(parsed);
        }
Esempio n. 5
0
        public object Evaluate()
        {
            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.TaxFunction                       += TaxFunction;
            visitor.TaxWithPointFunction              += TaxWithPointFunction;
            visitor.AnnualBonusTaxFunction            += AnnualBonusTaxFunction;
            visitor.ForeignTaxFunction                += ForeignTaxFunction;
            visitor.IsSalaryEndDateMonthEquelFunction += IsSalaryEndDateMonthEquelFunction;
            visitor.DoubleSalaryFunction              += DoubleSalaryFunction;
            visitor.AnnualBonusForeignTaxFunction     += AnnualBonusForeignTaxFunction;
            visitor.EvaluateFunction                  += EvaluateFunction;
            visitor.EvaluateParameter                 += EvaluateParameter;
            visitor.Parameters = parameters;

            //LogicalExpression.Create(Parse(expression)).Accept(visitor);
            //modify by wsl
            CommonTree ct = Parse(expression);
            //将表达式转化成LogicalExpression对象
            LogicalExpression le = LogicalExpression.Create(ct);

            //计算表达式
            le.Accept(visitor);
            return(visitor.Result);
        }
Esempio n. 6
0
        protected void EncapsulateNoValue(LogicalExpression expression)
        {
            if (expression is ValueExpression)
            {
                expression.Accept(this);
            }
            else
            {
                Result.Append("(");
                expression.Accept(this);

                // trim spaces before adding a closing paren
                while (Result[Result.Length - 1] == ' ')
                {
                    Result.Remove(Result.Length - 1, 1);
                }

                Result.Append(") ");
            }
        }
Esempio n. 7
0
        public object Evaluate(LogicalExpression InputLogicExpression = null)
        {
            if (InputLogicExpression == null) //evaluate with existing logical expression
            {
                if (HasErrors())
                {
                    throw new EvaluationException(Error);
                }

                if (ParsedExpression == null)
                {
                    ParsedExpression = Compile(OriginalExpression, (Options & EvaluateOptions.NoCache) == EvaluateOptions.NoCache);
                }
            }
            else //New evaluate
            {
                this.ParsedExpression = InputLogicExpression;
            }

            //
            if (ParsedExpression == null)
            {
                return(null);
            }


            var visitor = new EvaluationVisitor(Options);

            visitor.EvaluateFunction  += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.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;
                    }

                    ParsedExpression.Accept(visitor);
                    results.Add(visitor.Result);
                }

                return(results);
            }

            ParsedExpression.Accept(visitor);

            //Record data
            ParsedExpression.objResult = visitor.Result;

            //Resturn data
            return(visitor.Result);
        }
 public override void Visit(LogicalExpression expression)
 {
     expression.Accept(this);
 }
Esempio n. 9
0
        public object Evaluate()
        {
            if (HasErrors())
            {
                throw new EvaluationException(Error);
            }

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

            var visitor = new EvaluationVisitor(Options);

            visitor.EvaluateFunction  += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.Parameters         = Parameters;

            // Add a "null" parameter which returns null if configured to do so
            // Configured as an option to ensure no breaking changes for historical use
            if (Options.AllowNullParameter() && !visitor.Parameters.ContainsKey("null"))
            {
                visitor.Parameters["null"] = null;
            }

            // if array evaluation, execute the same expression multiple times
            if (Options.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)
                {
                    var enumerable = parameter as IEnumerable;
                    if (enumerable != null)
                    {
                        int localsize = enumerable.Cast <object>().Count();

                        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;
                    }

                    ParsedExpression.Accept(visitor);
                    results.Add(visitor.Result);
                }

                return(results);
            }

            ParsedExpression.Accept(visitor);
            return(visitor.Result);
        }
Esempio n. 10
0
        public object Evaluate()
        {
            if (HasErrors())
            {
                throw new EvaluationException(Error);
            }

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


            var visitor = new EvaluationVisitor(Options);

            visitor.EvaluateFunction  += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.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(var key in Parameters.Keys)
                    var __enumerator3 = (Parameters.Keys).GetEnumerator();
                    while (__enumerator3.MoveNext())
                    {
                        var key = (string)__enumerator3.Current;
                        {
                            ParametersBackup.Add(key, Parameters[key]);
                        }
                    }
                }

                ParameterEnumerators = new Dictionary <string, IEnumerator>();
                {
                    // foreach(var parameter in Parameters.Values)
                    var __enumerator4 = (Parameters.Values).GetEnumerator();
                    while (__enumerator4.MoveNext())
                    {
                        var parameter = (object)__enumerator4.Current;
                        {
                            if (parameter is IEnumerable)
                            {
                                int localsize = 0;
                                {
                                    // foreach(var o in (IEnumerable)parameter)
                                    var __enumerator7 = ((IEnumerable)parameter).GetEnumerator();
                                    while (__enumerator7.MoveNext())
                                    {
                                        var o = (object)__enumerator7.Current;
                                        {
                                            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(var key in Parameters.Keys)
                    var __enumerator5 = (Parameters.Keys).GetEnumerator();
                    while (__enumerator5.MoveNext())
                    {
                        var key = (string)__enumerator5.Current;
                        {
                            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(var key in ParameterEnumerators.Keys)
                        var __enumerator6 = (ParameterEnumerators.Keys).GetEnumerator();
                        while (__enumerator6.MoveNext())
                        {
                            var key = (string)__enumerator6.Current;
                            {
                                IEnumerator enumerator = ParameterEnumerators[key];
                                enumerator.MoveNext();
                                Parameters[key] = enumerator.Current;
                            }
                        }
                    }

                    ParsedExpression.Accept(visitor);
                    results.Add(visitor.Result);
                }

                return(results);
            }

            ParsedExpression.Accept(visitor);
            return(visitor.Result);
        }
Esempio n. 11
0
 private object Evaluate(LogicalExpression expression)
 {
     expression.Accept(this);
     return(Result);
 }
Esempio n. 12
0
        public object Evaluate()
        {
            if (HasErrors())
            {
                throw new EvaluationException(Error);
            }

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

            var visitor = new EvaluationVisitor(Options);

            visitor.EvaluateFunction  += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.Parameters         = Parameters;

            // if array evaluation, execute the same expression multiple times
            if (Options.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)
                {
                    var enumerable = parameter as IEnumerable;
                    if (enumerable != null)
                    {
                        int localsize = enumerable.Cast <object>().Count();

                        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;
                    }

                    ParsedExpression.Accept(visitor);
                    var res = visitor.Result;
                    results.Add(res == CalcNull.Value ? null : res);
                }

                return(results);
            }

            ParsedExpression.Accept(visitor);
            var result = visitor.Result;

            return(result == CalcNull.Value ? null : result);
        }
 protected void EncapsulateNoValue(LogicalExpression expression)
 {
     expression.Accept(this);
 }