在 NRULES 中实现前向链接的最佳方式

Kis*_*ore 1 nrules

我想根据前一个规则的结果运行一个规则。如何使用前向链接实现此功能?我不想为每个规则创建不同的类对象来实现前向链接。

在此示例中,仅为这一规则创建了一个 InstantDiscount 对象以实现前向链接。

public class PreferredCustomerDiscountRule : Rule
{
    public override void Define()
    {
        Customer customer = null;
        IEnumerable<Order> orders = null;
        Double total = Double.NaN;

        When()
            .Match<Customer>(() => customer, c => c.IsPreferred)
            .Query(() => orders, x => x
                .Match<Order>(
                    o => o.Customer == customer,
                    o => o.IsOpen)
                .Collect())
            .Let(() => total, () => orders.Sum(x => x.Amount))
            .Having(() => total > 1000);

        Then()
            .Yield(_ => new InstantDiscount(customer, total * 0.05));
    }
}

public class PrintInstantDiscountRule : Rule
{
    public override void Define()
    {
        InstantDiscount discount = null;

        When()
            .Match(() => discount);

        Then()
            .Do(_ => Console.WriteLine("Customer {0} has instant discount of {1}", 
                discount.Customer.Name, discount.Amount));
    }
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*yev 5

前向链接是一个规则更改规则引擎的工作内存以激活某些其他规则的过程。这可以通过将新事实插入规则引擎(在NRules中使用Yield或IContext.Insert)或通过更改一些现有事实(使用IContext.Update)来实现。

以下是原始示例,经过重新编写以将折扣附加到客户事实,然后更新该事实以实现前向链接。

public class PreferredCustomerDiscountRule : Rule
{
    public override void Define()
    {
        Customer customer = null;
        IEnumerable<Order> orders = null;
        Double total = Double.NaN;

        When()
            .Match<Customer>(() => customer, c => c.IsPreferred, c => !c.DiscountPercent.HasValue)
            .Query(() => orders, x => x
                .Match<Order>(
                    o => o.Customer == customer,
                    o => o.IsOpen)
                .Collect())
            .Let(() => total, () => orders.Sum(x => x.Amount))
            .Having(() => total > 1000);

        Then()
            .Do(ctx => ApplyDiscount(customer, 0.05))
            .Do(ctx => ctx.Update(customer));
    }

    private static void ApplyDiscount(Customer customer, double discount)
    {
        customer.DiscountPercent = discount;
    }
}

public class DicsountNotificationRule : Rule
{
    public override void Define()
    {
        Customer customer = null;

        When()
            .Match(() => customer, c => c.DiscountPercent.HasValue);

        Then()
            .Do(_ => Console.WriteLine("Customer {0} has instant discount of {1}%", 
                customer.Name, customer.DiscountPercent));
    }
}
Run Code Online (Sandbox Code Playgroud)

当通过更新现有事实进行前向链接时,必须注意不要重新激活更新事实的规则,以避免不需要的递归。NRules 中有几种控制递归的机制:

  • 以这样的方式编写条件,使更新使规则的条件无效(这就是我们在上面的示例中所做的;一旦设置了折扣,规则将不再匹配)
  • 在规则上使用重复性属性以防止重新触发
  • 使用议程过滤器仅在匹配事实发生某些更改时激活规则。

NRules 文档中描述了后两个选项。