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(); }
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()); }
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()); }
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()); }