Drools7:如何将 drl 文件分配给 kmodule.xml 中的 KieBase?

Drools7: How to assign drl files to the KieBase in kmodule.xml?

我正在尝试从 drools 5 迁移到 drools 7。在版本 6 中,spring 集成发生了变化。基于 documentation drools:resources drools:resource 已删除,但我无法找到如何使用新工具集实现相同的行为。我想要的是具有不同规则的不同 kiebase,这些规则在 drl 文件中定义。

documentation说资源可以使用包来定义。不幸的是,在我的情况下,一个包可能包含多个 drl 文件,我想 filter 其中一些。

我流口水了5.x:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:drools-spring="http://drools.org/schema/drools-spring"
       xsi:schemaLocation="http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <drools-spring:kbase id="rules1And3">
        <drools-spring:resources>
            <drools-spring:resource source="classpath:rules/Rules1.drl"/>
            <drools-spring:resource source="classpath:rules/Rules3.drl"/>
        </drools-spring:resources>
    </drools-spring:kbase>

    <drools-spring:kbase id="rules2And3">
        <drools-spring:resources>
            <drools-spring:resource source="classpath:rules/Rules2.drl"/>
            <drools-spring:resource source="classpath:rules/Rules3.drl"/>
        </drools-spring:resources>
    </drools-spring:kbase>


    <bean id="ruleSessionAutoRefundAndPox" factory-bean="rules1And3"
          factory-method="newStatelessKnowledgeSession"/>

    <bean id="ruleSessionNonCashRefund" factory-bean="rules2And3"
          factory-method="newStatelessKnowledgeSession"/>

</beans>

所以规则下有3个文件。第一个 kbase 只有规则 1 和规则 2,第二个只有规则 2 和规则 3。

"should"在7.x中的样子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:kie="http://drools.org/schema/kie-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">


    <kie:kmodule id="rules">
        <kie:kbase name="rules1And3">   
            <!--loaded drl files-->
        </kie:kbase>
        <kie:kbase name="rules2And3">
            <!--loaded drl files-->
        </kie:kbase>
    </kie:kmodule>

    <!-- maybe these are unnecessary and instead ksessions should been defined within kbase elements-->
    <bean id="sessionRules1And3" factory-bean="rules1And3"
          factory-method="newStatelessKnowledgeSession"/>

    <bean id="sessionRules2And3" factory-bean="rules2And3"
          factory-method="newStatelessKnowledgeSession"/>
</beans>

根据我所看到的,我什至不确定在新版本中是否可以实现完全相同的行为,或者整个方法可能是错误的,但我想要的是能够定义加载哪些 drl 文件对于 kiebase。

感谢您的帮助!

我找不到任何可以显式引用文件的东西。 我所做的是将其放在 main/resources 中的一个文件夹下 即:

src/main/resources/rules1/Rules1.drl

src/main/resources/rules3/Rules3.drl

然后 kbase 定义如下所示:

<kie:kmodule id="rules">
     <kie:kbase name="rules1And3" packages="rules1,rules3"/>   
     ...
</kie:kmodule>

注意1:drl文件中的包应该是正确的。即如果它在 com/some/package 下,那么在 drl 文件中你应该有包 com.some.package 这应该是正常情况,但这在版本 5

中未得到验证

注意 2:如果您有一个单独的 kmodule 用于测试,那么您应该在 test/resouces 中也有 drl 文件以便能够加载它们。

所以我就是这样解决的,如果有更好的答案我会接受,但是这个有效。