我想将Drools与Spring Boot一起用于Bean验证,但我已将问题缩小到以下几行代码:

主班

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        //SpringApplication.run(App.class, args);
        check();
    }

    public static void check() {
        // load up the knowledge base
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession("ksession-rules");
        //go
        Patient patient = new Patient("Hans", "Mueller");
        kSession.insert(patient);
        kSession.fireAllRules();
    }
}


Patient是仅具有id,名字和姓氏的实体,带有getter和setter。

kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>


和两个规则

package com.sample

import com.sample.Patient;

rule "Test"
    when
        eval(1 == 1)
    then
        System.out.println("This rule is always fired");
end

rule "Patient"
    when
        exists Patient()
    then
        System.out.println("Patient found");
end


当不调用SpringApplication.run(App.class, args)时(如上),一切正常:

15:50:12.730 [main] DEBUG org.drools.core.impl.KnowledgeBaseImpl - Starting Engine in PHREAK mode
15:50:12.820 [main] DEBUG org.drools.core.common.DefaultAgenda - State was INACTIVE is nw FIRING_ALL_RULES
15:50:12.821 [main] DEBUG org.drools.core.common.DefaultAgenda - Fire Loop
This rule is always fired
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - Fire Loop
Patient found
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - Fire Loop
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - State was FIRING_ALL_RULES is nw HALTING
15:50:12.827 [main] DEBUG org.drools.core.common.DefaultAgenda - State was HALTING is nw INACTIVE


但是,当我在主目录中添加SpringApplication.run(App.class, args)时,只会触发一个规则:

This rule is always fired


甚至org.drools.core.common.DefaultAgenda的日志记录也不再可见。

我不知道怎么了?我期望两种情况下的输出相同。 SpringBoot在后台执行某些操作吗?

最佳答案

对于每个有相同问题并阅读我的问题的人:

我还没有直接解决它,但是问题与netbeans有关。通过命令行启动应用程序时,一切正常。所以这是要走的路。

关于java - Drools无法与Spring Boot一起正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39898186/

10-11 10:38