-
Language:
English
-
Language:
English
24.6. Pet Store Example: Putting Items Into Working Memory from PetStore.drl
// Insert each item in the shopping cart into the Working Memory
rule "Explode Cart"
agenda-group "init"
auto-focus true
salience 10
dialect "java"
when
$order : Order( grossTotal == -1 )
$item : Purchase() from $order.items
then
insert( $item );
kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup( "show items" ).setFocus();
kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup( "evaluate" ).setFocus();
end- The first extract fires first because it has the
auto-focusattribute set totrue. - This rule matches against all orders that do not yet have their
grossTotalcalculated . It loops for each purchase item in that order. Some parts of the "Explode Cart" rule should be familiar: the rule name, the salience (suggesting the order for the rules being fired) and the dialect set tojava. agenda-groupinitdefines the name of the agenda group. In this case, there is only one rule in the group. However, neither the Java code nor a rule consequence sets the focus to this group, and therefore it relies on the next attribute for its chance to fire.auto-focustrueensures that this rule, while being the only rule in the agenda group, can fire whenfireAllRules()is called from the Java code.kcontext....setFocus()sets the focus to theshow itemsandevaluateagenda groups in turn, permitting their rules to fire. In practice, you can loop through all items on the order, inserting them into memory, then firing the other rules after each insert.