You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
11. Example – Robbie Goes Shopping 163<br />
required. The following frame is used as a mould for generating new carrier<br />
bags.<br />
frame carrier<br />
default contents are empty .<br />
default contents_count is 0 .<br />
demon contents_counter<br />
when the contents of Bag changes<br />
and Bag is an instance of carrier<br />
then add 1 to the contents_count of Bag .<br />
This demon will be activated whenever we change the contents of any<br />
carrier.<br />
Packing an item can be thought of as follows.<br />
To pack an item, first choose a carrier bag for the item, then remove the<br />
item from the shopping list and add the item to the contents of the bag.<br />
action pack_item( Item );<br />
if choose_bag( Bag, Item )<br />
and remove the Item from the shopping<br />
and include the Item in the contents of the Bag<br />
and write( 'Packing ' ) and write( Item )<br />
and write( ' into ' ) and write( Bag ) and nl .<br />
We now need to establish how to choose a carrier bag for an item. This may<br />
be done using a backward chaining relation, which can be thought of as<br />
follows.<br />
To choose a bag for an item, get an existing carrier bag, and check that<br />
the number of items already in the bag is less than the maximum<br />
allowed for the item about to be packed.<br />
The first clause of the choose_bag relation is:<br />
relation choose_bag( Bag, Item )<br />
if the Bag is some carrier<br />
whose contents_count is less than<br />
the maximum_allowed of Item .<br />
Whenever a carrier bag is needed it is always better to check existing bags<br />
(indicated by the keywords Bag is some carrier) if at all possible.<br />
The second clause of the choose_bag relation is<br />
relation choose_bag( Bag, Item )<br />
if gensym( bag_number_ , Bag )<br />
and Bag is a new carrier .<br />
Note that gensym/2 is a built-in predicate of the underlying Prolog system<br />
for generating unique names from a given root symbol (bag_number_ in<br />
<strong>flex</strong> toolkit