Search code examples
drools

Using Drools drl file with common functions in 2 separate kbase/ksessions


using Drools 6.2.0.Final we have a rules project with 2 separate kbases and ksessions configured like this in the kmodule.xml:

<kbase name="kbase1"
      packages="foo.bar.package1">
    <ksession name="ksession1" type="stateless" />
</kbase>

<kbase name="kbase2"
        packages="foo.bar.package2">
  <ksession name="ksession2" type="stateless" />
</kbase>

In each of these packages we have a number of .drl files that contain some business rules. In addition to those each package contains a .drl containing functions that are called from the business rules files, e.g. package1-functions.drl and package2-functions.drl. Since the content of these 2 function files is identical we are thinking about creating a "common" package and have only one copy of the functions file.

However, we have not managed to get our rules to work with the functions being in a separate package.

This is how we configured our kmodule.xml:

<kbase name="common"
    packages="foo.bar.common"/>

<kbase name="kbase1" includes="common"
    packages="foo.bar.package1">
    <ksession name="ksession1" type="stateless" />
</kbase>

<kbase name="kbase2" includes="common"
       packages="foo.bar.package2">
    <ksession name="ksession2" type="stateless" />
</kbase>

Another way we tried was without a "common" kbase but adding the common package to the "packages" attribute in kbase1 and kbase2.

Is it possible to use a common package within 2 other kbases?


Solution

  • There is no way a DRL function can be used from another DRL package.

    It is possible to have one or more functions on a separate DRL file, and they can be used in any DRL file that is in the same DRL package.

    Perhaps you are overrating the importance of DRL packages - you won't have any problems if all DRL files are in the same DRL package.

    Alternatively, consider the use of Java static methods.