kie-spring : drl files not found in the classpath.

1,321 views
Skip to first unread message

besyl

unread,
Apr 29, 2015, 7:12:56 PM4/29/15
to drools...@googlegroups.com

I have a situation. I am testing in junit with .drl files using the kie-spring.

The DRL files are found/scanned only when they are co-located in the src/test/resources folder and not in the src/main/resources/folder.

I even moved the drl files to a separate jar project/file into a src/main/resources folder along with the kie-spring .xml files. Still no luck!

I get a WARN : No files found for KieBase kbase1, searching folder ...

How can I do?

Spring config file :

<import resource="applicationPropertiesContextTest.xml" />
    <import resource="dependenciesInjectionContextTest.xml" />
    <import resource="classpath*:droolsContext.xml" />

droolsContext.xml:
                
  <kie:kmodule id="mykmodule">
          <kie:kbase name="kbase1" default="true" packages="rules" >
            <kie:ksession name="ksession" default="true" type="stateless"  />
        </kie:kbase>   
    </kie:kmodule>

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

My droolsContext.xml is load but drl not found.

After test I want to package this jar with DRL in a war and I'll have the same problem.

Think you for response.


Ken Siprell

unread,
Apr 30, 2015, 2:43:22 AM4/30/15
to drools...@googlegroups.com
I've had similar problems developing a Grails (http://www.grails.org/) plugin for Drools using kie-spring. I'm hoping you can use the information below to find some parallels in your application that might be useful. First some background on Grails and my plugin.

- The code referenced below is written in Groovy, but it should be easy to understand for a Java developer.

- The beans are defined here in the plugin's associated test application: https://github.com/kensiprell/grails-drools-sample/blob/master/grails-app/conf/drools-context.xml. I started out defining the kiePostProcessor bean in this file and had problems with the rule files not being found.

- The rule files are located under src/resources/drools-rules: https://github.com/kensiprell/grails-drools-sample/tree/master/src/resources/drools-rules. Note that the rule files are not added to a jar. The directory tree holding the files is copied to the appropriate directory when the application is compiled or a war is built.

- The plugin has a class (https://github.com/kensiprell/grails-drools/blob/1.1.0/DroolsGrailsPlugin.groovy) whose closures are called whenever the plugin or its associated application is started whether in testing, development, or production mode.

- The doWithSpring closure below uses http://grails.github.io/grails-doc/2.4.5/api/index.html?grails/spring/BeanBuilder to importBeans. I've inserted comments that might help you get your kiePostProcessor working.

def doWithSpring = {
   
try {
        importBeans
("drools-context.xml")
   
} catch (e) {
        log
.debug(e)
        log
.error "grails-app/conf/drools-context.xml does not exist. Try 'grails create-drools-config' or 'grails create-drools-context'."
   
}

   
// If the application is running in a container such as Tomcat, Jetty, etc. webInfClasses should return something like:
   
// /Users/Ken/Library/tomcat/webapps/grails-drools-sample-1.1.0/WEB-INF/classes
   
File webInfClasses = application.parentContext?.getResource('WEB-INF/classes')?.file
   
String configFilePath
   
if (webInfClasses?.exists()) {
       
// Use canonicalPath in case we're running on Windows
        configFilePath
= configFilePath = new File(webInfClasses.path).canonicalPath
   
} else {
       
// This returns something like the line below if Grails is running in development or test mode:
       
// /Users/Ken/Development/Plugins/grails-drools-sample/src/resources
       
String userDir = System.getProperty("user.dir")
        configFilePath
= new File("$userDir/src/resources").canonicalPath
   
}
    URL configFileURL
= new File(configFilePath).toURI().toURL()
   
// This Grails DSL might look confusing, but it's creating a bean named "kiePostProcessor" using this constructor:
   
// https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-spring/src/main/java/org/kie/spring/KModuleBeanFactoryPostProcessor.java#L79
    kiePostProcessor
(KModuleBeanFactoryPostProcessor, configFileURL, configFilePath) {}
}

Ken

John Spartan

unread,
Aug 5, 2015, 10:26:44 AM8/5/15
to Drools Setup
I just had this problem and bumped on that groovy code from Ken Spirell. Helped a lot.


For what it's worth here is my java implementation :

Java code :
public class MyKModuleBeanFactoryPostProcessorFactory {

   
public KModuleBeanFactoryPostProcessor get() throws IOException {

        URL resource
= getClass().getResource("WEB-INF/classes");

       
String configFilePath;
       
if (resource != null) {
           
File webInfClasses = new File(resource.toString());

            configFilePath
= webInfClasses.getCanonicalPath();
       
} else {

           
String userDir = System.getProperty("user.dir");

            configFilePath
= new File(userDir + "/src/main/resources").getCanonicalPath();
       
}
        URL configFileURL
= new File(configFilePath).toURI().toURL();
       
return new KModuleBeanFactoryPostProcessor(configFileURL, configFilePath);
   
}

}

Spring context :
<bean id="myKModuleBeanFactoryPostProcessorFactory" class="com.x.x.MyKModuleBeanFactoryPostProcessorFactory " />
   
<bean id="kiePostProcessor" factory-bean="myKModuleBeanFactoryPostProcessorFactory" factory-method="get" />



Reply all
Reply to author
Forward
0 new messages