More examples using Rule Builder

1,277 views
Skip to first unread message

jose....@gmail.com

unread,
Jul 17, 2015, 2:28:18 PM7/17/15
to nrules...@googlegroups.com
I was wondering if you have ANY examples whatsoever of Grouping Patterns utilizing the Rule Builder (building rules dynamically) similar to the ones displayed here: https://github.com/NRules/NRules/wiki/Fluent-Rules-DSL .
I have been playing around with your example: https://github.com/NRules/NRules/wiki/Rule-Builder, and also the one included in the source code, but cannot extrapolate the concepts well.

I am specifically trying to figure out how to control the flow of conditions within a rule ('Or' instead of 'And', etc), and also how to fire distinct actions depending on the output from the condition.

Any help/direction would be appreciated.  

Thank you!


Sergiy Nikolayev

unread,
Jul 19, 2015, 7:53:22 AM7/19/15
to nrules...@googlegroups.com
I'm currently traveling, so I'll post an example when I get back next week.
For now, you can look at the source code behind fluent rules, since they are internally using RuleBuilder. https://github.com/NRules/NRules/blob/develop/src/NRules/NRules.Fluent/Expressions/LeftHandSideExpression.cs

jose....@gmail.com

unread,
Jul 20, 2015, 12:01:14 PM7/20/15
to nrules...@googlegroups.com
Thank you for the link! This will help me get a grasp on it better.  Thank you!

Sergiy Nikolayev

unread,
Jul 27, 2015, 10:15:34 PM7/27/15
to NRules Users, jose....@gmail.com
Here is a modified example from the RuleBuilder sample, that uses an OR group on the left-hand side of the rule:

//Create rule builder
var builder = new RuleModel.Builders.RuleBuilder();
builder
.Name("TestRule");

//Customer pattern
PatternBuilder customerPattern = builder.LeftHandSide().Pattern(typeof (Customer), "customer");
Expression<Func<Customer, bool>> customerCondition =
    customer
=> customer.Name == "John Do";
customerPattern
.Condition(customerCondition);


//OR group
var orGroup = builder.LeftHandSide().Group(GroupType.Or);

//Order pattern 1
PatternBuilder orderPattern1 = orGroup.Pattern(typeof (Order), "order");
Expression<Func<Order, Customer, bool>> orderCondition11 =
   
(order, customer) => order.Customer == customer;
Expression<Func<Order, bool>> orderCondition12 =
    order
=> order.Amount > 100.00m;
orderPattern1
.Condition(orderCondition11);
orderPattern1
.Condition(orderCondition12);

//Order pattern 2
PatternBuilder orderPattern2 = orGroup.Pattern(typeof (Order), "order");
Expression<Func<Order, Customer, bool>> orderCondition21 =
   
(order, customer) => order.Customer == customer;
Expression<Func<Order, bool>> orderCondition22 =
    order
=> order.Amount < 10.00m;
orderPattern2
.Condition(orderCondition21);
orderPattern2
.Condition(orderCondition22);

//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);

The idea is that when you want to group conditions using OR group, you first need to create a group, and then add patterns to it.

Benjamin Pullicino

unread,
Oct 16, 2015, 3:39:51 AM10/16/15
to NRules Users, jose....@gmail.com
I am trying to create a more complex dynamic rule

(Condition1 OR Condition2) AND Condition3

Would it be possible to post an example on how to do this?

fredzy

unread,
Dec 29, 2015, 12:28:20 PM12/29/15
to NRules Users
@Sergiy Nikolayev
I want to create a UI where user can define a complex rules. The idea is to add a grammar for validation of the rule syntax that the users will define on the UI and then pass this rule to the RuleBuilder some how which will execute it on the go.
Is it possible with RuleBuilder? Any examples would be highly appreciated.
Thanking in anticipation.

Sergiy Nikolayev

unread,
Jun 24, 2018, 3:20:03 PM6/24/18
to NRules Users
While this is a very late response, maybe others are also interested in this. NRules.Language is doing exactly what's described in the question - custom grammar is defined using ANTLR, which is then parsed and converted to NRules rule via RuleBuilder. The rules using this language can be written in some sort of custom UI, stored in a DB or a text file, and then later fed to the parser to convert to the NRules structure and then compiled to the executable model.

Seva

unread,
May 27, 2019, 8:49:35 AM5/27/19
to NRules Users
Hi,

Can you post an example of a complex rule achieved with RuleBuilder Please?

For example:

Condition1 AND Condition2 AND (Condition3 OR Condition4)

or

(Condition1 OR Condition2) AND Condition3

Seems like i have don't fully understand how patterns works

Thanks


Reply all
Reply to author
Forward
0 new messages