Ejemplo n.º 1
0
        public static List <RuleAwareEntity> PopulateDynamicData(int blogCount = 35, int postCount = 40)
        {
            CreateBlogClass();

            List <RuleAwareEntity> lstBlog = new List <RuleAwareEntity>();

            for (int i = 0; i < blogCount; i++)
            {
                var blog = new RuleAwareEntity("Blog");
                blog.Id = i + 1;
                blog.TrySetDynamicProperty("Name", "Blog" + (i + 1));
                blog.TrySetDynamicProperty("Created", DateTime.Today.AddDays(-Rnd.Next(0, 100)));
                lstBlog.Add(blog);
                for (int j = 0; j < postCount; j++)
                {
                    var post = new RuleAwareEntity("Post");
                    post.Id = j + 1;
                    post.TrySetDynamicProperty("Blog", blog);
                    post.TrySetDynamicProperty("Title", $"Blog {i + 1} - Post {j + 1}");
                    post.TrySetDynamicProperty("Content", "My Content");
                    post.TrySetDynamicProperty("PostDate", DateTime.Today.AddDays(-Rnd.Next(0, 100)).AddSeconds(Rnd.Next(0, 30000)));
                    post.TrySetDynamicProperty("NumberOfReads", Rnd.Next(0, 5000));

                    blog.EnumerableComplexProp("Posts").Add(post);
                }

                Console.WriteLine("Item Created: " + i);
            }
            return(lstBlog);
        }
Ejemplo n.º 2
0
        private static void CreateBlogClass()
        {
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, int>("Blog", "BlogId");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, string>("Blog", "Name");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, int?>("Blog", "NullableInt");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, DateTime>("Blog", "Created");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, int>("Blog", "LikeCount");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, List <RuleAwareEntity> >("Blog", "Posts");

            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, int>("Post", "PostId");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, string>("Post", "Title");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, string>("Post", "Content");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, int>("Post", "BlogId");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, RuleAwareEntity>("Post", "Blog");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, int>("Post", "NumberOfReads");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, DateTime>("Post", "PostDate");
            RuleAwareEntity.CreateDynamicPropertyForType <RuleAwareEntity, string>("Post", "Satus");
        }
Ejemplo n.º 3
0
        public static void RuleEngineTest1()
        {
            var conetxt = RuleEngineContextHolder.GenerateContext(ruleEngineId, "UserProfile");

            conetxt.AssociateDynamicType <UserProfile>("UserProfile", "y");
            conetxt.AssociateDynamicType <UserProfileDetails>("UserProfileDetails", "x");

            RuleAwareEntity.CreateDynamicPropertyForType <UserProfile, double>("UserProfile", "Bonus");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfile, DateTime>("UserProfile", "BonusPayOutDate");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfile, string>("UserProfile", "BonusStatus");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfile, string>("UserProfile", "AgeStatus");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfile, string>("UserProfile", "SalaryStatus");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfileDetails, int>("UserProfileDetails", "Dynamic");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfileDetails, string>("UserProfileDetails", "DynamicStatus");
            RuleAwareEntity.CreateDynamicPropertyForType <UserProfileDetails, List <string> >("UserProfileDetails", "LstStringDynamic",
                                                                                              new List <string> {
                "Pamli", "Anaya", "Siya"
            });
            //DynamicRuleEntity.CreateDynamicPropertyForType<, string>("UserProfile", "SalaryStatus");
            conetxt
            .AssociateChildDynamicType <UserProfile, UserProfileDetails>("UserProfile", "UserProfileDetails", "UserProfileDetails");



            List <UserProfile> lst = GenerateData();

            foreach (var item in lst)
            {
                Console.WriteLine("Employee: "
                                  + item.FirstName
                                  + " Bonus: "
                                  + item.DoubleProp("Bonus")
                                  + " Age: "
                                  + item.Age
                                  + " Salary:"
                                  + item.UserProfileDetails.Salary
                                  + " Overtime:" + item.Overtime);

                //item.Age = 40;
            }

            Console.WriteLine();

            string s;


            var mathruleset = RulesetBuilder <UserProfile>
                              .Create(conetxt)
                              .WithName("Maths department Bounus")
                              .WithEntryCriteria("{UserProfileDetails.Department}.ToLower().Contains(\"math\")")
                              // .WithEntryCriteria("{UserProfileDetails.Department}.ToLower().Contains({UserProfileDetails.Department})")

                              /* .WithWhere("{UserProfileDetails.Department}.StartsWith(\"M\") " +
                               * "And {UserProfileDetails.Department}.Contains(\"at\")" +
                               * "And {UserProfileDetails.Department}.EndsWith(\"h\")")*/
                              .WithRule("({Age}>40 And {Overtime}>40) Or ({UserProfileDetails.Salary}*100< 150)")
                              .SetProperty("Bonus", 100.00)
                              .SetPropertyExpression("UserProfileDetails.LeaveBalance", "2*{UserProfileDetails.LeaveBalance}")

                              .SetProperty("Overtime", 100)
                              .Attach()
                              .WithRule("({Age}<40 And {Overtime}<40) Or ({UserProfileDetails}.{Salary}< 150)")
                              .SetProperty("Bonus", 150.00)
                              .SetProperty("UserProfileDetails.LeaveBalance", 60)
                              .SetProperty <int>("UserProfileDetails.Dynamic", 60)
                              .SetPropertyExpression("Overtime", "{Overtime}*2+1000")
                              .SetPropertyExpression("UserProfileDetails.Dynamic", "{UserProfileDetails.Dynamic}*2+1000")
                              .Attach()
                              .Compile();

            var itruleset = RulesetBuilder <UserProfile>
                            .Create(conetxt)
                            .WithName("IT department Bounus")
                            .WithJobExecutionRule(RulesEngine.RuleExecutionRule.RuleExecutionRuleEnum.FirstMatch)
                            .WithEntryCriteria("{UserProfileDetails}.{Department} = \"IT\"")
                            .WithRule("({Age}<=35 And {Overtime}>10) Or ({UserProfileDetails}.{Salary}*100< 150)")
                            .SetProperty("Bonus", 75.00)
                            .SetProperty("UserProfileDetails.LeaveBalance", 70)
                            .SetProperty("Overtime", 500)
                            .Attach()
                            .WithRule("({Age}>=35 And {Overtime}>40) Or ({UserProfileDetails}.{Salary}> 25)")

                            .SetProperty("Bonus", 85.00)
                            .SetProperty("UserProfileDetails.LeaveBalance", 84)
                            .SetProperty("Overtime", 200)
                            .Attach()
                            .Compile();

            var healthruleset = RulesetBuilder <UserProfile>
                                .Create(conetxt)
                                .WithName("Health department Bounus")
                                .WithEntryCriteria("{UserProfileDetails}.{Department} = \"Health\"")
                                .WithRule("({Age}<=35 And {Overtime}>=10) Or ({UserProfileDetails}.{Salary}< 150)")
                                .SetProperty("Bonus", 175.00)
                                .SetProperty("UserProfileDetails.LeaveBalance", 33)
                                .SetProperty("Overtime", 200)
                                .Attach()
                                .WithRule("({Age}>35 And {Overtime}>40) Or ({UserProfileDetails}.{Salary}> 150)")
                                .SetProperty("Bonus", 124.00)
                                .SetProperty("UserProfileDetails.LeaveBalance", 66)
                                .SetProperty("Overtime", 200)
                                .Attach()
                                .Compile();

            var dynamicChild = RulesetBuilder <UserProfile>
                               .Create(conetxt)
                               .WithName("Dynamic Status")

                               .WithRule("{UserProfileDetails.Dynamic} > 0")
                               .SetPropertyExpression
                                   ("UserProfileDetails.DynamicStatus", "string.Concat(\"Dynamic is Greater than 0 for-\", {FirstName}) + {LastName}")

                               .Attach()
                               .WithRule("{UserProfileDetails.Dynamic} < 0")
                               .SetPropertyExpression("UserProfileDetails.DynamicStatus", "\"Dynamic is less than 0 \"")

                               .Attach()
                               .WithDefaultRule()
                               .SetProperty("UserProfileDetails.DynamicStatus", "Dynamic is equal to 0")
                               .Compile();

            //lst[0].UserProfileDetails.LstString.Where(s1 => s1.Equals(lst[0].FirstName));

            /*  var a = lst[0];
             * var vv= a.UserProfileDetails.EnumerableStringProp("LstStringDynamic").Where(x => !x.Equals(a.FirstName) && !x.Equals("Anaya")).ToList();
             * var vvv = a.UserProfileDetails.LstString.Where(x => !x.Equals(a.FirstName)).ToList();
             * var vDyna = a.UserProfileDetails.LstString.AsQueryable().Where("!Equals(FirstName)").ToList();*/
            // a.SetPropertyValue("UserProfileDetails.LstString", a.UserProfileDetails.LstString.Where(x => !x.Equals(a.FirstName) && !x.Equals("Anaya")).ToList());

            var dynamicChild1 = RulesetBuilder <UserProfile>
                                .Create(conetxt)
                                .WithName("Dynamic Status")

                                //.WithRule("{UserProfileDetails.Dynamic} > 0 And {UserProfileDetails.LstString}.Any(Equals(\"Abhik\")) ")
                                //.WithRule("{UserProfileDetails.Dynamic} > 0 And {UserProfileDetails.LstString}.Contains(\"Abhik\") ")
                                //.WithRule("{UserProfileDetails.Dynamic} > 0 And {UserProfileDetails.LstStringDynamic}.Contains(\"Abhik\") ")
                                .WithRule("{UserProfileDetails.Dynamic} > 0 And {UserProfileDetails.LstStringDynamic}.Contains(\"Pamli\") ")
                                .SetPropertyExpression
                                //("UserProfileDetails", "new UserProfileDetails(\"Econo\" as  Department, 5000 as Salary, 45 as LeaveBalance, List.OfString(\"Abhik\") as LstString)")
                                // ("UserProfileDetails", "new UserProfileDetails({UserProfileDetails.DynamicStatus} as  Department, 5000 as Salary, 45 as LeaveBalance, List.OfString(\"Abhik\") as LstString)")
                                    ("UserProfileDetails", "new UserProfileDetails({UserProfileDetails.DynamicStatus} as  Department, 5000 as Salary, 45 as LeaveBalance, List.OfString(\"Abhik\") as LstString)")
                                //.SetPropertyExpression("UserProfileDetails.Dynamic", "500")
                                //.SetPropertyExpression("UserProfileDetails.LstString", "{UserProfileDetails.LstStringDynamic}")
                                .SetPropertyExpression("UserProfileDetails.LstString", "List.OfString(\"Abhik\")")
                                //.SetPropertyExpression("UserProfileDetails.LstString", "{a.UserProfileDetails.LstString}.Where(x=>!x.Equals({a.FirstName}) And !x.Equals(\"Anaya\")).ToList()")
                                //.SetPropertyExpression("UserProfileDetails.LstString", "{UserProfileDetails.LstString}.Where(!Equals({a.FirstName}) And !Equals(\"Anaya\")).ToList()")
                                .SetPropertyExpression("UserProfileDetails.LstString", "List.Append({UserProfileDetails.LstString},\"Samik\", \"Saji\")")
                                //.SetPropertyExpression("UserProfileDetails.LstString", "{UserProfileDetails.LstStringDynamic}.Append(\"Samik\", \"Saji\")")
                                //.SetPropertyExpression("UserProfileDetails.LstString", " {UserProfileDetails.LstString}.AddCheck(\"Abhik\")")

                                .Attach()

                                /*.WithRule("{UserProfileDetails.Dynamic} < 0")
                                 * .SetPropertyExpression<string>("UserProfileDetails.DynamicStatus", "\"Dynamic is less than 0 \"")
                                 *
                                 * .Attach()
                                 * .WithDefaultRule()
                                 * .SetProperty("UserProfileDetails.DynamicStatus", "Dynamic is equal to 0")*/
                                .Compile();



            foreach (var item in lst)
            {
                mathruleset.Execute(item);
                itruleset.Execute(item);
                healthruleset.Execute(item);
                dynamicChild.Execute(item);
                // dynamicChild1.Execute(item);
            }

            foreach (var item in lst)
            {
                Console.WriteLine("Bonus of Employee: "
                                  + item.FirstName
                                  + " is "
                                  + item.DoubleProp("Bonus")
                                  + " of age: "
                                  + item.Age
                                  + " leaveBalance: "
                                  + item.UserProfileDetails.LeaveBalance);

                Console.WriteLine("Overtime of Employee: "
                                  + item.FirstName
                                  + " Has overtime "
                                  + item.Overtime);

                Console.WriteLine("##########################################################################################");
                Console.WriteLine(item.UserProfileDetails.IntProp("Dynamic"));
                Console.WriteLine(item.UserProfileDetails.StringProp("DynamicStatus"));
                Console.WriteLine(item.UserProfileDetails.EnumerableStringProp("LstStringDynamic").Contains("Pamli"));
                Console.WriteLine("##########################################################################################");
            }
            var bonusRuleSet = RulesetBuilder <UserProfile>
                               .Create(conetxt)
                               .WithName("Bonus Rule")
                               .WithPlaceHolder("Tomorrow", "DateTime.Now.AddDays(5)")
                               .WithPlaceHolder("DayAfter", "DateTime.Now.AddDays(2)")
                               .WithPlaceHolder("Bonus5075", "{Bonus} >50 and {Bonus}<=75")
                               .WithRule("{Bonus} < 50")
                               .SetPropertyExpression("BonusPayOutDate", "{Tomorrow}")
                               .SetPropertyExpression("BonusPayOutDate", "{BonusPayOutDate}.AddDays(1)")
                               .SetPropertyExpression("BonusPayOutDate", "DateTime.Parse(\"12 / 1 / 2012\")")
                               .SetPropertyExpression("BonusPayOutDate", "\"2012-12-02\"")
                               .SetPropertyExpression("BonusPayOutDate", "DateTime(2007, 1, 1)")
                               .Attach()
                               .WithRule("{Bonus5075}")
                               .SetPropertyExpression("BonusPayOutDate", "{DayAfter}")
                               .Attach()
                               .WithRule("{Bonus} >75 and {Bonus}<=100")
                               .SetProperty("BonusPayOutDate", DateTime.Now.AddDays(3))
                               .Attach()
                               .WithDefaultRule()
                               .SetPropertyExpression("BonusPayOutDate", "DateTime.Now.AddDays(1)")
                               .SetPropertyExpression("BonusPayOutDate", "{BonusPayOutDate}.AddDays(1)")
                               .SetPropertyExpression("BonusPayOutDate", "DateTime.Parse(\"12 / 1 / 2012\")")
                               .SetPropertyExpression("BonusPayOutDate", "\"2012-12-02\"")

                               .Compile();



            foreach (var item in lst)
            {
                bonusRuleSet.Execute(item);

                Console.WriteLine("Bonus of Employee: "
                                  + item.FirstName
                                  + " is "
                                  + item.DoubleProp("Bonus")
                                  + " and the payout date: "
                                  + item.DateTimeProp("BonusPayOutDate"));
            }

            TestGrouping(lst, conetxt);
            //TestDiagnostic(lst);
        }
Ejemplo n.º 4
0
        public static void RuleEngineTest1()
        {
            var conetxt = RuleEngineContextHolder.GenerateContext(ruleEngineId, "Employee");

            RuleAwareEntity.CreateDynamicPropertyForType <Employee, double>("Employee", "Bonus");
            RuleAwareEntity.CreateDynamicPropertyForType <Employee, string>("Employee", "BonusCategory");
            List <Employee> employees = new List <Employee>();

            Employee empl = null;

            empl = new Employee();
            empl.EmployeeNumber = 253055;
            empl.FirstName      = "Joseph";
            empl.LastName       = "Denison";
            empl.HourlySalary   = 12.85;
            empl.Age            = 40;
            //empl.CreateDynamicProperty<double>("Bonus");
            //empl.CreateDynamicProperty<string>("BonusCategory");
            employees.Add(empl);

            Employee empl1 = new Employee();

            empl1.EmployeeNumber = 204085;
            empl1.FirstName      = "Raymond";
            empl1.LastName       = "Ramirez";
            empl1.HourlySalary   = 9.95;
            empl1.Age            = 36;
            empl1.DefaultBonus   = 5000;
            //empl1.CreateDynamicProperty<double>("Bonus");
            //empl1.CreateDynamicProperty<string>("BonusCategory");
            employees.Add(empl1);

            Employee empl2 = new Employee();

            empl2.EmployeeNumber = 970044;
            empl2.FirstName      = "Christian";
            empl2.LastName       = "Riley";
            empl2.HourlySalary   = 14.25;
            empl2.Age            = 30;
            //empl2.CreateDynamicProperty<double>("Bonus");
            //empl2.CreateDynamicProperty<string>("BonusCategory");
            employees.Add(empl2);

            var ruleset = RulesetBuilder <Employee>
                          .Create(conetxt)
                          .WithName("Employee Bounus PropertyRule")
                          .WithEntryCriteria("EmployeeNumber > 500")
                          .WithRule("HourlySalary>13 And Age>25")
                          .SetPropertyExpression("Bonus", "100.00")
                          .SetPropertyExpression("Bonus", "DoubleProp(\"Bonus\") + 100.00")
                          .SetProperty("BonusCategory", "5 Star")
                          .SetPropertyExpression("Age", "int(Prop(\"Age\"))*2")

                          .Attach()
                          .WithRule("HourlySalary>10 And Age>25")
                          .SetProperty("Bonus", 80.00)
                          .SetProperty("BonusCategory", "4 Star")
                          .SetPropertyExpression("Age", "int(Prop(\"Age\"))*4")
                          .Attach()
                          .WithRule("HourlySalary>8 And Age>25")
                          .SetProperty("Bonus", 60.00)
                          .SetProperty("BonusCategory", "3 Star")
                          .SetPropertyExpression("Age", "int(Prop(\"Age\"))*3")
                          .SetPropertyExpression("Bonus", "DoubleProp(\"DefaultBonus\")/2")
                          .Attach()
                          .Compile();

            foreach (var item in employees)
            {
                ruleset.Execute(item);
            }

            if (ruleset.HasSuccessRule())
            {
                foreach (var item in employees)
                {
                    Console.WriteLine("Bonus of Employee: "
                                      + item.FirstName
                                      + " is "
                                      + item.DoubleProp("Bonus")
                                      + " and his Bonus Category is: "
                                      + item.StringProp("BonusCategory")
                                      + " of age: "
                                      + item.Age);
                }
            }
        }