public void TestCompileSource()
        {
            var program = GetSourceCode();
            var process = RuleToCSharpCompiler.CreateFromString(program, typeof(Variables));

            Assert.NotNull(process);
        }
Ejemplo n.º 2
0
        public IActionResult Evaluate([FromBody] EvaluateRequest request)
        {
            try
            {
                var jtoken  = request.Process;
                var json    = jtoken.ToString();
                var rootAst = JsonParser.ParseMRule(json);
                var process = RuleToCSharpCompiler.CreateFromAst(
                    rootAst,
                    new string[] { "ce_toy_fx.sample.web.Models", "ce_toy_fx.sample.web.Models.VariableTypes" },
                    new Type[] { typeof(Variables) });

                var applicants = request.Application.Applicants.Select(x => x.ToInternalModel()).ToList();

                /*
                 * var applicants = new List<Applicant>
                 *  {
                 *      new Applicant
                 *      {
                 *          Id = "a1",
                 *          KeyValueMap = new Dictionary<string,object>
                 *          {
                 *              { Variables.Age, 25 },
                 *              { Variables.Credit, 1000 },
                 *              { Variables.CreditA, 250 },
                 *              { Variables.CreditB, 250 },
                 *              { Variables.Salary, 100 },
                 *              { Variables.Flags, 1 },
                 *              { Variables.Deceased, false },
                 *              { Variables.Address, new Address { Street = "Street 1", PostalCode = "12345" } },
                 *              { Variables.Role, Roles.Primary },
                 *          }.ToImmutableDictionary()
                 *      },
                 *      new Applicant
                 *      {
                 *          Id = "a2",
                 *          KeyValueMap = new Dictionary<string,object>
                 *          {
                 *              { Variables.Age, 35 },
                 *              { Variables.Credit, 100 },
                 *              { Variables.CreditA, 25 },
                 *              { Variables.CreditB, 0 },
                 *              { Variables.Salary, 200 },
                 *              { Variables.Flags, 0 },
                 *              { Variables.Deceased, false },
                 *              { Variables.Address, new Address { Street = "Street 2", PostalCode = "58098" } },
                 *              { Variables.Role, Roles.Other },
                 *          }.ToImmutableDictionary()
                 *      }
                 *  };
                 */

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

                return(Json(evalResult.Item1.isSome ? ("Granted, " + evalResult.Item2.Amount) : "Rejected"));
            }
            catch (Exception e)
            {
                return(Json(e.Message));
            }
        }
        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);
        }