【问题标题】:Execute Drools rule at certain time or fact在特定时间或事实执行 ​​Drools 规则
【发布时间】:2013-05-30 20:59:01
【问题描述】:

我希望在早上 8 点或太阳升起时触发规则。这个怎么办?

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

计时器就像一个先决条件。所以我想在这种情况下它没有用。

一个额外的 time 事实必须每秒更新一次,这将导致规则每秒重新触发。我想我可以将 time 事实拆分为 hourminutesecond 事实,但这不会真正解决问题,但只是减少它发生的频率。

这样的规则在 Drools 中是否可能/可行?

【问题讨论】:

    标签: java time drools rule


    【解决方案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();
         }
    
    }
    

    【讨论】:

    • 很抱歉 3 年后才来这里,但这不是超级昂贵,因为它一直在循环并且永远不会停止吗?我实际上需要实现这样的东西,但我真的想知道这是否是正确的方法。
    【解决方案2】:

    所以我这样做是为了有时间作为额外的触发器:

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

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      相关资源
      最近更新 更多