Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

java - Getting null pointer exception while running helloworld in drools

I got the following error while running a simple helloworld sample drools project.

199 [main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession name: ksession-rules
java.lang.NullPointerException
    at com.sample.DroolsTest.main(DroolsTest.java:24)

Code:

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;
        }

    }

}

drools code:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks drools kie-api/internal library execution looks for mandatory values under srcmain esourcesMETA-INFmavenpom.properties file for the drools eclipse project.

Updating my pom.xml or pom.properties into below content worked fine on 6.0.0 drools distribution.

groupId=com.test.sample.drools
artifactId=DroolsTestProject
version=1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...