コード例 #1
0
        public void DynamicBaseClassCollectionNotInTest()
        {
            DynamicBaseClass person = new DynamicBaseClass()
            {
                Name = "Person", ReferenceName = "person", Type = ObjectType.Object
            };

            person.Fields.Add(new { Name = "Joe", Country = "Ireland", Age = 25 });

            DynamicBaseClass collection = new DynamicBaseClass()
            {
                Name = "CollectionPerson", ReferenceName = "collectionperson", Type = ObjectType.Collection
            };

            collection.Fields.Add(new { Name = "Joe", Country = "Ireland", Age = 20 });
            collection.Fields.Add(new { Name = "Bert", Country = "USA", Age = 40 });
            collection.Fields.Add(new { Name = "Carl", Country = "USA", Age = 50 });
            collection.Fields.Add(new { Name = "Carol", Country = "Germany", Age = 60 });

            ExpressionRuleLoader expressionRuleLoader = new ExpressionRuleLoader();
            Rule          rule1         = new Rule(" person[Age] notin collectionperson[Age] ");
            RuleValidator ruleValidator = new RuleValidator();
            var           result        = ruleValidator.ValidateExpressionRulesAll(
                new DynamicBaseClass[] { person, collection },
                new Rule[] { rule1 });

            Assert.AreEqual(result, true);
        }
コード例 #2
0
        public void DynamicBaseClassSimpleTest()
        {
            DynamicBaseClass person = new DynamicBaseClass()
            {
                Name = "Person", ReferenceName = "person", Type = ObjectType.Object
            };

            person.Fields.Add(new { Name = "Joe", Country = "Ireland", Age = 25 });

            DynamicBaseClass collection = new DynamicBaseClass()
            {
                Name = "PersonCollection", ReferenceName = "personcollection", Type = ObjectType.Object
            };

            collection.Fields.Add(new { Total = 100, AverageAge = 99, Min = 1, Max = 100 });

            ExpressionRuleLoader expressionRuleLoader = new ExpressionRuleLoader();
            Rule rule1 = new Rule(
                " (person[Name].lower = 'joe' && " +
                "person[Age] <= personcollection[AverageAge]) ");
            RuleValidator ruleValidator = new RuleValidator();
            var           result        = ruleValidator.ValidateExpressionRulesAll(
                new DynamicBaseClass[] { person, collection },
                new Rule[] { rule1 });

            Assert.AreEqual(result, true);
        }
コード例 #3
0
ファイル: RuleValidator.cs プロジェクト: yan2oo7/ruleengine
        public bool ValidateExpressionRulesAll(DynamicBaseClass value, Rule[] rules)
        {
            Evaluator evaluator = new Evaluator();

            foreach (var rule in rules)
            {
                if (rule.ProcessingRule != string.Empty)
                {
                    if (!evaluator.EvaluateDynamic(rule.ProcessingRule, value))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
コード例 #4
0
        public void DynamicBaseClassLengthTest()
        {
            DynamicBaseClass person = new DynamicBaseClass()
            {
                Name = "Person", ReferenceName = "person", Type = ObjectType.Object
            };

            person.Fields.Add(new { Name = "Joe", Country = "Ireland", Age = 25 });

            Rule rule1 = new Rule(
                " person[Name].length = 3 ");
            RuleValidator ruleValidator = new RuleValidator();
            var           result        = ruleValidator.ValidateExpressionRulesAll(
                new DynamicBaseClass[] { person },
                new Rule[] { rule1 });

            Assert.AreEqual(result, true);
        }
コード例 #5
0
        public void DynamicBaseClassFilterCountTest()
        {
            DynamicBaseClass collection = new DynamicBaseClass()
            {
                Name = "CollectionPerson", ReferenceName = "collectionperson", Type = ObjectType.Collection
            };

            collection.Fields.Add(new { Name = "Joe", Country = "Ireland", Age = 25 });
            collection.Fields.Add(new { Name = "Bert", Country = "USA", Age = 60 });
            collection.Fields.Add(new { Name = "Carl", Country = "USA", Age = 30 });
            collection.Fields.Add(new { Name = "Eric", Country = "Sweden", Age = 30 });

            ExpressionRuleLoader expressionRuleLoader = new ExpressionRuleLoader();
            Rule          rule1         = new Rule(" collectionperson.Filter[Age > 30].Count >= 1 ");
            RuleValidator ruleValidator = new RuleValidator();
            var           result        = ruleValidator.ValidateExpressionRulesAll(
                new DynamicBaseClass[] { collection },
                new Rule[] { rule1 });

            Assert.AreEqual(result, true);
        }
コード例 #6
0
        public void DynamicBaseClassComplexTest()
        {
            DynamicBaseClass person = new DynamicBaseClass()
            {
                Name = "Person", ReferenceName = "person", Type = ObjectType.Object
            };

            person.Fields.Add(new { Name = "Joe", Country = "Ireland", Age = 25 });

            DynamicBaseClass collection = new DynamicBaseClass()
            {
                Name = "CollectionPerson", ReferenceName = "collectionperson", Type = ObjectType.Object
            };

            collection.Fields.Add(new { Total = 100, AverageAge = 99, Min = 1, Max = 100 });

            DynamicBaseClass averagePerson = new DynamicBaseClass()
            {
                Name = "AveragePerson", ReferenceName = "averageperson", Type = ObjectType.Object
            };

            averagePerson.Fields.Add(new { AverageAge = 30, StandardDeviation = 7 });

            ExpressionRuleLoader expressionRuleLoader = new ExpressionRuleLoader();
            Rule rule1 = new Rule(
                " (person[Name] = 'Joei' && " +
                "person[Age] <= averageperson[AverageAge]) " +
                "|| (person[Age] > collectionperson[Total]) " +
                "|| collectionperson[Total] = 100 ");
            RuleValidator ruleValidator = new RuleValidator();
            var           result        = ruleValidator.ValidateExpressionRulesAll(
                new DynamicBaseClass[] { person, collection, averagePerson },
                new Rule[] { rule1 });

            Assert.AreEqual(result, true);
        }