Thursday, September 25, 2014

Drools : First Project in Kepler

Drools 6 : Environment Setup in Eclipse Kepler

To create drools project, navigate to File à New à Other.

Now it will open project wizard, in that select Drools à Drools Project and click next.


Enter project name and click next.


Now it will open create new drools project wizard, just click next.
(Selected options will create sample code for us.)


On Drools Runtime Wizard, select ‘Drools 5.1.x or above’ or ‘Drools 6.0.x’ for ‘Generate code compatible with’ field.

Note: Drools 6.0.x (Maven Project) is require ‘GroupId’, ‘ArtificatId’ and ‘Version’ fields.


I have selected ‘Drools 5.1.x or above‘ and click Finish.


Now, Drools project is created with DroolsTest.java and Sample.drl files.
And we have compilation errors.

So Right click on project, Select Build Path à Configure Build Path



Now add, following jars using Add External JARS and click Ok.
drools-compiler-6.1.0.Final.jar
drools-core-6.1.0.Final.jar
drools-reteoo-6.1.0.Final.jar
jbpm-flow-6.1.0.Final.jar
jbpm-flow-builder-6.1.0.Final.jar
knowledge-api-6.1.0.Final.jar
slf4j-simple-1.7.7.jar



Now project is ready with no errors.

Lets run DroosTest.java as Java Application, Just Righclick on DroolsTest.java Run As à Java Application.

Hurray, it is showing the output in console

Hello World
Goodbye cruel world

Here I am giving the Sample Java Code and Rule code with small modifications for reference.

MessageStatus.java
package com.sample;

public enum MessageStatus {
       HELLO, GOODBYE
}

Message.java
package com.sample;

public class Message {
       private MessageStatus status;
       private String message;
       public MessageStatus getStatus() {
              return status;
       }
       public void setStatus(MessageStatus status) {
              this.status = status;
       }
       public String getMessage() {
              return message;
       }
       public void setMessage(String message) {
              this.message = message;
       }
}

DroolsTest.java
package com.sample;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * 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
                     KnowledgeBase kbase = readKnowledgeBase();
                     StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
                     KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
                     // go !
                     Message message = new Message();
                     message.setMessage("Hello World");
                     message.setStatus(MessageStatus.HELLO);
                     ksession.insert(message);
                     ksession.fireAllRules();
                     logger.close();
              } catch (Throwable t) {
                     t.printStackTrace();
              }
       }

       private static KnowledgeBase readKnowledgeBase() throws Exception {
              KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
              kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
              KnowledgeBuilderErrors errors = kbuilder.getErrors();
              if (errors.size() > 0) {
                     for (KnowledgeBuilderError error : errors) {
                           System.err.println(error);
                     }
                     throw new IllegalArgumentException("Could not parse knowledge.");
              }
              KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
              kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
              return kbase;
       }
}

Sample.drl
package com.sample

import com.sample.Message;
import com.sample.MessageStatus;

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

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

Note : Please see environment setup post before this.

No comments:

Post a Comment