Java Code for Blocks World Example
return ruleBase; }
The Drools class F if oConf lictResolver is not so well named, but a first-in first- out FIFO strategy is like depth first search. The default conflict resolution strategy
favors rules that are eligible to fire from data that has most recently changed.
Since we have already seen the definition of the Java POJO classes used in the rules in Section 5.4.1 the only remaining Java code to look at is in the static main method:
RuleBase ruleBase = readRule; WorkingMemory workingMemory =
ruleBase.newStatefulSession; System.out.println\nInitial Working Memory:\n\n +
workingMemory.toString; Just fire the first setup rule:
workingMemory.fireAllRules1; IteratorFactHandle iter =
workingMemory.iterateFactHandles; while iter.hasNext {
System.out.printlniter.next; }
System.out.println\n\n Before firing rules...; workingMemory.fireAllRules20;
limit 20 cycles System.out.println\n\n After firing rules.;
System.out.println\nFinal Working Memory:\n + workingMemory.toString;
iter = workingMemory.iterateFactHandles; while iter.hasNext {
System.out.printlniter.next; }
For making rule debugging easier I wanted to run the first “start up” rule to define the initial problem facts in working memory, and then print working memory. That is
why I called workingM emory.f ireAllRules 1 to ask the Drools rule engine to
just fire one rule. In the last example we called workingM emory.f ireAllRules with no arguments so the rule engine runs forever as long as there are rules eligible
to fire. After printing the facts in working memory I call the f ireAllRules 20
with a limit of 20 rule firings because blocks world problems can fail to terminate at least the simple rules that I have written for this example often failed to terminate
when I was debugging this example. Limiting the number of rule firings is often a good idea. The output from this example with debug output removed is:
Clearing: remove C from B to table
89
Moving B from A to C Clearing: remove B from C to table
Moving A from table to C Moving C from table to B
Done
Note that this is not the best solution since it has unnecessary steps. If you are interested, here is the output with debug printout showing the facts that enabled
each rule to fire:
[Block_11475926 C on top of: B
supporting: ] [Block_14268353 B
on top of: A supporting: C]
Clearing: remove C from B to table [Block_3739389 B
on top of: A supporting: ]
[Block_15146334 C on top of: table
supporting: ] [Block_2968039 A
on top of: table supporting: B]
Moving B from A to C [Block_8039617 B
on top of: C supporting: ]
[Block_14928573 C on top of: table
supporting: B] Clearing: remove B from C to table
[Block_15379373 A on top of: table
supporting: ] [OldBlockState_10920899 C
on top of: table supporting: ]
[Block_4482425 table on top of:
supporting: A] Moving A from table to C
[Block_13646336 C on top of: table
supporting: ] [Block_11342677 B
on top of: table supporting: ]
[Block_6615024 table on top of:
supporting: C] Moving C from table to B
Done
This printout does not show the printout of all facts before and after running this example.