24.8. Pet Store Example: Evaluate Agenda Group from PetStore.drl
此内容没有您所选择的语言版本。
24.8. Pet Store Example: Evaluate Agenda Group from PetStore.drl
// Free Fish Food sample when we buy a Gold Fish if we haven't already bought
// Fish Food and don't already have a Fish Food Sample
rule "Free Fish Food Sample"
agenda-group "evaluate"
dialect "mvel"
when
$order : Order()
not ( $p : Product( name == "Fish Food") && Purchase( product == $p ) )
not ( $p : Product( name == "Fish Food Sample") && Purchase( product == $p ) )
exists ( $p : Product( name == "Gold Fish") && Purchase( product == $p ) )
$fishFoodSample : Product( name == "Fish Food Sample" );
then
System.out.println( "Adding free Fish Food Sample to cart" );
purchase = new Purchase($order, $fishFoodSample);
insert( purchase );
$order.addItem( purchase );
end
// Suggest a tank if we have bought more than 5 gold fish and don't already have one
rule "Suggest Tank"
agenda-group "evaluate"
dialect "java"
when
$order : Order()
not ( $p : Product( name == "Fish Tank") && Purchase( product == $p ) )
ArrayList( $total : size > 5 ) from collect( Purchase( product.name == "Gold Fish" ) )
$fishTank : Product( name == "Fish Tank" )
then
requireTank(frame, drools.getWorkingMemory(), $order, $fishTank, $total);
end
// Free Fish Food sample when we buy a Gold Fish if we haven't already bought
// Fish Food and don't already have a Fish Food Sample
rule "Free Fish Food Sample"
agenda-group "evaluate"
dialect "mvel"
when
$order : Order()
not ( $p : Product( name == "Fish Food") && Purchase( product == $p ) )
not ( $p : Product( name == "Fish Food Sample") && Purchase( product == $p ) )
exists ( $p : Product( name == "Gold Fish") && Purchase( product == $p ) )
$fishFoodSample : Product( name == "Fish Food Sample" );
then
System.out.println( "Adding free Fish Food Sample to cart" );
purchase = new Purchase($order, $fishFoodSample);
insert( purchase );
$order.addItem( purchase );
end
// Suggest a tank if we have bought more than 5 gold fish and don't already have one
rule "Suggest Tank"
agenda-group "evaluate"
dialect "java"
when
$order : Order()
not ( $p : Product( name == "Fish Tank") && Purchase( product == $p ) )
ArrayList( $total : size > 5 ) from collect( Purchase( product.name == "Gold Fish" ) )
$fishTank : Product( name == "Fish Tank" )
then
requireTank(frame, drools.getWorkingMemory(), $order, $fishTank, $total);
end
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The rule "Free Fish Food Sample" will only fire if:
The store does not already have any fish food, and
The store does not already have a free fish food sample, and
The store does have a Gold Fish in its order.
The rule "Suggest Tank" will only fire if
The store does notalready have a Fish Tank in its order, and
The store does have more than five Gold Fish Products in its order.
If the rule does fire, it calls the requireTank() function . This shows a Dialog to the user, and adding a Tank to the order and Working Memory if confirmed.
When calling the requireTank() function the rule passes the global frame variable so that the function has a handle to the Swing GUI.
If the rule does fire, it creates a new product (Fish Food Sample), and adds it to the order in Working Memory.