Storing and managing Rules in a database
(note that this example uses the Drools SPI API - which will be changing significantly with version 3 - but it shows how you can use custom front ends to drive the rule engine, in particular using a dynamic language like python).

Thanks to Sujit Pal for this article (sujit.pal@comcast.net) and the contribution.

(Note that this is being adapted into a rule repository for Drools 3, adding in versioning, history, searching etc.)

Introduction

        http://localhost:8080/pluto/list.html

Database interface usage examples

      
        DBClient client = new DBClient();
        Map results = client.execute("MyRuleSet", context);
      

List names = client.getAllRuleSets();

 

 

 

 

RuleSet ruleset = client.getRuleSetByName("MyRuleSet");

 

 

 

 

       

public class MyCondition extends ConditionSupport {


            public MyCondition(Rule rule) {
                super(rule);
            }


            /**
             * There should be something called "data" in the Context
             */
            public void setData(Data data) {
                this.data = data;
            }


            public boolean doIsAllowed() throws ConditionException {
                // do something with data
                return someBoolean; // a true if condition passed, else false
            }
        }

 

 

 

 

      

public class MyConsequence extends ConsequenceSupport {


            public MyConsequence(Rule rule) {
                super(rule);
            }


            public Data getData() {
                return data;
            }


            public void setData(Data data) {
                this.data = data;
            }


            // other getter and/or setters


            public boolean doReassertContext() {
                // override only if the consequence taints the working
                // memory so the facts need to be re-asserted.
                return false;
            }


            public void doInvoke() throws ConsequenceException {
                // do something with data
            }
        }

 

 

 

 

      

Condition condition = new DynamicPythonCondition(rule);
        condition.setScriptText("cart.getState() == \"Exploded\"");
        condition.setContext(context);

 

 

 

 

Consequence consequence = new DynamicPythonConsequence(rule);
        consequence.setScriptText("cart.setState(\"Exploded\")");
        consequence.setTaintMode(false);
        consequence.setContext(context);

 

 

 

  Web interface usage examples

 

       

 

       Expanding a rule for viewing or editing - Clicking on the Expand link next to each Rule will bring up a detail view of the Rule. Here is a screenshot:
        

      Adding a rule to a ruleset - Clicking on the Add New Rule link at the bottom of the edit screen brings up a form to add a new Rule to the ruleset. Here is a screenshot:
        

      Editing a rule - Clicking the Edit link in the expanded Rule view will allow you to edit a Rule. Here is a screenshot:
        

 

           The web interface allows an easy way to manage rulesets in the database, in some cases it is less intimidating than writing DRL files in an editor.

           The database interface takes away much of the complexity (and tunability) of understanding and writing code against the Drools API.

           The web interface currently does not support versioning of rulesets, or locking rulesets while they are being modified, but this should be fairly easy to do if needed.

      Many thanks to Mark Proctor and the Drools team for the software.
      Many thanks to Michael Neale for his support during the writing of this article.

The code for described in this article can be downloaded from here.