2013-05-30 56 views
0

我想要一條規則在上午8點或太陽升起時啓動。如何去做這件事?在某個時間或事實上執行Drools規則

rule X 
    when 
     ("it's 8am" and ... and ...) 
     or 
     (Sun(up) and ... and ...) 
    then 
     // do something 
end 

計時器的行爲像一個先決條件。所以我猜這在這種情況下沒有用。

額外時間事實上必須每秒更新一次,這會導致規則每秒都會重新刷新。我想我可以在時間實際上分成小時分鐘事實,但不會真正解決問題,而只是讓它發生較少。

在Drools中這樣的規則是可行的嗎?

回答

1

你可以有一個不斷追蹤時間的規則。那麼你的規則就可以檢查那段時間是否開火。爲了簡單起見,我使用了毫秒,但我認爲你可以看到它可以如何適應任何你想要的。爲了解決每發生第二個問題的問題,請調整Time類以使用Calendar對象或其他行。只需使用Time對象初始化知識會話即可。

rule "update time" 
    when 
     $time : Time(value != currentTime) 
    then 
     modify($time){ 
      setValue($time.getCurrentTime()); 
     }; 
end 

rule "X" 
    when 
     Time(value = //whatever time) 
    then 
    // do something 
end 


public class Time 
{ 
    long value; 

    public Time() 
    { 
      value = getCurrentTime(); 
    } 

    //getter and setter for value 

    public long getCurrentTime() 
    { 
      return System.currentTimeMilliSeconds(); 
    } 

} 
+2

對不起,3年後來到這裏,但這不會是超級昂貴,因爲它始終循環,永不停止?我實際上需要實現這樣的東西,但我真的想知道這是否是正確的方式來做到這一點。 –

1

所以這是我做的有時間作爲一個額外觸發:

1)創建單靠一個cron觸發器,它在上午8時插入一時間基於事實的一個單獨的規則。

2)對cron觸發的時間事實進行實際的規則檢查。

rule "08:00 AM" 
    timer(cron: 0 0 8 * * ?) 
    when // empty 
    then 
     insertLogical(new Time(31)); // 31 is a rule ID, see below 
end 

rule "X" 
    when 
     Time(ruleID == 31) 
     or 
     Sun(up) 
    then 
     // do something 
end 

insertLogical將事實插入到內存中,並在不再需要時將其刪除。這是事實:

public class Time { 
    private int ruleID; 

    public Time(int ruleID) { 
     this.ruleID = ruleID; 
    } 

    public getRuleID() { 
     return this.ruleID; 
    } 
}