Monday, August 4, 2008

Drools Plugin for Grails Application

I have just released a Grails Plugin for Drools. The plugin details can also be found at http://www.grails.org/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.

Features

  • Easy implementation of Drools

  • Rules can be stored in file or in database

  • Rules can be dynamically changed without rebuilding or restarting application


Installation

grails install-plugin drools

Usage

  • Install the plugin

  • Verify example rules: http://localhost:8080/your_app/drools/example

  • Create your own rules: http://localhost:8080/your_app/drools and use droolsService in your code


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: Customer[Name:Jack, Subscription:Gold, Discount:0%] Status[New]
Ticket #2: Customer[Name:Tom, Subscription:Silver, Discount:0%] Status[New]
Ticket #3: Customer[Name:Bill, Subscription:Bronze, Discount:0%] Status[New]
Firing rules now ...
Tickets after processing:
Ticket #1: Customer[Name:Jack, Subscription:Gold, Discount:5%] Status[Escalate]
Ticket #2: Customer[Name:Tom, Subscription:Silver, Discount:0%] Status[Escalate]
Ticket #3: Customer[Name:Bill, Subscription:Bronze, Discount:0%] Status[Pending]

4 comments:

  1. I’ll try to look into that

    ReplyDelete
  2. Are you planning to upgrade for Drools 5 ? The are some class changes on it and there are the new Drools Flows…

    regards

    Cristiano

    ReplyDelete
  3. I downloaded your drools plugin last night. It looks really cool, and I think it is just what my application is needing.

    But I wonder, is there a way to also get Guvnor deployed/integrated into my application?

    ReplyDelete
  4. Are you considering integrating Guvnor as part of the drools plugin? If you are looking for a general how-to article, you can take a look at this:
    http://magazine.redhat.com/2008/08/12/jboss-drools-how-to-tuning-guvnor-part-1/

    ReplyDelete