Beispiel #1
0
 protected Rule()
 {
     _builder = new RuleBuilder();
     _lhsExpression = new LeftHandSideExpression(_builder);
     _rhsExpression = new RightHandSideExpression(_builder);
     _definition = new Lazy<IRuleDefinition>(BuildDefinition);
 }
        private IRuleDefinition BuildRule()
        {
            //Create rule builder
            var builder = new NRules.RuleModel.Builders.RuleBuilder();
            builder.Name("TestRule");

            //Build conditions
            PatternBuilder customerPattern = builder.LeftHandSide().Pattern(typeof (Customer), "customer");
            //Can compose expressions at runtime
            ParameterExpression customerParameter = customerPattern.Declaration.ToParameterExpression();
            var customerCondition = Expression.Lambda(
                Expression.Equal(
                    Expression.Property(customerParameter, "Name"),
                    Expression.Constant("John Do")),
                customerParameter
                );
            customerPattern.Condition(customerCondition);

            PatternBuilder orderPattern = builder.LeftHandSide().Pattern(typeof (Order), "order");
            //Can specify expression at compile time
            Expression<Func<Order, Customer, bool>> orderCondition1 = 
                (order, customer) => order.Customer == customer;
            Expression<Func<Order, bool>> orderCondition2 = 
                order => order.Amount > 100.00m;
            orderPattern.Condition(orderCondition1);
            orderPattern.Condition(orderCondition2);

            //Build actions
            Expression<Action<IContext, Customer, Order>> action = 
                (ctx, customer, order) => Console.WriteLine("Customer {0} has an order in amount of ${1}", customer.Name, order.Amount);
            builder.RightHandSide().Action(action);

            //Build rule model
            return builder.Build();
        }
Beispiel #3
0
        private IRuleDefinition ChildCOnessionEligibility()
        {
            var builder = new NRules.RuleModel.Builders.RuleBuilder();

            builder.Name("Child Concession Elgibility Rule");

            PatternBuilder      passengerPattern   = builder.LeftHandSide().Pattern(typeof(Passenger), "passenger");
            ParameterExpression passengerParameter = passengerPattern.Declaration.ToParameterExpression();
            var passengerCondition = Expression.Lambda(
                Expression.LessThan(
                    Expression.Property(passengerParameter, "Age"),
                    Expression.Constant(6)),
                passengerParameter);

            passengerPattern.Condition(passengerCondition);

            //Expression<Action<IContext, Passenger>> action =
            //    (ctx, customer, order) => Console.WriteLine("Customer {0} has an order in amount of ${1}", customer.Name, order.Amount);
            //Added
            Expression <Action <IContext, Passenger> > action =
                (ctx, passenger) => ctx.Insert($"Passenger : {passenger.Name} is eligible for child concession.");

            builder.RightHandSide().Action(action);

            return(builder.Build());
        }
Beispiel #4
0
        private IRuleDefinition ValidateSrCitizenFlag()
        {
            var builder = new NRules.RuleModel.Builders.RuleBuilder();

            builder.Name("Validate IsConsession flag Rule");

            PatternBuilder passengerPattern = builder.LeftHandSide().Pattern(typeof(Passenger), "passenger");
            Expression <Func <Passenger, bool> > passengerCondition = (passenger) => passenger.IsSeniorCitizen && passenger.Age < 60;

            passengerPattern.Condition(passengerCondition);

            Expression <Action <IContext, Passenger> > action =
                (ctx, passenger) => ctx.Insert($"Passenger : {passenger.Name}  is not eligible for senior citizen concession, since his age is under 60.");

            builder.RightHandSide().Action(action);

            return(builder.Build());
        }
        private IRuleDefinition BuildRule()
        {
            //Create rule builder
            var builder = new NRules.RuleModel.Builders.RuleBuilder();

            builder.Name("TestRule");

            //Build conditions
            PatternBuilder customerPattern = builder.LeftHandSide().Pattern(typeof(Customer), "customer");
            //Can compose expressions at runtime
            ParameterExpression customerParameter = customerPattern.Declaration.ToParameterExpression();
            var customerCondition = Expression.Lambda(
                Expression.Equal(
                    Expression.Property(customerParameter, "Name"),
                    Expression.Constant("John Do")),
                customerParameter
                );

            customerPattern.Condition(customerCondition);

            PatternBuilder orderPattern = builder.LeftHandSide().Pattern(typeof(Order), "order");
            //Can specify expression at compile time
            Expression <Func <Order, Customer, bool> > orderCondition1 =
                (order, customer) => order.Customer == customer;
            Expression <Func <Order, bool> > orderCondition2 =
                order => order.Amount > 100.00m;

            orderPattern.Condition(orderCondition1);
            orderPattern.Condition(orderCondition2);

            //Build actions
            Expression <Action <IContext, Customer, Order> > action =
                (ctx, customer, order) => Console.WriteLine("Customer {0} has an order in amount of ${1}", customer.Name, order.Amount);

            builder.RightHandSide().Action(action);

            //Build rule model
            return(builder.Build());
        }
 public RightHandSideExpression(RuleBuilder builder)
 {
     _builder = builder;
 }