我能够像这样简化“非逻辑”
rule "Three most recent events tagged with 'OK'"
when
$e1 : Event( tag == "OK")
$e2 : Event( tag == "OK", millis < $e1.millis )
$e3 : Event( tag == "OK", millis < $e2.millis )
not Event( this != $e2, tag == "OK", $e3.millis < millis, millis < $e1.millis )
then
System.out.printf("%s - %s - %s%n", $e1, $e2, $e3);
end
没有什么关于清洁活动的说法。通常这是可取的,因此您可以通过删除最后一个事件来实现相同的逻辑:
rule "Three most recent events tagged with 'OK'"
when
$e1 : Event( tag == "OK")
$e2 : Event( tag == "OK", millis < $e1.millis )
$e3 : Event( tag == "OK", millis < $e2.millis )
then
System.out.printf("%s - %s - %s%n", $e1, $e2, $e3);
retract ($e3)
end
假设每秒你将插入一个事件一个“OK”另一个空“”,这是测试:
@DroolsSession("classpath:/test3.drl")
public class PlaygroundTest {
@Rule
public DroolsAssert drools = new DroolsAssert();
@Test
public void testIt() {
for (int i = 0; i < 10; i++) {
drools.advanceTime(1, SECONDS);
drools.insertAndFire(new Event(i % 2 == 0 ? "OK" : "", i));
}
}
}
所有三个变体都将产生相同的触发逻辑:
00:00:01 --> inserted: Event[tag=OK,millis=0]
00:00:01 --> fireAllRules
00:00:02 --> inserted: Event[tag=,millis=1]
00:00:02 --> fireAllRules
00:00:03 --> inserted: Event[tag=OK,millis=2]
00:00:03 --> fireAllRules
00:00:04 --> inserted: Event[tag=,millis=3]
00:00:04 --> fireAllRules
00:00:05 --> inserted: Event[tag=OK,millis=4]
00:00:05 --> fireAllRules
00:00:05 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [Event, Event, Event]
OK4 - OK2 - OK0
00:00:06 --> inserted: Event[tag=,millis=5]
00:00:06 --> fireAllRules
00:00:07 --> inserted: Event[tag=OK,millis=6]
00:00:07 --> fireAllRules
00:00:07 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [Event, Event, Event]
OK6 - OK4 - OK2
00:00:08 --> inserted: Event[tag=,millis=7]
00:00:08 --> fireAllRules
00:00:09 --> inserted: Event[tag=OK,millis=8]
00:00:09 --> fireAllRules
00:00:09 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [Event, Event, Event]
OK8 - OK6 - OK4
00:00:10 --> inserted: Event[tag=,millis=9]
00:00:10 --> fireAllRules
变体window:length(3)
也将处理最后 3 个 OK 事件。但一开始就不同:它也会为 1 和 2 的第一个 OK 事件触发。如果会话不包含任何事件,它也会在开始时以空列表触发一次。根据文档,滑动窗口立即开始匹配,定义滑动窗口并不意味着规则必须等待滑动窗口“满”才能匹配。例如,计算 window:length(10) 上事件属性平均值的规则将立即开始计算平均值,对于无事件它将从 0(零)开始,并在事件到达时更新平均值逐个。
00:00:01 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [InitialFactImpl, UnmodifiableRandomAccessList]
[]
00:00:01 --> inserted: Event[tag=OK,millis=0]
00:00:01 --> fireAllRules
00:00:01 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [InitialFactImpl, UnmodifiableRandomAccessList]
[OK0]
00:00:02 --> inserted: Event[tag=,millis=1]
00:00:02 --> fireAllRules
00:00:03 --> inserted: Event[tag=OK,millis=2]
00:00:03 --> fireAllRules
00:00:03 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [InitialFactImpl, UnmodifiableRandomAccessList]
[OK0, OK2]
00:00:04 --> inserted: Event[tag=,millis=3]
00:00:04 --> fireAllRules
00:00:05 --> inserted: Event[tag=OK,millis=4]
00:00:05 --> fireAllRules
00:00:05 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [InitialFactImpl, UnmodifiableRandomAccessList]
[OK0, OK2, OK4]
00:00:06 --> inserted: Event[tag=,millis=5]
00:00:06 --> fireAllRules
00:00:07 --> inserted: Event[tag=OK,millis=6]
00:00:07 --> fireAllRules
00:00:07 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [InitialFactImpl, UnmodifiableRandomAccessList]
[OK2, OK4, OK6]
00:00:08 --> inserted: Event[tag=,millis=7]
00:00:08 --> fireAllRules
00:00:09 --> inserted: Event[tag=OK,millis=8]
00:00:09 --> fireAllRules
00:00:09 <-- 'Three most recent events tagged with 'OK'' has been activated by the tuple [InitialFactImpl, UnmodifiableRandomAccessList]
[OK4, OK6, OK8]
00:00:10 --> inserted: Event[tag=,millis=9]
00:00:10 --> fireAllRules