Login required
Download

Drools GORM

(1)
Author(s): martins
Current Release: 0.4
Grails Version: 1.2 > *
Tags rules engine workflow
grails install-plugin drools-gorm
This plugin integrates Drools 5 in a Grails application. It provides persistent storage for Drools Flow using GORM.

For Grails version before 1.2 use plugin version 0.3.

Usage

Currently, the plugin provides direct access to the Drools API. (see Drools Documentation Library)

(see integration tests for more examples: Integration Tests on Fisheye)

Custom work item handler

You can use your Grails service artefact as work item handler if they implement the interface WorkItemHandler.

There are two possibilities to refer to your work item from workflow:

  • Convention: If you name your service class for example "HelloWorldWorkItemService" then the work item will be called "Hello World".
  • Configuration: In your service class you can define a static variable called "workItemName".

Drools Flow Example

Create a KnowledgeBase:

def getKnowledgeBase() {
    def str = """<?xml version="1.0" encoding="UTF-8"?> 
        <process xmlns="http://drools.org/drools-5.0/process"
                 xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                 xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
                 type="RuleFlow" name="RuleFlowExample"
                 id="org.drools.examples.gorm.RuleFlowExample" 
                 package-name="org.drools.examples.gorm">
          <header>
          </header>

<nodes> <start id="1" name="Start" x="16" y="16" width="80" height="40" /> <join id="4" name="Join" x="72" y="160" width="80" height="40" type="1" /> <actionNode id="5" name="Action" x="16" y="88" width="80" height="40" > <action type="expression" dialect="mvel" >System.out.println("process started")</action> </actionNode> <eventNode id="6" name="Event" x="128" y="88" width="80" height="40" scope="external" > <eventFilters> <eventFilter type="eventType" eventType="ExampleEvent" /> </eventFilters> </eventNode> <actionNode id="7" name="Action" x="72" y="232" width="80" height="40" > <action type="expression" dialect="mvel" >System.out.println("process finished");</action> </actionNode> <end id="8" name="End" x="72" y="304" width="80" height="40" /> </nodes>

<connections> <connection from="5" to="4" /> <connection from="6" to="4" /> <connection from="1" to="5" /> <connection from="4" to="7" /> <connection from="7" to="8" /> </connections> </process> """

// configure JANINO def pkgBuilderCfg = new PackageBuilderConfiguration() def javaConf = pkgBuilderCfg.getDialectConfiguration("java") javaConf.setCompiler(JavaDialectConfiguration.JANINO)

// create knowledge base def kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pkgBuilderCfg) kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRF) def kbase = KnowledgeBaseFactory.newKnowledgeBase() if (kbuilder.hasErrors()) { def error_str = kbuilder.getErrors().toString() throw new IllegalArgumentException("Could not parse knowledge: $error_str") } kbase.addKnowledgePackages(kbuilder.getKnowledgePackages())

def env = KnowledgeBaseFactory.newEnvironment() env.set(EnvironmentName.GLOBALS, new MapGlobalResolver()) return [kbase, env] }

Create a StatefulKnowledgeSession and start a Process:

def startSimpleProcess() {
    def (kbase, env) = getKnowledgeBase()
    def ksession = 
        GORMKnowledgeService.newStatefulKnowledgeSession(kbase, null, env)
    def ksessionId = ksession.id

def processInstance = ksession.startProcess("org.drools.examples.gorm.RuleFlowExample") def processInstanceId = processInstance.id

ksession.dispose()

return [ksessionId, processInstanceId] }

Load the stored StatefulKnowledgeSession and resume the Process:

def resumeSimpleProcess(ksessionId, processInstanceId) {
    def (kbase, env) = getKnowledgeBase()
    def ksession = GORMKnowledgeService.loadStatefulKnowledgeSession(
                                            ksessionId, kbase, null, env)

ksession.signalEvent(processInstanceId, "ExampleEvent", "eventData")

ksession.dispose() }

Version History

  • 08/Feb/10 - Version 0.4
    • Grails 1.2 support
  • 25/Sep/09 - Version 0.3
    • NEW: Grails service artefacts are automatically registered as work item handlers if they implement WorkItemHandler
    • FIXED: some errors related to multithreading
    • NEW: some integration tests added
  • 03/Sep/09 - Version 0.2
  • 14/Jul/09 - Version 0.1
    • Initial release

Development

  • For issues, improvements or new features use JIRA