在特定时间或事实执行Drools规则 [英] Execute Drools rule at certain time or fact

查看:152
本文介绍了在特定时间或事实执行Drools规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在早上8点或太阳升起时开火。

I'd like a rule to fire when it's 8am OR the sun is up. How to go about this?

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

计时器的作用类似于先决条件。因此,我猜想在这种情况下它没有用。

A timer acts like a prerequisite. So I guess it's not useful in this case.

额外的时间事实必须每秒进行更新,这将导致规则每秒射击一次。我想我可以将 time 事实分解为 hour minute second 事实,但是不会确实可以解决问题,但只会使其减少发生。

An extra time fact would have to be updated every second, which would cause the rule to refire every second. I guess I could split the time fact into hour, minute, and second facts, but that wouldn't really solve the problem, but only make it occur less often.

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

Is such a rule possible/viable in Drools?

推荐答案

您可能有一条不断跟踪时间的规则。然后,您的规则就可以检查触发时间了。为了简化起见,我使用毫秒为单位,但是我认为您可以看到如何将其调整为所需的内容。要解决第二个问题,请修改Time类以使用Calendar对象或类似的东西。只需使用Time对象初始化您的知识会话。

You could have a rule that is constantly keeping track of the time. Then your rule can just check for that time to fire. I used milliseconds for simplicity sake, but I think you can see how it can be adapted to whatever you want. To solve your firing issue every second issue, adapt the Time class to use Calendar objects or something along those lines. Just initialize your knowledge session with a Time object.

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

}

这篇关于在特定时间或事实执行Drools规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆