Drools窗口长度和行为

问题描述 投票:0回答:1

我使用以下规则来发出警报,以防先发送了一个绿色数据包,然后发送了一个红色数据包

package com.myspace.floodmonitoringrules;

import java.lang.Number;
import java.util.ArrayList;

rule "rule3"
    dialect "mvel"
    enabled true
    when
        w : ArrayList( size() == 1 ) from collect ( devicestatus( flood_status_color == "green" ) over window:length (1)) 
        ds : devicestatus( flood_status_color == "red" )
    then
        ds.setFlood_alert( "WindowDetected" );
end

但是如果之前没有发送绿色数据包,则在发送任何红色数据包后将触发该规则。这是我将ksession定义为有状态的时候。并且当我将其定义为无状态时,该规则根本不会被解雇!

我需要了解这种行为,因为我是流口水的新手。

window drools rules behavior collect
1个回答
0
投票

两者都给出相同的结果

declare Status @role (event) end

rule "rule3"
    dialect "mvel"
    enabled true
    when
        $g : Status( floodStatusColor == "green" ) over window:length (1)
        $r : Status( floodStatusColor == "red", this after $g ) over window:length (1)
    then
        System.out.println("WindowDetected")
        delete($g)
end

rule "rule3"
    dialect "mvel"
    enabled true
    when
        $list : List() from collect (Status() over window:length (2))
        $g : Status( floodStatusColor == "green" ) from $list
        $r : Status( floodStatusColor == "red", this after $g ) from $list 
    then
        System.out.println("WindowDetected")
end

测试

@DroolsSession("test.drl")
public class PlaygroundTest {

    @Rule
    public DroolsAssert drools = new DroolsAssert();

    @Test
    public void testIt() {
        drools.insertAndFire(new Status("red"));
        drools.advanceTime(1, SECONDS);
        drools.insertAndFire(new Status("green"));
        drools.advanceTime(1, SECONDS);
        drools.insertAndFire(new Status("red"));
        drools.advanceTime(1, SECONDS);
        drools.insertAndFire(new Status("red"));
        drools.advanceTime(1, SECONDS);
        drools.insertAndFire(new Status("red"));
        drools.advanceTime(1, SECONDS);
        drools.insertAndFire(new Status("green"));
        drools.advanceTime(1, SECONDS);
        drools.insertAndFire(new Status("red"));
    }

输出

00:00:00 --> inserted: Status[floodStatusColor=red]
00:00:00 --> fireAllRules
00:00:01 --> inserted: Status[floodStatusColor=green]
00:00:01 --> fireAllRules
00:00:02 --> inserted: Status[floodStatusColor=red]
00:00:02 --> fireAllRules
00:00:02 <-- 'rule3' has been activated by the tuple [InitialFactImpl, Status, Status]
WindowDetected
00:00:02 --> retracted: Status[floodStatusColor=green]
00:00:03 --> inserted: Status[floodStatusColor=red]
00:00:03 --> fireAllRules
00:00:04 --> inserted: Status[floodStatusColor=red]
00:00:04 --> fireAllRules
00:00:05 --> inserted: Status[floodStatusColor=green]
00:00:05 --> fireAllRules
00:00:06 --> inserted: Status[floodStatusColor=red]
00:00:06 --> fireAllRules
00:00:06 <-- 'rule3' has been activated by the tuple [InitialFactImpl, Status, Status]
WindowDetected
00:00:06 --> retracted: Status[floodStatusColor=green]

您可能想看看similar question,它更加复杂和有趣,您可以在没有计时窗口的情况下解决类似的问题。

© www.soinside.com 2019 - 2024. All rights reserved.