Setting up Drools in Eclipse

Drools version used : 5.5
Eclipse : Luna

Baby steps for setting up Drools 5.5 in Eclipse...

1. Create a Java Project in Eclipse
2. Within src folder, I created folders "java" and "rules" for accommodating the java and .drl file
3. Download the Drools 5.5  jars from Maven 
http://mvnrepository.com/artifact/org.drools/drools-core/5.5.0.Final
4. The following are the jars that are required for making drools work.


  • xercesImpl.jar

  • xalan-2.7.1.jar

  • drools-compiler-5.5.0.Final.jar

  • drools-core-5.5.0.Final.jar

  • knowledge-api-5.5.0.Final.jar

  • mvel2-2.1.3.Final.jar

  • antlr-runtime-3.3.jar

  • knowledge-internal-api-5.5.0.Final.jar

  • slf4j-api-1.6.4.jar

  • serializer-2.7.1.jar

  • core-3.2.3.v_686_R32x.jar

  • xml-apis-1.4.01.jar

  • xmlbeans-2.6.0.jar

  • 5. Create a user library and include those jars in the java build path


    6. Create a drools file named Sample.drl in the rules folder.
    7. Create a main class in the java folder, with the following code snippet 

    public static void main(String args[]) {

               KnowledgeBase kbase = readKnowledgeBase();
               StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
               ksession.insert(// Insert the Fact object Here);
                    // Define the object that is going to be used as global variable
               Status  status =  new Status();
                 // Set the global value
               ksession.setGlobal("status", status);
               ksession.fireAllRules();
               ksession.dispose();
    }

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

    8. Don't forget to define the required rules in .drl file.

    Happy Drooling....






    No comments:

     Python Basics How to check the version of Python interpreter mac terminal

    Popular in last 30 days