【问题标题】:What makes the order of this ruleset是什么决定了这个规则集的顺序
【发布时间】:2017-04-16 08:03:53
【问题描述】:

我目前正在学习 Drools 并阅读本书Mastering JBoss Drools 6

在第 4 章的开头给出了一个例子来说明 delete 关键字的使用。就是这个例子:

rule "Init current date" 
when 
then 
   insert(new Date()); 
end 

rule "Expire coupons" 
when 
   $now: Date() 
   $cp: Coupon(validUntil before $now) 
then 
   delete($cp); 
end 

rule "Execute coupon" 
when 
   $o: Order() 
   $cp: Coupon(order == $o) 
then 
   System.out.println(" We have a coupon for this order!"); 
end

现在我的问题是:为什么“执行优惠券”规则比“优惠券过期”规则触发得晚。据我所知,规则的顺序是不确定的,所以我认为“执行优惠券”规则可以在其他两个规则之前触发

【问题讨论】:

    标签: jboss drools


    【解决方案1】:

    你是对的。根据我的经验,我什至会在“执行优惠券”先触发上下注,因为通常会先触发后面的规则。

    显然,这个例子必须被纠正,要么由

    rule "Execute coupon" 
    when
        $now: Date() 
        $o: Order() 
        $cp: Coupon(order == $o, validUntil after $now ) 
    then 
        System.out.println(" We have a coupon for this order!"); 
    end
    

    或通过使用显着性(如果可能,应尽量避免)。

    但是(我没有书)我也可以想象规则集可能按给定工作的场景:

    session.insert( new Date() );
    session.insert( coupon );
    session.fireAllRules();
    
    session.insert( order );
    session.fireAllRules();
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      相关资源
      最近更新 更多