2017-04-16 64 views
0

的順序,我目前正在學習的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 

現在我的問題是:爲什麼是比「過期優惠券」的規則後解僱了「執行優惠券」的規則。據我所知,規則的順序是非確定性的,所以我在考慮在執行其他兩條規則之前可以觸發「執行優惠券」規則

回答

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