Search code examples
javaeclipsemavendrools

Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)


I am trying to adapt drools6.0 for an existing code base (it is maven project under eclipse). I didnt had need to learn drools or maven before (though they were part of my previous project), suffice to say I am lost in what I wanted to do. Based on my understanding (googling), java class files get hooked to rules based on the package name(?). That takes care of compile time issues. But I am seeing null pointer exception at run time. Inorder to adapt drools into my existing code base: I 1)created helloworld drools project, ran it successfully 2)copied the java file to my existing package, 3)created rule file in Eclipse with correct package: FIle->New->other->Rule Resource; 3)converted existing project into drools package by right clicking project and configure->convert to drools project

This all takes care of compilation issues, but I get following run time error

[main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession    name: ksession-rules
java.lang.NullPointerException
at main.java.com.harmonia.cbm.afloat.dataaquisition.dql.DroolsTest.main(DroolsTest.java:23)

This is because ksession that is returned from kcontainer is null and throws null pointer exception in last line

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// above line is returning null
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
kSession.insert(message);

Already spent more than a day trying to figure out how drools works and how above can be fixed. Pl suggest

1) am I taking the right approach to convert existing project into drools project. I want all existing functionality of my code base; but want to add rules based approach for future enhancements. Came across following link, but not clear if it helps my situation http://drools.46999.n3.nabble.com/Retrofitting-a-project-with-JBoss-Rules-td48656.html

2)Any useful drools tutorials in better understanding following 3 lines (besides java docs)

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");

3)Any hints on resolving null pointer exception (assuming I am taking the right and easy approach of converting existing project into drools project)

UPDATE @David: thanks for detailed post. I realized that converting existing project into maven project, while works, did not appeal to me since existing directory structure/naming is preserved (most likely different from what maven creates by default). I posted alternative solution where I thought this problem has to do with classpath issues http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html#a4028011


Solution

  • I hit similar problems.

    I think that part of the problem is trying to live in both worlds. The JBoss Drools eclipse plugin world and the maven world.

    I have Eclipse 4.3.1 (Kepler) with various Jboss/Drools plugins installed.

    I took a working eclipse example and made sure I could run it in maven.

    1. Created a demo drools project File->New->Other..->Drools->Drools Project
    2. Ensured you could run the test programs DroolsTest
    3. Converted project to maven project - Configure->Convert To Maven Project (This will create a pom.xml file with many dependencies. These can be prunes)
    4. Removed the Drools Library from the build path - in the project properties Build Path -> Libraries - select Drools Library and click Remove
    5. Disable the Drools builder - in project properties Builders -> uncheck Drools Builder
    6. Comment out dependancy jsr94 from the pom.xml(not retrievable)
    7. Run maven from the command line "mvm clean install".

    This should give you a project that builds and runs entirely from Maven.

    Add to your pom.xml

     <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
      </plugin>
    

    And

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.2</version>
      <scope>runtime</scope>
    </dependency>
    

    Try:

    mvn -e exec:java -Dexec.mainClass="com.sample.DroolsTest"

    It should produce:

    
    ...
    [com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule: file:/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes/META-INF/kmodule.xml
    [com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - KieModule was added:FileKieModule[ ReleaseId=x:x:1.0file=/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes]
    [com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule: file:/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes/META-INF/kmodule.xml
    [com.sample.DroolsTest.main()] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - KieModule was added:FileKieModule[ ReleaseId=x:x:1.0file=/Users/davidbernard/Projects/action-deducing-diff/xx/target/classes]
    Hello World
    Goodbye cruel world
    ...
    
    

    You should now also be able to run DroolsTest from eclipse.

    You will have a rules->Sample.drl file and a kmodule.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
        <kbase name="rules" packages="rules">
            <ksession name="ksession-rules"/>
        </kbase>
    </kmodule>
    

    The "ksession" name should match the code creating the ksession:

    KieSession kSession = kContainer.newKieSession("ksession-rules");
    

    The "packages" should match the directory the rule file is in.