We'll now write our first Drools rule. Let's say that we have the Account
bean that has one property called balance
. For every Account
bean whose balance is less than 100, we want to write a message to the standard output:
package droolsbook; rule "basic rule" when Account( balance < 100 ) // condition then System.out.println("Account balance is " + "less than 100"); // consequence end
Code listing 1: Basic rule file (basic.drl)
The rule file mentioned earlier starts with a package name. The package acts as a namespace for rules. The rule names within a package must be unique. This concept is similar to Java's packages (classes within a Java package must have different names). After the package definition comes the rule definition. It starts with the rule name, following with the conditions and consequence sections. rule
, when
, then
, and end
are the Drools keywords. This rule is triggered for every bean instance of type Account
, whose balance is less than...