在新事件的情况下,Drools融合而不是解雇规则

时间:2015-05-28 12:12:29

标签: java drools complex-event-processing drools-fusion

我正在研究drools fusion 6.2 final,并希望在流模式下发生新事件时触发规则。但规则并没有自行解雇。

我的规则文件内容如下:

//created on: May 8, 2015
package com.test

import com.test.Applicant
declare Applicant
   @role(event)
end

rule "Your First Rule"
when
    accumulate( $st : Applicant(age: age) over window:time(10ms) from      entry-point X , $c: average ( age ) )
then
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~avg~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println($c);
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~avg~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
end

代码如下:

package com.test;

import org.kie.api.KieBaseConfiguration;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.conf.ClockTypeOption;
import org.kie.api.runtime.rule.EntryPoint;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatefulKnowledgeSession;

@SuppressWarnings("deprecation")
public class DroolsTest {


    public DroolsTest() {
        KieBaseConfiguration config_time = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
        config_time.setOption(EventProcessingOption.STREAM);

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("test.drl", getClass()), ResourceType.DRL);
        if (kbuilder.hasErrors()) {
            System.out.println("EERRRRRRRROOOOOORRRR");
            System.out.println(kbuilder.getErrors().toString());
            System.out.println("EERRRRRRRROOOOOORRRR");
        }

        KieBaseConfiguration kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();

        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

        KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
        conf.setOption(ClockTypeOption.get("REALTIME"));
        final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(conf, null);
        final EntryPoint atmStream = ksession.getEntryPoint("X");

        for (int i = 0; i < 100; i++) {
            System.out.println("LOOOOOOOOOOOOOOOOOOOOP  " + i);
            Applicant applicant = new Applicant("Mr John Smith " + i, i);
            atmStream.insert(applicant);
//          ksession.fireAllRules();
        }
        ksession.dispose();
    }

    public static void main(String[] args) {
        new DroolsTest();
    }

}

故意调用规则时会触发规则,但在流上发生新事件时无法触发规则。

2 个答案:

答案 0 :(得分:0)

如果您不调用fireAllRules,则不会看到规则的任何影响。取消注释这一行:

ksession.fireAllRules();

虽然,重复调用fireAllRules运行会话并不是最好的方法。最好是使用一个线程来调用fireUntilHalt,例如

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

并从另一个线程插入事件,例如,在使用Thread.sleep模拟事件和时间流逝或从外部源读取事件时从循环中插入事件。

答案 1 :(得分:0)

看起来你有2个KieBaseConfigurations定义。 config_time.setOption(EventProcessingOption.STREAM);已设置为流媒体但从未使用过。

尝试在您实际使用的kBaseConfig上设置EventProcessingOption.STREAM。