You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
6. Anatomy of a <strong>flex</strong> Program 58<br />
<strong>flex</strong> toolkit<br />
This should succeed, with the given rule firing once only (because after that<br />
the viewer's tickets required will be zero).<br />
Displaying Results<br />
Unfortunately, at the moment we have no way of knowing if it ran correctly,<br />
i.e. if 3 seats were subtracted from the cinema's total seats. We will add<br />
another action to simply write out some relevant values, called<br />
write_values.<br />
Here we will use the KSL operator `s instead of of for accessing the slots<br />
of frames.<br />
action write_values ;<br />
write( 'Cinema seats: ' ) and<br />
write( cinema`s seats ) and<br />
tab( 2 ) and<br />
write( 'Viewer tickets required: ' ) and<br />
write( viewer`s tickets_required ) and<br />
nl.<br />
Note the use of the built-in Prolog predicates write/1, tab/1 and nl/0<br />
to write text, spaces and a new line. Any Prolog predicate may be called in<br />
this way from <strong>flex</strong>; its arguments will be dereferenced by the <strong>flex</strong> interpreter<br />
before executing (so that, for example, the term viewer`s<br />
tickets_required will be dereferenced to the current value of the slot<br />
tickets_required of the frame viewer).<br />
By writing out the values before and after running <strong>flex</strong>, we may see that the<br />
operation has been done correctly.<br />
?- restart, write_values, go, write_values.<br />
Cinema seats: 500 Viewer tickets required: 3<br />
Cinema seats: 497 Viewer tickets required: 0<br />
Note that restart/0 is a built-in <strong>flex</strong> predicate which resets slot values<br />
back to their original values.<br />
Extending the Program<br />
In reality there would be more than one cinema and more than one viewer,<br />
(so we would probably uses instances), and the forward chaining engine<br />
would continue until there were no more viewers with<br />
tickets_required values greater than 0. There would also need to be a<br />
check that the number of tickets required was less than the number of seats<br />
in the cinema.<br />
rule allocate_tickets<br />
if the viewer`s tickets_required is greater than 0<br />
and the cinema`s seats is greater than or equal to<br />
the viewer`s tickets_required<br />
then the seats of the cinema becomes<br />
the seats of the cinema