问题描述
我有一个每个10秒钟的滑动窗框。事实不断进入工作存储器(STREAM模式)。我也有一个ID列表。我需要编写一条规则,该规则将在每个10秒的计时间隔中检查以下条件(使用计时器逻辑)
- 我的事实“ AlertGroupRuleModel”具有一个名为“ ruleId”的属性,它是一个正整数。
- AlertGroupRuleModel事实出现在最近10秒钟的滑动窗口中,它们一起满足预定义的ruleId列表的存在(列表ruleIds = Arrays.asList (1,2,3,4,5))。
- 我需要一种逻辑,例如在滑动窗口中累积所有事实,并检查我在预定义列表中的规则ID在滑动窗口中是否至少出现一次。
- 如果我的列表中的任何ID在特定的滑动窗口中不存在,那么我的规则就不会出现。
我需要在每个10s的时间间隔中进行10s滑动窗口事实检查。因此,Timer1将考虑所有最近10s时间范围内的事实。 Timer2将考虑下一个10s间隔之内的事实,依此类推。
我的事实类结构,
@Getter
@Setter
public class AlertGroupRuleModel {
private long ruleId;
private long tagId;
public AlertGroupRuleModel() {
}
}
将数据加载到队列以进行DRL处理
private static BlockingQueue<AlertGroupRuleModel> factQueue = new LinkedBlockingDeque<>(1000);
/* Loading facts to a queue : Contineously reading data from streams and loading facts to a queue*/
factQueue.add(new AlertGroupRuleModel(streamedTagId,streamedRuleId()));
来自单独线程的DRL处理
public void run() {
/* Loading kie session */
KieServices kServices = KieServices.Factory.get();
File file = new File("/home/unni/Documents/testRules.drl");
Resource resource = kServices.getResources().newFileSystemResource(file).setResourceType(ResourceType.DRL);
kSession = new KieHelper().addResource(resource,ResourceType.DRL)
.build(EventProcessingOption.STREAM).newKieSession();
/* Starting a new Thread to start DRL processing from Working memory */
new Thread(new Runnable() {
public void run() {
kSession.fireUntilHalt();
}
}).start();
/* Inserting AlertGroupRuleModel into DRL kSession */
while (true) {
model = factQueue.poll(500,TimeUnit.MILLISECONDS);
If (null == model ) {
Thread.sleep(1000);
continue;
}
kSession.insert(model);
}
}
DRL必须是这样的
import com.wisilica.rule.engine.model.AlertGroupRuleModel;
import java.util.List
declare AlertGroupRuleModel
@role( event )
end
rule "All Ids are present in sliding window"
dialect "mvel"
timer (cron:0/10 * * * * ?)
when
Boolean(booleanValue == true) from accumulate(
/* <source pattern> : Filter facts with condition tagId = 1*/
AlertGroupRuleModel(tagId = 1) over window:time( 10s ),/* <init code> : Initialize a list with predefined values to check whether all values in list is present in sliding window */
init( $ruleIds : <initialize a list here say 1,5> ),/* <action code> : check if fact's ruleId is present in predefined list*/
action( <Magic_code> to check if $ruleIds.contains(ruleId)),/* <result expression> : if all Ids in predefined list is present in sliding window,then retun as true else false*/
result( <result boolean value,set true if all ids in list are present atleast once in sliding window. else return false > )
)
then
System.out.println("All ids in list are present atleast once in sliding window of 10s ");
end
建议此类规则使用有效的DRL格式
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)