Information Technology Reference
In-Depth Information
The Drools rule engine
Drools is a rule engine framework that provides the possibility of creating rules in a script
language called Drools Rule Language ( DRL ) and running the rules inside an in-memory
inference engine that provides great performance. Rule languages have a declarative
nature, which means that the next action to be taken is determined by the input data that
triggers specific conditions in the rules, unlike the imperative nature of languages such as
Java, where the next action to be taken is determined by the sequence of actions written in
the code.
Rules written in DRL follow a very specific syntax for constraints and a configurable syn-
tax for the actions of the rule (which we will keep as plain Java for simplicity). The con-
straint part of the rule shouldn't be interpreted as regular imperative code such as a Java
code, where we specifically tell the system what to do at a particular point in time. Instead,
it should be considered as a declarative statement, similar to a SQL query, where we will
search anything that matches a specific criterion. For every match, we will execute the con-
sequence of the rule (the "then" part of the rule). The structure of a DRL rule is similar to
the following code:
rule "prioritize requirements with lots of bugs"
lock-on-active
when
r: Requirement(bugs.size() > 3)
then
r.setPriority(20 / r.getBugs().size());
update(r);
end
In the preceding rule, we searched for all Requirement objects that have a bugs list with
more than three elements. For each one of them, we set the priority to a calculated value
and update the reference in the rule engine memory to evaluate other possible rules. The
lock-on-active attribute is there to make sure we don't re-evaluate the same rule for
the same object after we update it.
The power of rules grows as we add more rules. The internal algorithm of the rule engine
(called PHREAK on the current version) will optimize the structure of all rules and make
sure that all added objects evaluate rules in the shortest execution path possible. After-
Search WWH ::




Custom Search