How to use Rule# to author NRules that consume multiple class objects

168 views Asked by At

I wish to use Rule# to author rules in my application using the NRules engine. Since I would like users to author their rules at runtime, I wish to know if it is possible to do something like this

When()
            .Match<Customer>(() => customer)
            .Exists<Order>(o => o.Customer == customer, o => o.PercentDiscount > 0.0);
Then()
            .Do(_ => customer.NotifyAboutDiscount());

using Rule# ?

The example here https://github.com/NRules/NRules.Language shows me how to author a rule or rules with a single object. However, I would like to use different class objects in one rule as in the example above.

I can't find a more detailed documentation on Rule# ...

1

There are 1 answers

1
Sergiy Nikolayev On BEST ANSWER

It's important to note that Rule# (aka NRules.Language) is not feature complete, compared to the internal rules DSL. The best place to see all examples of what's currently supported in Rule# is the integration tests: https://github.com/NRules/NRules.Language/blob/develop/src/NRules.RuleSharp/NRules.RuleSharp.IntegrationTests/RuleCompilerTests.cs

Specifically, for your example, assuming your domain model contains Customer and Order types with the corresponding properties and methods, the rule will look like this:

rule "Notify About Discount"
when
    var customer = Customer();
    exists Order(o => o.Customer == customer, o => o.PercentDiscount > 0.0);
    
then
    customer.NotifyAboutDiscount();