Pages

Saturday, September 26, 2009

JBoss Drools Content Repository

JBoss Drools project features a BRMS called Guvnor. Drools Guvnor features a rich web based GUI, including editors and other stuff. Sometimes you cannot really use Guvnor in your deployment due to some bureaucratic issues: for instance, all the web UIs in our organization are Flex-based and Guvnor's UI is GWT-based (there might be other reasons for sure). So you want to use Drools but your obliged to use the tools/frameworks that are approved in your organization.

There are some issues that you have to pay attention to when starting to use Drools:
  • How to store the rules?
  • How to version the rules?
  • What will be your deployment scheme?
  • Who will manage the rules?
  • Can the domain experts understand how the rule editors work?
  • etc

In this post I'd like to cover how the rules are stored with drools-repository module of JBoss Drools.

JBoss Drools uses Jackrabbit JCR implementation for storing the rules. drools-repository is a thin wrapper around the JCR repository, and its aim is to store the rules in a predefined way. So basically, if you need to store the rules somehow differently than Drools does by default, you can just re-configure the Jackrabbit that lays underneath drools-repository.

Guvnor's source code is a good reference for learning the drools-repository API. There's also a good post on how to make Guvnor to use MySQL to persist the rules.

To setup the repository you can either use JackrabbitRepositoryConfigurator directly, or use RepositorySessionUtil.getRepository() which returns RulesRepository instance, which provides you the API to work with the assets in the Drools' JCR repository

Next, drools-compiler actually defines a class model for rules. The org.drools.guvnor.client.modeldriven package provides a number of classes that can be used to model the rules in your UI. This is why it contains the 'client' in its name in order to support GWT compiler. RuleModel is the top level class that represents a rule. So once you have retrieved the rule asset for the repository, you have the RuleModel class and you can now create you're own widgets to represent the rule in you UI.

A good place to see how does the rule persistence work in Guvnor is the org.drools.guvnor.server.contenthandler package. For instance, let's take a look on the code in BRLContentHandler:

private String getSourceDRL(AssetItem asset, BRMSPackageBuilder builder) {
       RuleModel model = BRXMLPersistence.getInstance().unmarshal(asset.getContent());
       model.name = asset.getName();
       model.parentName = this.parentNameFromCategory(asset, model.parentName);  
       String drl = BRDRLPersistence.getInstance().marshal(model);
       if (builder.hasDSL() && model.hasDSLSentences()) {
         drl = builder.getDSLExpander().expand(drl);
       }
       return drl;
     }    

You can read that RuleModel is retrieved using BRXMLPersistence class, and further on a DRL (technical rule) representation can be retrieved using BRDRLPersistence class. That said, BRXMLPersistance and BRDRLPersistence can be used to convert between text and class model representation of rules. This can obviously help you to use this API to build your own UI upon the drools-repository.

6 comments:

Diego Naya said...

Great post Anton. Did you developed a custom UI for Guvnor?

Unknown said...

Thx Diego,

We have developed a very simplistic UI as we wanted the rules authoring and management features to be embedded into our Flex-based app. The problem was that we did only one-way translation from wizard into DRL, and so I started to seek for a possibility to make it translatable back and forth, so we could draw a GUI form from DRL and also generate GRL once the user have filled a form. It should work basically the same way as Drools Eclise plug-in or Guvnor's guided editor, but we want it to be more domain-specific so our users could understand it better.

RWilson said...

Hi,
Great post. Can you please provide some pointers to integrating SCM like SVN,CVS with guvnor ?

thanks
rw

Unknown said...

@RWilson, sorry, haven't put my hands on Guvnor for a while.

drools tester said...

Hello,

we are planning to use Drools as a rule engine for our project here, I have few queries on Rule administration in drools if you could help please -

1. A naming convention for rules needs to be established in order to allow organized and efficient access to rules
2. A rule library or rule administration tool needs to be provided that allows for filtering,searching and browsing available rules. The tool should contain information on the rule type, the rule version as well as a clear description of rule functionality and required parameters.
3. All rules shall include a descriptive field that describes the function or functionality of the rule using text. For tested and implemented rules a full documentation of the rule functionality needs to be available.
4. Rules in the administration tool should be distinguishable by their implementation status (productive, tested, sandbox only, incomplete)

Tiffany's World said...

Appreciiate you blogging this

Disqus for Code Impossible