Drools Fusion滑动时间窗口,用于以下条件

时间:2015-02-17 05:31:24

标签: window drools sliding drools-fusion

通过Drools融合,我想为计算机的cpu_idle做一些警报。条件是:

  1. 我会收到我每10秒监控的机器的记录;

  2. 如果cpu_idle< 10,drools应该打开一个10mim的时间窗口,并开始计算条件cpu_idle< 10发生的次数, 变量是happen_times。

  3. 10分钟后,流口水检查发生的时间。如果发生时间> 5,那么做一些警告否则什么都不做。
  4. 我不知道该怎么做。我使用drools 6.1。下面是主要代码:

    public static void main(String[] args) {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add( ResourceFactory.newByteArrayResource(str.getBytes()),
                      ResourceType.DRL );
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    
        EntryPoint atmStream = ksession.getEntryPoint("ATM Stream" );
    
         ksession.fireAllRules();
    }
    

    我有一个记录bean,如:

    public class Record {
    
        private double cpu_idle;
        private Timestamp collecte_time;
        public double getCpu_idle() {
            return cpu_idle;
        }
        public void setCpu_idle(double cpu_idle) {
            this.cpu_idle = cpu_idle;
        }
        public Timestamp getCollecte_time() {
            return collecte_time;
        }
        public void setCollecte_time(Timestamp collecte_time) {
            this.collecte_time = collecte_time;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

你需要一个

class Window {
    int count = 1;
    long time;
    Window( long time ){...}
    // getters and setters
}

以及以下规则(均未经测试):

rule "nothing to do"
when
    $r: Record( cpu_idle >= 10 )
    not Window()
then
    retract( $r );
end

rule "open new window"
when
    $r: Record( cpu_idle < 10, $ts: collecte_time )
    not Window()
then
    insert( new Window( $ts.getTime() ) );
    retract( $r );
end

rule "keep window"
when
    $r: Record( cpu_idle >= 10, $ts: collecte_time )
    $w: Window( $ns: time )
    eval( ($ts.getTime() - $ns)/1000 <= 10*60)
then
    retract( $r );
    modify( $w ){ setCount( $w.getCount() + 1 ) }
end

rule "drop window"
when
    $r: Record( cpu_idle >= 10, $ts: collecte_time )
    $w: Window( $ns: time )
    eval( ($ts.getTime() - $ns)/1000 > 10*60)
then
    retract( $r );
    retract( $w );
end

rule "alarm"
when
    $w: Window( count >= 5 )
then
    // raise alarm
    retract( $w );
end

我在Record中使用Date而不是lava.sql.Timestamp。