簡體   English   中英

在特定時間或事實執行Drools規則

[英]Execute Drools rule at certain time or fact

我想在早上8點或太陽升起時開火。 怎么去這個?

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

計時器就像一個先決條件。 所以我想在這種情況下這沒用。

額外的時間事實必須每秒更新一次,這將導致規則每秒重新啟動。 我想我可以把時間事實分成小時分鍾第二個事實,但這並不能真正解決問題,只會讓它不那么經常發生。

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

你可以有一個不斷追蹤時間的規則。 然后你的規則可以檢查那個時間來開火。 為簡單起見,我使用毫秒,但我認為你可以看到它如何適應你想要的任何東西。 要解決每秒問題的觸發問題,請調整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();
     }

}

所以我這樣做就是把時間作為額外的觸發器:

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM