kashili kashili
kashili kashili

Reputation: 955

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

Upvotes: 5

Views: 17952

Answers (6)

Kumaresan Perumal
Kumaresan Perumal

Reputation: 1956

In my experience I was trying to add the following maven dependency in pom.xml after removing those dependencies. it worked for me.

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${runtime.version}</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-decisiontables</artifactId>
        <version>${runtime.version}</version>
    </dependency>

    <dependency>
        <groupId>org.jbpm</groupId>
        <artifactId>jbpm-test</artifactId>
        <version>${runtime.version}</version>
    </dependency>

Upvotes: 0

V.Dev
V.Dev

Reputation: 167

Little late on the answer here, but this might help others.

Check your META-INF/Maven/pom.properties file, and it might contain this:

groupId=
artifactId=
version=

Nothing was defined! Changed it to:

groupId=com.sample  (package name)
artifactId=DroolsTest (class name)
version=0.0.1-SNAPSHOT  

Copy and paste the same data from pom.properties file to pom.xml . Runs fine now.

Upvotes: 2

Alex
Alex

Reputation: 11

I faced a similar issue while trying out the JBoss Examples at: 4.2.6.1. Default KieSession http://docs.jboss.org/drools/release/6.4.0.Final/drools-docs/html/ch04.html#KIEExamplesSection

I was getting the following exception on trying out the default KieSession example:

org.drools.compiler.kie.builder.impl.ClasspathKieProject - Unable to build index of kmodule.xml url=file:/C:/Repo/Git/temp/drools-examples/drools-examples/drools-examples-service/target/classes/META-INF/kmodule.xml
org.xml.sax.SAXParseException; systemId: file:/C:/Repo/Git/temp/drools-examples/drools-examples/drools-examples-service/target/classes/META-INF/kmodule.xml; lineNumber: 1; columnNumber: 52; cvc-elt.1.a: Cannot find the declaration of element 'kmodule'.
10:29:18.243 [main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession name: testsession

I realised that the issue was due to the xmlns used in the default kiession example from Jboss Docs: From JBoss Docs - HAS ISSUE:

kmodule xmlns="http://www.drools.org/xsd/kmodule"

</kmodule>

What I used - WORKS WITHOUT ISSUE:

kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"

</kmodule>

Upvotes: 1

ProfVersaggi
ProfVersaggi

Reputation: 886

I think there is a much simpler answer ... I stumbled upon this just today as I was attempting the very same thing and got it working after following a clue in the discussion lists:

I was Trying to get Drools to execute from Spring MVC WEB in Pure Maven Project (no added natures of any kind) and ran head first into this problem:

ERROR KieContainerImpl - Unknown KieSession name: ksession-rules

Initial RND: (some direction - but no solutions)

Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project) http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html#a4028011

Related Problem (... but a big clue):

WARN ClasspathKieProject - Unable to load pom.properties tried recursing down from/Apache-Tomcat7/webapps/maven-spring-drools/WEB-INF/classes

SOLUTION:

As it turns out, creating the directory WEB-INF/classes and placing the files pom.properties and pom.xml in it solves the problem. The clue came from this link: http://drools.46999.n3.nabble.com/Drools-6-Unable-to-build-index-of-kmodule-xml-td4026791.html - where Mark Proctor indicated that the project has to be a maven project, and it needs to find pom.properties.

As soon as the KIE finds the pom.properties file, it figures out the KieSession name and the problem is solved.

Note: I'd suspect you'd have to manually keep that POM.XML file updated but the pom.properties file never changes.

This has been my experience - hope it helps someone....

Upvotes: 2

David Bernard
David Bernard

Reputation: 306

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.

Upvotes: 8

Mark Proctor
Mark Proctor

Reputation: 711

We have a number of examples, all documented. that get you started. Each can be run from the command line with maven, and have unit test to show them running. https://github.com/droolsjbpm/drools/tree/master/drools-examples-api

Docs here: http://docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html/KIEChapter.html#KIEExamplesSection

Upvotes: 1

Related Questions