最近n个事件,匹配某种模式
Latest n events, matching a certain pattern
Drools 有内置功能,选择最近的 n 个事件,匹配特定模式吗?我在文档中阅读了有关滑动长度 windows 的内容,股票报价示例似乎正是我想要的:
"For instance, if the user wants to consider only the last 10 RHT Stock Ticks, independent of how old they are, the pattern would look like this:"
StockTick( company == "RHT" ) over window:length( 10 )
在测试示例时,在我看来它更像是一个
StockTick( company == "RHT" ) from StockTick() over window:length( 10 )
选择最新的 10 个 StockTick 事件,然后按公司过滤它们 == "RTH",导致 0 到 10 个 RHT-Ticks,事件虽然流包含超过 10 个 RTH-事件。
解决方法如下:
$tick : StockTick( company == "RHT" )
accumulate(
$other : StockTick(this after $tick, company == "RHT" );
$cnt : count(other);
$cnt < 10)
性能和可读性差。
很可能您看到的是初始阶段,其中 window 中的事件计数根据约束尚未达到 window:length
中指定的长度。例如,
rule "Your First Rule"
when
accumulate( $st : Applicant($age: age > 5) over window:length(10)
from entry-point X,
$avg: average ( $age ), $cnt: count( $st ))
then
System.out.println("~~~~~avg~~~~~");
System.out.println($avg + ", count=" + $cnt);
System.out.println("~~~~~avg~~~~~");
end
甚至在有 10 个匹配的申请人之前显示输出,但后来,$cnt
永远不会低于 10,即使 $age
周期性地从 0 到 9。
如果您确实认为找到了支持您声明的示例,请提供完整的复制代码并确保注明 Drools 版本。
您的解决方法确实非常糟糕,因为它会为每个 StockTick 累积。但是 window:length(n)
可以通过使用维护 n
事件列表的辅助事实来非常有效地实现。这甚至可能比window:length
.
更有优势
Drools 有内置功能,选择最近的 n 个事件,匹配特定模式吗?我在文档中阅读了有关滑动长度 windows 的内容,股票报价示例似乎正是我想要的: "For instance, if the user wants to consider only the last 10 RHT Stock Ticks, independent of how old they are, the pattern would look like this:"
StockTick( company == "RHT" ) over window:length( 10 )
在测试示例时,在我看来它更像是一个
StockTick( company == "RHT" ) from StockTick() over window:length( 10 )
选择最新的 10 个 StockTick 事件,然后按公司过滤它们 == "RTH",导致 0 到 10 个 RHT-Ticks,事件虽然流包含超过 10 个 RTH-事件。
解决方法如下:
$tick : StockTick( company == "RHT" )
accumulate(
$other : StockTick(this after $tick, company == "RHT" );
$cnt : count(other);
$cnt < 10)
性能和可读性差。
很可能您看到的是初始阶段,其中 window 中的事件计数根据约束尚未达到 window:length
中指定的长度。例如,
rule "Your First Rule"
when
accumulate( $st : Applicant($age: age > 5) over window:length(10)
from entry-point X,
$avg: average ( $age ), $cnt: count( $st ))
then
System.out.println("~~~~~avg~~~~~");
System.out.println($avg + ", count=" + $cnt);
System.out.println("~~~~~avg~~~~~");
end
甚至在有 10 个匹配的申请人之前显示输出,但后来,$cnt
永远不会低于 10,即使 $age
周期性地从 0 到 9。
如果您确实认为找到了支持您声明的示例,请提供完整的复制代码并确保注明 Drools 版本。
您的解决方法确实非常糟糕,因为它会为每个 StockTick 累积。但是 window:length(n)
可以通过使用维护 n
事件列表的辅助事实来非常有效地实现。这甚至可能比window:length
.