drools:如何编写在入口点中没有数据时命中的规则

e3bfsja2  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(301)

我不太会流口水。我期待一个传感器数据,将发送数据从跟踪设备(如标签设备)。我使用drools入口点来跟踪传感器数据。我需要根据这些传感器数据对一些事件发出警报。
drl文件如下

import com.sample.AlertRuleModel;

declare AlertRuleModel 
    @role( event )
    @timestamp( timespamp )
end

rule "No signals are coming from any entry-point for more than 10s"
     when
        $f : AlertRuleModel() from entry-point "AlertRuleStream"
        not(AlertRuleModel(this != $f, this after[0s, 10s] $f) from entry-point "AlertRuleStream")
    then
        $f.setRuleId(1);
        <Do alert here>
end

rule "Rule on Tag1 has not been in zone1 for more than 1 minutes"
     when
         $f : AlertRuleModel( tagId == 1, zoneId == 1 ) from entry-point "AlertRuleStream"
         not(AlertRuleModel(this != $f, tagId == 1, zoneId != 1, this after[0s, 1m] $f) from entry-point "AlertRuleStream")
    then
         $f.setRuleId(2);
        <Do alert here>
end

java代码

kSession = RuleExecutionService.getKieSession(packetProcessorData.getAlertRuleDrlPath());
    ruleStream = kSession.getEntryPoint("AlertRuleStream");
    kSession.addEventListener(new DefaultAgendaEventListener() {
        public void afterMatchFired(AfterMatchFiredEvent event) {
            super.afterMatchFired(event);
            onPostExecution(event, RuleTypeEnum.ALERT_RULE.getName());
        }
    });

    new Thread() {
        @Override
        public void run() {
            kSession.fireUntilHalt();
        }
    }.start();

流数据插入部分

private BlockingQueue<AlertRuleModel> alertFactQueue;
.
.
AlertRuleModel alertRuleModel = null;
while (true) {
    alertRuleModel = alertFactQueue.poll(1, TimeUnit.SECONDS);
    if (alertRuleModel != null) {
        //LOGGER.debug("Inserting alertRuleModel into \"AlertRuleStream\"");
        ruleStream.insert(alertRuleModel);
        continue;
    } 

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        LOGGER.error("Exception while sleeping thread during alertFactQueue polling..", e);
    }

}

但当我运行应用程序时,
第一条规则“超过10秒没有任何信号从任何入口发出”根本不起作用。我不知道为什么,请告诉我,如果我做错了什么或任何语法错误是第一条规则。
在第二个规则“tag1在zone1中的时间不超过1分钟”的情况下,当我传递tagid==1和zoneid==1的事实时,它总是立即命中。我尝试了不同的时间间隔,比如[0秒,10米]之后。但它仍然会在传递带有上述值的事实之后立即命中。
请告诉我我在哪里犯了错误。。?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题