Question

Getting started (Without Maven) I first Installed GEF and Drools 6.0.0 final plugin in eclipse. and then I created a Drools project which generated the two files below.

DroolsTest.java

package com.sample;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
/**
 * This is a sample class to launch a rule.
 */

public class DroolsTest {
public static final void main(String[] args) {
    try {
        // load up the knowledge base
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession("ksession-rules");

        // go !
        Message message = new Message();
        message.setMessage("Hello World");
        message.setStatus(Message.HELLO);
        kSession.insert(message);
        kSession.fireAllRules();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

public static class Message {

    public static final int HELLO = 0;
    public static final int GOODBYE = 1;

    private String message;

    private int status;

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getStatus() {
        return this.status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

}

}

Sample.drl

package com.sample

import com.sample.DroolsTest.Message;

rule "Hello World"
    when
        m : Message( status == Message.HELLO, myMessage : message )
    then
        System.out.println( myMessage );
        m.setMessage( "Goodbye cruel world" );
        m.setStatus( Message.GOODBYE );
        update( m );
end

rule "GoodBye"
    when
        Message( status == Message.GOODBYE, myMessage : message )
    then
        System.out.println( myMessage );
end

I get NPE at kSession.insert(message); obviously due to missing ksession-rules here

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

I get the same thing when I mavenize this project and run it as a maven test. I notice some ppl already experienced this and are point to classpath issue, I am still not clear with the solution though.

mvn eclipse:eclipse did not help either.

Links I went thru already

  1. Getting null pointer exception while running helloworld in drools
  2. Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)
Was it helpful?

Solution

After going thru Drools 6.0.0 in github, I see a file kModule.xml should be present with session name "ksession-rules" tied to a rule. This file did not get generated (bug??)

I am however downgrading to 5.6.0 to get better community support and good documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top