Drools Rules for an Example Help Desk
more comfortable for me. As in the blocks world example, we want to place the rules file in the same package
as the Java code using the rules file and import any POJO classes that we will use in working memory:
package com.markwatson.examples.drool import com.markwatson.examples.drool.
DroolsHelpDesk.Problem; The first rule sets a higher than default rule salience so it will fire before any rules
with the default rule salience a value of zero. This rule has a feature that we have not seen before: I have no matching expressions in the “when” clause. All Java
Problem instances will match the left-hand side of this rule.
rule Print all problems salience 100
when p : Problem
then System.out.printlnFrom rule ’Print all problems’:
+ p;
end The following rule matches an instance of the class P roblem in working memory
that has a value of “Problem.CIRCUIT BREAKER OFF” for the vakue of attribute environmentalData. This constant has the integer value of 1002 but is is obvi-
ously more clear to use meaningful constant names:
rule Reset circuit breaker when
p1 : ProblemenvironmentalData == Problem.CIRCUIT_BREAKER_OFF
then System.out.printlnReset circuit breaker: + p1;
end The last rule could perhaps be improved by having it only fire if any appliance was
not currently running; we make this check in the next rule. Notice that in the next rule we are matching different attributes problemT ype and environmentalData
and it does not matter if these attributes match in a single working memory element or two different working memory elements:
94
rule Check for reset circuit breaker when
p1 : ProblemproblemType == Problem.NOT_RUNNING ProblemenvironmentalData ==
Problem.CIRCUIT_BREAKER_OFF then
System.out.printlnCheck for power source: + p1 + . The unit is not is not on and +
the circuit breaker is tripped - check + the circuit breaker for this room.;
end We will look at the Java code to use these example rules in the next section.