0

我正在研究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();
    }

}

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

4

2 回答 2

0

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

ksession.fireAllRules();

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

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

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

于 2015-05-28T13:52:37.493 回答
0

看起来您定义了 2 个 KieBaseConfigurations。config_time.setOption(EventProcessingOption.STREAM); 设置为流式传输但从未使用过。

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

于 2015-05-29T21:38:14.540 回答