Exemplo n.º 1
0
        /// <summary>
        /// This method is validates incoming transactions with all active rules and returns a score
        /// </summary>
        /// <returns>double</returns>
        public double RunRules(TransactionParameters incoming)
        {
            double  ruleScore      = 0;
            dynamic rightParameter = null;
            dynamic leftParameter  = null;
            dynamic expectedResult = null;
            dynamic result         = null;

            try
            {
                foreach (var rule in GetActiveRules())
                {
                    if (rule.RuleDetails == null)
                    {
                        continue;
                    }
                    var ruleDetails = rule.RuleDetails.OrderBy(p => p.Order).ToList();
                    foreach (var detail in ruleDetails)
                    {
                        if (!string.IsNullOrEmpty(detail.LeftOperator) || !detail.IsChained)
                        {
                            leftParameter = TypeConversion.ConvertToType(detail.LeftOperator, detail.RuleType);
                        }
                        else if (string.IsNullOrEmpty(detail.LeftOperator) || !detail.IsChained)
                        {
                            if (string.IsNullOrEmpty(detail.RightParamererString))
                            {
                                throw new Exception("");
                            }
                            var                type         = Type.GetType(detail.RuleType);
                            PropertyInfo       info         = type.GetProperty(detail.LeftParameterString);
                            ConstantExpression constantExpr = Expression.Constant(incoming);
                            Expression         getExpr      = Expression.Property(constantExpr, detail.LeftParameterString);
                            leftParameter = Expression.Lambda <Func <dynamic> >(getExpr).Compile()();
                        }
                        else if (detail.IsChained)
                        {
                            leftParameter = TypeConversion.ConvertToType(result, detail.RuleType);
                        }
                        if (!string.IsNullOrEmpty(detail.RightOperator))
                        {
                            rightParameter = TypeConversion.ConvertToType(detail.RightOperator, detail.RuleType);
                        }
                        else if (string.IsNullOrEmpty(detail.RightOperator))
                        {
                            if (string.IsNullOrEmpty(detail.RightParamererString))
                            {
                                throw new Exception("");
                            }
                            var                type         = Type.GetType(detail.RuleType);
                            PropertyInfo       info         = type.GetProperty(detail.LeftParameterString);
                            ConstantExpression constantExpr = Expression.Constant(incoming);
                            Expression         getExpr      = Expression.Property(constantExpr, detail.RightParamererString);
                            rightParameter = Expression.Lambda <Func <dynamic> >(getExpr).Compile()();
                        }
                        expectedResult = TypeConversion.ConvertToType(detail.ExpectedResult, detail.ExpectedResultType);
                        result         = new Operator().RunTest(leftParameter, rightParameter, expectedResult, detail.Operand);
                        if (result != expectedResult && detail.CanBeTerminated && detail.ExpectsResult)
                        {
                            break;
                        }
                        else if (result == expectedResult && detail.IsLast)
                        {
                            ruleScore += rule.RuleScore;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                return(ruleScore);
            }
            catch (Exception ex)
            {
                return(0);
            }
        }