Example #1
0
        public void CompileMRuleJoin()
        {
            var mrulejoin = new MRuleJoin
            {
                Name     = "Join rule",
                Children = new List <MRule>
                {
                    new MRuleDef
                    {
                        Name       = "Child rule 1",
                        Condition  = "Vars.Age.Max() < 55",
                        Projection = new Projection {
                            Value = "Math.Min(Vars.Amount, 1000)", ProjectionType = ProjectionType.Amount
                        },
                    },
                    new MRuleDef
                    {
                        Name       = "Child rule 2",
                        Condition  = "Vars.Age.Max() < 35",
                        Projection = new Projection {
                            Value = "Math.Min(Vars.Amount, 2000)", ProjectionType = ProjectionType.Amount
                        },
                    }
                }
            };

            var compiler = new AstCompiler();

            mrulejoin.Accept(compiler);

            var code = compiler.ToString();

            output.WriteLine(code);
            Assert.NotNull(code);
        }
        public void TestCompileAst()
        {
            var sampleProcessAst = new MRuleJoin
            {
                Name     = "Join rule",
                Children = new List <MRule>
                {
                    new MRuleDef
                    {
                        Name       = "Child rule 1",
                        Condition  = "Vars.Age.Max() < 55",
                        Projection = new Projection {
                            Value = "Math.Min(Vars.Amount, 1000)", ProjectionType = ProjectionType.Amount
                        },
                    },
                    new MRuleDef
                    {
                        Name       = "Child rule 2",
                        Condition  = "Vars.Age.Max() < 35",
                        Projection = new Projection {
                            Value = "Math.Min(Vars.Amount, 500)", ProjectionType = ProjectionType.Amount
                        },
                    }
                }
            };

            var process = RuleToCSharpCompiler.CreateFromAst(
                sampleProcessAst,
                new string [] { "ce_toy_fx.tests.Data", "ce_toy_fx.tests.Data.VariableTypes" },
                new Type[] { typeof(Variables) }
                );

            Assert.NotNull(process);

            Assert.Equal(1, process.Keys.Count);
            Assert.Equal("Age", process.Keys[0]);

            var applicants = new List <Applicant>
            {
                new Applicant
                {
                    Id          = "a1",
                    KeyValueMap = new Dictionary <string, object>
                    {
                        { "Age", 25 }
                    }.ToImmutableDictionary()
                },
                new Applicant
                {
                    Id          = "a2",
                    KeyValueMap = new Dictionary <string, object>
                    {
                        { "Age", 45 }
                    }.ToImmutableDictionary()
                }
            };

            var evalResult = process.RuleExpr(new RuleExprContext <Unit>
            {
                Log        = ImmutableList <LogEntry> .Empty,
                Amount     = 1500,
                Applicants = applicants.ToDictionary(x => x.Id).ToImmutableDictionary()
            });

            Assert.True(evalResult.Item1.isSome);
            Assert.Equal(1000, evalResult.Item2.Amount);
        }