Kie-spring Issue: Cannot find rules at class path

985 views
Skip to first unread message

Cheng Yun

unread,
May 10, 2015, 1:00:15 PM5/10/15
to drools...@googlegroups.com
Hello,

I am working on a project using drools 6.1.0.Final.

My rule files are located in a separate jar from the java code that starts the rule engine. Spring is used to integrate my rules and the system. When running the program, kie-spring only looks the local classpath for the rule resources (in my case it is "../target/test-classes/), so it cannot find my rule files in the jar (already set as depencency). 

I have had a look at the class "KModuleBeanFactoryPostProcessor" and I found that the following code causes the issue:

protected void initConfigFilePath() {
try {
configFilePath = getClass().getResource("/").toURI().getPath();
} catch (URISyntaxException e) {
configFilePath = getClass().getResource("/").getPath();
}
}

The default constructor calls this method to find the configFilePath, so it always look at the classpath where KModuleBeanFactoryPostProcessor is located:

<bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor"/>
 
So I changed a little bit of the bean creation statement like this:
 

<bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor"/>

     <Constructor-arg class = "configFileURL" value = ""/>

<Constructor-arg class = "configFilePath" value = "Give an absolute path of rule files here"/> </bean>
 
By now, I can specify an absolute location where the kie-spring should look at, but this way does not  meet my requirements. Ideally, I would like a classpath solution that kid-spring can find my rule files in a dependency jar.

I guess this is not a new issue. Any help is much appreciated. 

Many thanks,
Cheng
 

Vinod Kiran

unread,
May 11, 2015, 10:51:51 AM5/11/15
to drools...@googlegroups.com
Would you be able to attach a simple reproducer? 

Cheng Yun

unread,
May 11, 2015, 11:26:34 AM5/11/15
to drools...@googlegroups.com
Hi Vinod,

Sorry, Im not able to provide my code but it is very simple to reproduce the scenario:
1. Create two hello world projects using drools6 plugin: ProjectA, ProjectB
2. Let ProjectA depend on ProjectB
3. Remove Rules files from ProjectA and remove java code from ProjectB
4. Create a spring config file in ProjectA
5. Try to run the program from ProjectA
6. Result will be rules not found

Hopefully this will help understand the problem.

Thanks,
在 2015年5月11日星期一 UTC+8下午10:51:51,Vinod Kiran写道:

Vinod Kiran

unread,
May 11, 2015, 10:03:37 PM5/11/15
to drools...@googlegroups.com
DRL, Support Java POJO, kmodule.xml
mvn dependency on Project A, spring xml, java code to lookup kbase/ksession insert objects and fireAllRules

The above code seems to work fine, do verify that this is close to the scenario you are trying and let me know if this helps.

Cheng Yun

unread,
May 12, 2015, 12:17:38 PM5/12/15
to drools...@googlegroups.com
Hi Vinod,

Thanks a lot for your reply but the way I'm using kid-spring seems to be different from yours. Please find attached sample code that mocks up the scenario I'm facing to.

As described in my previous email, it is based the hello world sample code generated by the eclipse plugin. Maven dependency + spring. 

kid-spring is able to create the kiebase/kieSession, but no rules are loaded actually because it only looks at the Java project's target/classes directory where no rules exist.

thanks,
在 2015年5月12日星期二 UTC+8上午10:03:37,Vinod Kiran写道:
XtestJava.zip
XtestRules.zip

Vinod Kiran

unread,
May 12, 2015, 11:52:53 PM5/12/15
to drools...@googlegroups.com
In a quick glance thru the code, I notice that you are using 6.1. Would it be possible to move to 6.2?


On Sunday, May 10, 2015 at 10:30:15 PM UTC+5:30, Cheng Yun wrote:

Cheng Yun

unread,
May 13, 2015, 12:02:50 PM5/13/15
to drools...@googlegroups.com
Yes, I have tried using 6.2.0.Final org.kie jars, the result was the same. I think the key is class "KModuleBeanFactoryPostProcessor", which is not changed in 6.2.

Thanks,
在 2015年5月13日星期三 UTC+8上午11:52:53,Vinod Kiran写道:

Vinod Kiran

unread,
May 13, 2015, 8:06:36 PM5/13/15
to drools...@googlegroups.com
6.2 introduced a feature to import existing kbase(s) from class path into spring context.

Try again with 6.2 and this time, use the kie:import tag and you should have the code working fine.

https://github.com/vinodkiran/kie-spring-java/blob/master/src/main/resources/beans.xml

Cheng Yun

unread,
May 14, 2015, 10:29:55 AM5/14/15
to drools...@googlegroups.com
Hi Vinod,

Thanks very much. Do you mean that I just add a <kie:import> tag in the spring config, like this?

<kie:import />

<kie:kmodule id="Kmodule1">

<kie:kbase name="process" packages="process">

<kie:ksession name="ksession-process" />

</kie:kbase>

</kie:kmodule>

<bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" />

Will have a try when Im back to work.

Thanks again,
在 2015年5月14日星期四 UTC+8上午8:06:36,Vinod Kiran写道:

Vinod Kiran

unread,
May 14, 2015, 1:12:57 PM5/14/15
to drools...@googlegroups.com
Remove the kie:kmodule completely. Only the kie:import will suffice.

What is happening here is that the rules jar (project a) has the drl, pojo and kmodule.xml. As this jar is on the class path, the kmodule.xml is auto discovered and the kbase is created.

All this happens before the Java project (project b) is initialized and spring context is created. The kie:import tag tells the kie-spring module to import all kbases (and associated ksessions) found on the class path into the spring context.

You do not need the kie:kmodule spring tag because you do not need to recreate the kbase, you are only trying to refer to an existing kbase.

Hope this helps.

Vinod Kiran

unread,
May 14, 2015, 1:13:14 PM5/14/15
to drools...@googlegroups.com
Message has been deleted

Cheng Yun

unread,
May 15, 2015, 8:46:08 AM5/15/15
to drools...@googlegroups.com
Hi Vinod,

The actual scenario is that we don't use kmodule.xml, we use spring xmls to configure our kbases. We have hundreds of rule files and over 70 kbases migrated from Drools 5 and rules config files are part of our spring framework. That's why we are trying to use pure spring integration instead of a seperate kmodule.xml file.

Does that mean it is not possible for the ke-espring to find rules in a dependency jar?

Thanks,
在 2015年5月15日星期五 UTC+8上午1:12:57,Vinod Kiran写道:

Vinod Kiran

unread,
May 15, 2015, 9:48:34 PM5/15/15
to drools...@googlegroups.com
With Drools 6, the recommended way would be to make the these jar files self contained deploy-able units (kjar) and use them in your java application.  

In the absence of the kmodule.xml, currently kie-spring (and even kie) would not be able find the rule files (DRL, DTable, ....) from jars on the classpath.

Cheng Yun

unread,
May 17, 2015, 5:41:03 AM5/17/15
to drools...@googlegroups.com
Hi Vinod,

Thank you for the information, which will help us make design decisions. According to that, we may choose to use kmodule.xml to define kbases, but one problem would be the large number of  kbases we have. For maintainability reasons, it is not good that we put all kbase definitions in one single kmodule.xml file, is there a way that we can break them down into several sub-files and summarise in the kmodule.xml.(e.g., in spring, we use the <import> tag)? 

Thanks
在 2015年5月16日星期六 UTC+8上午9:48:34,Vinod Kiran写道:

Jerry Lacy

unread,
Nov 25, 2015, 12:48:16 PM11/25/15
to Drools Usage

Hi,

 

I too have the situation where ProjectB contains the .drl files and ProjectA uses them to create KieSession instances via kie-spring.  I’ve tried the solution of having the META-INF/kmodule.xml in ProjectB and using the <kie:import/> tag in the spring config for ProjectA.  This seems to work fine with one important exception: there doesn’t seem to be a way to tell spring that the beans are to have scope=prototype.  When trying to obtain new KieSession instances this obviously a huge problem for most use cases since only one singleton instance is created.  This seems to make kie-spring usable only in a project that contains the .drl files.

 

The other major issue I’ve run into trying to use kie-spring is the problem with Junit tests only being able to locate .drl files from within /source/test/resources and not /source/main/resources (covered in another thread).  This creates a situation where we are forced to keep 2 sets of .drl files in sync.

 

Am I overlooking anything relative to these 2 problems?  I would love to use kie-spring but these issues seem to be show stoppers.

Reply all
Reply to author
Forward
0 new messages