Я смог упростить «не логику» вот так
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
Скажем, каждую секунду вы будете вставлять событие одно «ОК», другое «пусто», вот тест:
@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. Он также будет запущен один раз с пустым списком в начале, если сеанс не содержит никаких событий. Согласно документации скользящие окна начинают сопоставляться немедленно, и определение скользящего окна не означает, что правило должно ждать, пока скользящее окно заполнится, чтобы выполнить сопоставление. Например, правило, которое вычисляет среднее значение свойства события в окне: длина (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
person
Mike
schedule
11.03.2020