Thursday, May 9, 2013

Drools

Rule engines provide us a declarative language to express our rules, in contrast to the imperative nature of languages. Java, C, PHP, Python, and Cobol are imperative languages, meaning that they follow the instructions that we give them, one after the other.

Using Drools Rule Language(DRL) we specify situations that are evaluated by the rule engine. Each rule defines a situation. A rule has two sections,

conditional section => Starts when 'when' keyword
consequence section => Starts when 'then' keyword

A rule that is activated is said to be eligible to be fired.

In the rule consequence, you can write any Java code you want. This code will be executed as regular Java code.

Rule engine doesn't care about the order of the rules that we provide; it will analyze them by their conditional sections

rule "enabled to drive must have a car"
When
$p: Person( enabledToDrive == true )
not(Car(person == $p))
then
insert(new Car($p));
end

rule "person with new car must be happy"
when
$p: Person()
$c: Car(person == $p)
then
$p.setHappy(true);
end

No comments:

Post a Comment