Getting Started with Drools Expert

I’m trialling expert systems, in order to abstract away some tricky internationalization logic in an IVR application. Drools Expert might be what I need and will hopefully save time, compared with devil-in-the-details DSLs. The idea of a Rules Engine is that business rules are abstracted out of your application. Business rules are likely to change, so ideally they should not be in the source tree. Additionally, rules may be consulted or modified by business users, so ideally would be free from syntactic mess, and should be self-documenting. The idea in Drools is that a domain object (in this case an Applicant for a driving license) is injected into a rule. A rule is specified in a form which looks a bit like pseudo-code. The rule contains knowledge on what should happen to the Applicant depending on the values of its properties (for example if age is greater than 18). As conditions are satisfied, properties may be set on the object (e.g. setting valid to true or false). So when the Applicant has ‘passed through’ the rule, its state will reflect the outcome of the application. I wanted to get a quick example up and running, so I can gradually tweak it towards what I want. But I had some trouble with the Quick Start tutorial in the user guide. Here’s what I did to get a simple unit test passing in IntelliJ 9.

  1. Download the Drools 5 binaries from here
  2. Create a project in IDEA from scratch, with a source folder
  3. Create 3 new directories beneath the module folder: “test”, “drl”, and “lib”. The “test” dir will hold your unit tests, “drl” will hold your rules files, and “lib” is where all your jars will go. In the Module Settings dialogue, set “test” to be test sources.
  4. Copy the necessary jars from the Drools download to “lib”, then create a project library containing them. Initially I made the mistake of starting simple and adding one jar at a time, but it seems Drools really does have a lot of dependencies. So in the end I added drools-api-5.0.1.jar, drools-compiler-5.0.1.jar, drools-core-5.0.1.jar from the root directory, and everything inside lib. We can always remove things such as the hibernate dependencies once everything is working. Also add the “drl” directory we created earlier to the project library - this is so the enclosed rule files will be in the runtime classpath.
  5. Add junit 4 (or higher) to the project lib (I find it more convenient to add junit to my global library, as I use it in all my projects).
  6. This is just an empty test, but ensure it runs to completion without errors. This will prove that there are no glaring issues with dependencies.
  7. At this point, let’s check Drools will even start up before we add any code. In keeping with the tutorial on the JBoss site, create a new package in the “test"directory called com.company.license. Create a class here called TestApplication, and add the following code: http://www.pastie.org/1420478.  This is just an empty test, but ensure it runs to completion without errors. This will prove that there are no glaring issues with dependencies.
  8. Now we have a working Drools engine, let’s add a domain class and a rule. Add the package “com.company.license” to the “src” directory, then add the following class to it: http://www.pastie.org/1420481.  Note that we need a getter for age, because the rule will need to introspect on it.
  9. Now add the rules file to the “drl” directory. We’ll call it licenseApplication.drl: http://www.pastie.org/1420485.  The rule is fairly self-explanatory. It only operates on Applicant types - this is the first constraint. Once this is satisfied, the second constraint is that the Applicant must be 18 or over. If this is true, then $a is bound to the instance, and the ‘then’ branch is executed. This branch mutates the Applicant so valid = false.
  10. At this point we can enhance the test to actually execute the rule. We will create an Application (expected to fail), execute the rule on it and check the valid property has been set to false. Update TestApplicant to the following: http://www.pastie.org/1420491.  The test should pass. Note that after we create the Applicant, we set valid to true. This is just to ensure the rule sets it back to false, otherwise we couldn’t be sure the boolean wasn’t still at its default value of false when it was constructed.

And that’s our first rule. I might post some more if I find Drools useful - so far it looks promising.

arjun - May 2, 2011

thanx