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.

5.5.3 Java Code for an Example Help Desk

We will see another trick for using Drools in this example: creating working memory elements i.e., instances of the Problem POJO class in Java code instead of in a “startup rule” as we did for the blocks world example. The code in this section is also in the DroolsHelpDesk.java source file as is the POJO class definition seen in Section 5.5.1. The static main method in the DroolsHelpDesk class is very similar to the main method in the blocks world example except that here we also call a new method createT estF acts: public static final void mainString[] args throws Exception { load up the rulebase RuleBase ruleBase = readRule; WorkingMemory workingMemory = ruleBase.newStatefulSession; createTestFactsworkingMemory; .. same as the blocks world example .. } We already looked at the utility method readRule in Section 5.4.3 so we will just look at the new method createT estF acts that creates two instance of the POJO class P roblem in working memory: private static void 95 createTestFactsWorkingMemory workingMemory throws Exception { Problem p1 = new Problem101, Problem.REFRIGERATOR; p1.problemType = Problem.NOT_RUNNING; p1.environmentalData = Problem.CIRCUIT_BREAKER_OFF; workingMemory.insertp1; Problem p2 = new Problem101, Problem.TV; p2.problemType = Problem.SMOKING; workingMemory.insertp2; } In this code we created new instances of the class P roblem and set desired at- tributes. We then use the W orkingM emory method insert to add the ojects to the working memory collection that Drools maintains. The output when running this example is reformatted to fit the page width: From rule ’Print all problems’: [Problem: TV problem type: SMOKING environmental data: NONE] From rule ’Print all problems’: [Problem: REFRIGERATOR problem type: NOT_RUNNING environmental data: CIRCUIT_BREAKER_OFF] Unplug appliance to prevent fire danger: [Problem: TV problem type: SMOKING environmental data: NONE] Check for power source: [Problem: REFRIGERATOR problem type: NOT_RUNNING environmental data: CIRCUIT_BREAKER_OFF] The unit is not is not on and the circuit breaker is tripped - check the circuit breaker for this room. 96