What are JBoss Rules?
JBoss Rules (the new brand for the Drools rules engine project, now at version 3.0.5) provides an open source and standards-based business rule engine using an enhanced implementation named ReteOO ( RETE adaptation for an object-oriented language), a descendant of the well-known RETE algorithm. As an alternative to ReteOO, JBoss Rules offers a hybrid, experimental version of the LEAP algorithm.
What are JBoss Rules?
JBoss Rules is a production rule system. A JBoss production rule is a two part structure with a condition part (the
when part) and an action part (the
then part). A simple template of a JBoss production rule is:
rule "rule_name" [attribute][value] when conditions then conclusions end
Let's look at the following rule from Product Derby Case Study:
If the driver is male and is under the age of 25, then he is a young driver.
The JBoss code of the rule is:
package org.productderby.rules; import org.productderby.concepts.Driver; rule "Young_Driver" when driver:Driver(age < 25, gender == GenderType.MALE) then driver.setDriverType(DriverType.YOUNG); modify(driver); end
JBoss Rules are built on top of vocabularies implemented as Java beans. The UML vocabulary of this rule is:
A collection of rules (i.e. the "Young_Driver" rule), together with their necessary vocabulary (i.e.
import org.productderby.concepts.Driver; ) define a JBoss Rules package.
The central concept for rule conditions is the Column. The conditions part (LHS) of a JBoss rule is a list (conjunction) of 'columns'. A column is used to indicate Field Constraints (e.g.
age < 25 ) on JBoss Rules Facts. Facts are instances of Java beans stored in the Working Memory such that, internally, the beans attributes values are obtained by calling the corresponding accessors (getters). We can also modify the value of a Field Constraint using the corresponding mutators(setters) (e.g.
driver.setDriverType(DriverType.YOUNG); ) in the action part (RHS) of the rule.
JBoss Rules columns allow the following types of field constraints: literalConstraint (numeric, date, string, boolean, enum, regexp), boundVariableConstraint, returnValueConstraint and predicateConstraint.
The "Young_Driver" rule has a single column:
driver:Driver(age < 25, gender == GenderType.MALE). It consists of two literal constraints used to restrict the fact matching from Working Memory (instances of Driver class) and a variable declaration (
driver is an object variable of type
First literal constraint age and gender match just drivers with
age less than 25 years. The second Field Constraint
gender == GenderType.MALE is an enumeration literal constraint matching all drivers which are male. The entire column match with drivers that satisfy simultaneously both constraints. This corresponds to a conjunction of constraints i.e. our colum is equivalent with:
driver:Driver(age < 25); driver:Driver(gender == GenderType.MALE);
The action part (RHS) of a JBoss rule consists of a block of any valid Java code. Its purpose is to to invoke specific state-changing actions including retract, modify and assert of facts to working memory. The action part of the "Young_Driver" rule updates the
driverType attribute of the
driver object i.e.
driver.setDriverType(DriverType.YOUNG); and then notifies the rule engine of the modified facts (the call of the
modify()). Internally, the notification corresponds to a retract of the old fact followed by an assert with the new fact.
Notice that actually, the action part of a JBoss rule comprises only Java code, but soon is expected to support other languages like Groovy or C#.