Drools Plugin

Drools is a business rule management system (BRMS) and an enhanced Rules Engine implementation, ReteOO, based on Charles Forgy's Rete algorithm tailored for the Java language.

Author: Manohar Viswanathan (manohar.viswanathan@gmail.com)

Features

  • Easy implementation of Drools
  • Rules can be stored in file or in database
  • Rules can be dynamically changed without rebuilding or restarting application
  • Both java & groovy objects can be passed to Rules Engine. (Note that groovy domain objects must be in a package)

Installation

First create a grails project:  grails create-app XXX
make XXX the current directory

(if you need an http proxy issue: grails set-proxy)

grails install-plugin drools

grails run-app

Usage

Components

  • DroolsController: Provides CRUD functionality for Drools Rule
  • DroolsService: Fires all rules

DroolsService

droolsService.fireRules(resource, ruleKey, objList)
  • resource: specifies where the rules are stored. 'file' or 'db'
  • ruleKey: identifier for the rule. For 'file' resource, this would be the name of the file (eg:ticket_example.drl). For 'db' resource, this is the Drools Rule key
  • objList: list of objects that need to be put in session

Example

A simple Ticket processing system is available in the plugin. This example shows how tickets are processed based on their subscription plan.

Run the example: http://localhost:8080/your_app/drools/example

Your console output should be:

Start processing Ticket # 3
Start processing Ticket # 2
Start processing Ticket # 1
Firing rule Bronze Priority for Ticket # 3
Firing rule Silver Priority for Ticket # 2
Firing rule Gold Priority for Ticket # 1
Firing rule Special Discount for Ticket # 1
Display on your browser should be:
Tickets due for processing:
Ticket #1: CustomerName:Jack, Subscription:Gold, Discount:0% Status:New
Ticket #2: CustomerName:Tom, Subscription:Silver, Discount:0% Status:New
Ticket #3: CustomerName:Bill, Subscription:Bronze, Discount:0% Status:New
Firing rules now …
Tickets after processing:
Ticket #1: CustomerName:Jack, Subscription:Gold, Discount:5% Status:Escalate
Ticket #2: CustomerName:Tom, Subscription:Silver, Discount:0% Status:Escalate
Ticket #3: CustomerName:Bill, Subscription:Bronze, Discount:0% Status:Pending

4 Comments

  • Gravatar
    Installed version 0.2 in a new project using grails 1.1 and received the following errors:

    java.lang.LinkageError: loader constraint violation: loader (instance of <bootloader>) previously initiated loading for a different type with name "org/w3c/dom/Document"

    SOLUTION: remove xml-apis-1.0.b2.jar from HOME/.grails/1.1/projects/your_project/plugins/drools-0.2/lib

    java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()

    SOLUTION: change the fireRules method in HOME/.grails/1.1/projects/your_project/plugins/drools-0.2/grails-app/services/DroolsService to seem like the following:

    ...

    import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;

    ...

    def fireRules(resource, ruleKey, objList) { // create package builder def config = new PackageBuilderConfiguration() config.setClassLoader(this.getClass().getClassLoader()) JavaDialectConfiguration javaConf = (JavaDialectConfiguration) config.getDialectConfiguration( "java" ); javaConf.setCompiler( JavaDialectConfiguration.JANINO ); def builder = new PackageBuilder(config)

    … } }

    The reasoning behind this change here: Using JBoss Rules with JBoss or Tomcat

    I'm running OpenSolaris 2008.11 and it's default Java 6 and could run the builtin example with these changes.

    Hope it helps!

    Esteban

    May 06, 2009 15:05 PM esteban
  • Gravatar
    Installed version 0.2 in a new project using grails 1.1 and received the following errors:

    java.lang.LinkageError: loader constraint violation: loader (instance of <bootloader>) previously initiated loading for a different type with name "org/w3c/dom/Document"

    SOLUTION: remove xml-apis-1.0.b2.jar from HOME/.grails/1.1/projects/your_project/plugins/drools-0.2/lib

    java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()

    SOLUTION: change the fireRules method in HOME/.grails/1.1/projects/your_project/plugins/drools-0.2/grails-app/services/DroolsService to seem like the following:

    ...

    import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;

    ...

    def fireRules(resource, ruleKey, objList) { // create package builder def config = new PackageBuilderConfiguration() config.setClassLoader(this.getClass().getClassLoader()) JavaDialectConfiguration javaConf = (JavaDialectConfiguration) config.getDialectConfiguration( "java" ); javaConf.setCompiler( JavaDialectConfiguration.JANINO ); def builder = new PackageBuilder(config)

    … } }

    The reasoning behind this change here: Using JBoss Rules with JBoss or Tomcat

    I'm running OpenSolaris 2008.11 and it's default Java 6 and could run the builtin example with these changes.

    Hope it helps!

    Esteban

    May 06, 2009 15:05 PM esteban
  • Gravatar
    Found good introduction to Drools here: [http://www.onjava.com/pub/a/onjava/2007/01/17/building-enterprise-services-with-drools-rule-engine.html}
    Jun 09, 2009 06:06 AM JGFMK
  • Gravatar
    It could be interesting to append an "active" property to Rule domain and fire only rules actives.
    Jun 23, 2009 10:06 AM jagedn

Post a Comment