【问题标题】:Drools7: How to assign drl files to the KieBase in kmodule.xml?Drools7:如何将 drl 文件分配给 kmodule.xml 中的 KieBase?
【发布时间】:2019-07-31 09:19:18
【问题描述】:

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

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

我在 drools 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,第二个 kbase 只有规则 2 和规则 3。

它在 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>

根据我所看到的,我什至不确定在新版本中是否可以实现相同的行为,或者整个方法可能是错误的,但我想要的是能够定义为基数。

感谢您的帮助!

【问题讨论】:

    标签: spring drools


    【解决方案1】:

    我找不到任何可以显式引用文件的方法。 我所做的是将文件夹放在 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>
    

    NOTE1:drl 文件中的包应该是正确的。即如果它在 com/some/package 下,那么在 drl 文件中你应该有 package com.some.package 这应该是正常的,但这在版本 5 中没有得到验证

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

    所以这就是我设法解决它的方法,如果有更好的答案我会接受它,但这有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      相关资源
      最近更新 更多