Spring JBoss Rules (Drools) Integration

From Kb

Jump to: navigation, search
package org.drools.examples;
 
import java.util.ArrayList;
import java.util.List;
 
import org.drools.StatefulSession;
import org.drools.ioc.StatefulAdapterIF;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
 
/**
 * This is a Spring configured version of the 'HelloWorld' Example. The only difference is that
 * we allow Spring to Configure and load the rules for us. Once we have our handle to the 
 * StatefulSession (Drools) the sample works exactly the same as before.
 */
public class HelloWorldSpringExample {
 
    public static final void main(final String[] args) throws Exception {
 
    	//Allow Spring to configure our components, then get a Stateful (Drools) Session
    	//Steps 1 and 2 are only needed as we run the example from the command line
    	//See documentation / commnets below on how it is more simple when
    	//your entire web / ejb app is already Spring configured.
 
 
       // 1. Read the configuration file - presumes current dir is project root folder
        ApplicationContext appContext= 
          new FileSystemXmlApplicationContext(
            "java/spring-config-stateful.xml");
 
        //2. get an object from the Spring (XML) context (config file)
        //'statefulAdapter' is a name defined in our Spring xml file
        StatefulAdapterIF statefulAdapterSpringBean= 
          (StatefulAdapterIF) appContext.getBean("statefulAdapter");
 
        //====================================================
 
        //Note that if you have Spring already on your project you
        //Could skip the above steps - you would have a setStatefulAdapter(StatefulAdapterIF)
        //method (on this class) and allow Spring to hand you a reference to the StatefulAdapterIF
 
        //====================================================
 
        //The parts after this point are the same as the normal
        //Java-Drools HelloWorld Sample. We've removed the 
        //listener / debugging code to make this clearer
 
        //(A) Get our (Drools) Stateful Session
        final StatefulSession session = statefulAdapterSpringBean.newStatefulSession();
        session.setGlobal( "list", new ArrayList() );
 
        //(B) Create our JavaBean Fact object
        final Message message = new Message();
        message.setMessage( "Hello World" );
        message.setStatus( Message.HELLO );
 
        //(C) Insert Fact into Session / Memory and fire all rules
        session.insert( message );
        session.fireAllRules();
 
        //(D) Dispose of the session
        session.dispose();
    }
 
    public static class Message {
        public static final int HELLO   = 0;
        public static final int GOODBYE = 1;
 
        private String          message;
 
        private int             status;
 
        public Message() {
 
        }
 
        public String getMessage() {
            return this.message;
        }
 
        public void setMessage(final String message) {
            this.message = message;
        }
 
        public int getStatus() {
            return this.status;
        }
 
        public void setStatus(final int status) {
            this.status = status;
        }
 
        public static Message doSomething(Message message) {
            return message;
        }
 
        public boolean isSomething(String msg, List list) {
            list.add( this );        
            return this.message.equals( msg );
        }
    }
 
}
Personal tools