Tuesday, September 14, 2010

State Machines, Business Process, Rules with Drools

I have posted before how to use Drools as a rule engine but Drools as I said is way more than that.

When I developed (4 years ago) my own BPM based on SCXML I was looking for a state machine implementation that would allow me to change business rules with a domain specific language.

Since then I have evaluated Spring Webflow, JBPM and Drools. Spring Webflow is too oriented to web so I rapidly understood that was not what I wanted to make sure I had BPM on my BHUB architecture.

Later on I found JBPM very complete but not simple enough. I wanted something lighter. It is then when a year ago I came into this post. At that point I did a proof of concept and I proposed its use.

Here I am again proposing a year later its use again for a different project. This time with an already released drools eclipse plugin that actually worked out of the box for me.

I had success with a previous version of the plugin but this time I have to say it was really easier. My instructions to debug and develop with Eclipse are still valid. There are some stuff like expression evaluations that I would like to see but so far with variables inspection I would say the team can move. Of course this is only available in the Eclipse platform and so the team will need to use it (and I know how hard it is to switch IDEs)

State Machine

To see how you can implement a state machine using Drools refer to the StateExampleUsingSalience example from the distribution:

1. Download http://download.jboss.org/drools/release/
2. Import the project in eclipse and make it a maven project.
3. Include the below in the pom.xml (For some reason that dependency was forgotten)
4. Put some breakpoints in the "then" section of the org.drools.examples.StateExampleUsingSalience.drl file.
5. Right click on org.drools.examples.StateExampleUsingSalience and use option "Debug as Drools Application"
6. Put the "salience 10" statement in rule "B to D" to see how you force certain state transition to have priority.

Business Process

Business process is all about states. It is a synonym of workflow. A business process outlines the order in which the different components of your business are executed: For example we need to pull data from different feeds, then run a report, make some corrections here and there, send emails, wait for responses and finally run the Executive Report.


I am not going to discuss semantics here but just the idea that any business process, besides states, needs rules that sometimes apply inside states and some times out of the states. Rules are several "if" statements that will affect the state or not but ultimately will have an impact on the output. As a programmer we program rules in backend, middle tier, front end and a mix of them. It would be ideal if rules could be reused but also if they are externalized so someone with domain logic could play with them. A rules engine allows to use Domain Specific Languages (DSL) to separate even further the application concerns. Rules can be applied to any of the three components of the MVC pattern even though you will find it more related to the M and C components.


If a Rules engine is ok or not for your project is a very important question. Martin Fowler has written an article about rules engines and their drawbacks. You must be sure you understand where you are heading because your project might become easily unstable and unmanageable.

A lot of discussions are going back and forth in the community about rules or process centric solutions. Drools advocates to mix the concepts for more real implementations and I cannot agree more. I personally like SCXML because of the parallelism (start here to learn about Harel State charts).

In any case so far Drools has the simplicity I look for and I think the team will be happier to use it if I compare it to an in house business process and rules engine based on SCXML, especially because nowadays is "in vogue" to move away from XML ;-)



No comments: