0
votes

I'm trying to migrate from drools 5 to drools 7. In version 6 there were changes in the spring integration. Based on the documentation drools:resources drools:resource was removed, however I couldn't find out how to achieve the same behavior with the new toolset. What I want is to have different kiebases with different rules, that are defined in drl files.

The documentation says that the resources can be defined using packages. Unfortunately in my case a package may contain several drl files and I want to filter some of them.

What I had in 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>

So here there were 3 files under the Rules. The first kbase only had rule 1 and rule 2 and the second had only rule 2 and rule3.

How it "should" look like in 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>

Based on what I saw I'm not even sure if the very same behavior is achievable in the new version or maybe the whole approach is wrong, but what I want is to be able to define which drl files are loaded for a kiebase.

Thanks for any help!

1

1 Answers

0
votes

I could not find anything that would make it possible to refer to a file explicitly. What I did is that I placed the under a folder in the main/resources i.e:

src/main/resources/rules1/Rules1.drl

src/main/resources/rules3/Rules3.drl

and then the kbase definition looks like this:

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

NOTE1: the package in the drl file should be correct. i.e. if it is under com/some/package then in the drl file you should have package com.some.package This should how it is normally, but this was not validated in version 5

NOTE2: if you have a separate kmodule for tests then you should have the drl files also in test/resouces in order to be able to load them.

So this is how I manged to resolve it, if there will be any better answer I will accept it, but this works.