background preloader

Drools

Facebook Twitter

Drools - Drools - Business Rules Management System (Java™, Open Source) Drools Rule Templates. Drools 5 sees the addition of a new feature - rule templates.

Drools Rule Templates

Rule templates allow you to set up templates (!) That may then be merged with data. You can think of them as similar to decision tables but far more powerful. With Rule Templates the data is separated from the rule and there are no restrictions on which part of the rule is data-driven. So whilst you can do everything you could do in decision tables you can also do the following:store your data in a database (or any other format)conditionally generate rules based on the values in the datause data for any part of your rules (e.g. condition operator, class name, property name)run different templates over the same dataI find the best way to explain things is via example so over a few posts I will take you through three of the examples in the drools-examples project. Drools complex facts. Ruling with Drools Rule Engine. In a project several years ago I built a rule engine from scratch.

Ruling with Drools Rule Engine

In a recent project, which needed a rule engine, I decided to take different route. I decided to give Drools rule engine from JBOSS a try. It has worked out well so far. In this post, I will share my experience with it. I will use insurance as an example to demonstrate how to use it. Why Rule Engine You should seriously consider rule engine when the following conditions apply.

You have complex business logicBusiness logic changes oftenYou want to provide visibility of the business logic to a wider audience, including business analystsYou want to empower business folks with owning and managing business logic and rules AI Talk Let’s explore a little the AI background of rule engines. When you visit your doctor, what goes on in the doctor’s mind, in my opinion is little bit of forward chaining and backward chaining. Inside a Rule Engine A set of factsA set of rules. Salmon Run: Drools Rules in a Database, Take 2. Quite some time back, I wrote an article describing how I used Drools with database backed rules.

Salmon Run: Drools Rules in a Database, Take 2

Since Drools (version 2) did not support this at the time, I ended up writing some glue code that tied Commons Collections Predicates and Closures into Drools Rule Conditions and Consequences, then built my rules on top of that. It was for a proof of concept project which ultimately never materialized. Over the years, I've had requests for the code backing the article. Fast forward 6 years, and I find myself trying to do something similar for an upcoming project. Time sure flies when you are having fun. What I describe here is a small example I tried out to prove to myself that this will work before putting it into the main application. Rules in Drools (now called JBoss Rules) are written using the "when ${condition} then ${consequence}" pattern. These rules work are substituted into a DRT (Drools Template) file, which is written in MVEL and in my case looks something like this.

Drools Expert User Guide. The Conditional Element from enables users to specify an arbitrary source for data to be matched by LHS patterns.

Drools Expert User Guide

This allows the engine to reason over data not in the Working Memory. The data source could be a sub-field on a bound variable or the results of a method call. It is a powerful construction that allows out of the box integration with other application components and frameworks. One common example is the integration with data retrieved on-demand from databases using hibernate named queries. The expression used to define the object source is any expression that follows regular MVEL syntax. Here is a simple example of reasoning and binding on another pattern sub-field: rule "validate zipcode" when Person( $personAddress : address ) Address( zipcode == "23920W") from $personAddress then // zip code is ok end With all the flexibility from the new expressiveness in the Drools engine you can slice and dice this problem many ways.

There are several ways to address this issue: Chapter 6. Authoring. The next example demonstrates the joint effect of a cell defining the pattern type and the code snippet below it.

Chapter 6. Authoring

This spreadsheet section shows how the Person type declaration spans 2 columns, and thus both constraints will appear as Person(age == ..., type == ...). Since only the field names are present in the snippet, they imply an equality test. In the following example the marker symbol $param is used. The result of this column is the pattern Person(age == "42")). You may have noticed that the marker and the operator "==" are redundant.

The next example illustrates that a trailing insertion marker can be omitted. Here, appending the value from the cell is implied, resulting in Person(age < "42")). You can provide the definition of a binding variable, as in the example below. . Here, the result is c: Cheese(type == "stilton"). A simple construction of an action statement with the insertion of a single value is shown below. Chapter 6. Authoring.