| iMatix home page
| Libero home page | Libero documentation
| << | < | > | >>
Libero Libero
Version 2.32

The Coke Machine Example

The coke machine (once awake and gently humming) sits against the wall, waiting. There is a theory about this humming business, notably that coke machines have terrible memories, and can't remember the words. A second school of thought says that the coke machines are humming because they're thinking about all the terrible things they're going to do to humans with crow bars. A third school of thought reckons that the coke machines have already done something, and are humming because they thought it was really funny. Like all coke machines, it's very good at waiting, due to Natural Selection. All the impatient proto-coke machines ran off screaming 'I can't take it anymore' and were crunched underfoot by brontosaurii back in the mists of prehistory.

A punter arrives with a handful of loose change. The coke-machine (covered by the modern and tasteful metal grid that serves both as a defensive covering and as an attraction for coke machines of the opposite sex) moves into 'attention' mode. When it hears the clink of tasty cash, rather that the ominous clunk of a heavy crow-bar, it relaxes into 'ready mode'. The machine is now ready to handle any eventuality, so long as it includes coinage.

As the last coin falls and the punter presses the button labelled 'healthie waye sparkling spring water', the machine considers the possibilities. Should it play dead, and just take the punter's money? Or maybe it can substitute a can of 30% sucrose 'sweeto-sap'? Finally it rumbles once, then again for added effect, then excretes a can of water. It munches the coins, then settles back against the wall, quietly humming again.

Just to prove that you can describe any sequence of events, however improbable, with a state machine, here goes. (The observant reader will have skipped to the next section by now, realising that this one is not worth the paper it's printed on. If you're reading this on-line, then this section is not worth the phosphor it's been digitised, rasterised, and sprayed by an electronic beam gun onto.) Act 1, scene 1 has our protagonist against the wall, trying to remember the words of the song. Our coke-machine hero is normally OK; if something went terribly wrong with the rehearsals (e.g. a dinosaur got sucked through a five-dimensional space-time vortex stretched between the middle terrasic to five minutes into rehersals; the dinosaur a little off balance after the trip puts down its left foot, and 'crunch'...), it might be Dead, in which case we have to call the whole thing off. We'll call this scene 'Should Be Gently Humming'...

Should-Be-Gently-Humming:
    (--) Ok                         -> Something-Happened
          + Wait-For-A-Punter
    (--) Dead                       ->
          + Stop-The-Programme

The coke machine has been waiting, perhaps for days, for a punter to come along. When the footprints fade in from the distance (a coke machine's ears are always against the ground, which explains why you rarely find coke machines on wet or dirty surfaces), the machine hears either a cashy 'clink' or a more sinister 'clunk':

Something-Happened:
    (--) Clink                      -> Before-Cooperating
          + Accept-Punter's-Cash
          + Wait-For-Punter's-Choice
          + Shall-We-Cooperate?
    (--) Clunk                      ->
          + Exit-Stage-Left-Running
          + Stop-The-Programme

The advent of a 'Clunk' tells the machine to beat it, smartly. If a 'Clink' happened, the machine goes on to think about a game plan for the coming few minutes. It accepts whatever spare change the punter has to spare, then pounces, metaphorically:

Before-Cooperating:
    (--) Ok                         -> Cooperate
          + Consider-Punter's-Choice
    (--) Nasty                      -> Let's-Be-Nasty
          + Consider-Punter's-Choice
    (--) Play-Dead                  -> Something-Happened
          + Switch-Off-All-Lights
          + Wait-Until-Punter-Has-Left
          + Switch-On-The-Lights
          + Wait-For-A-Punter

If it's in a good mood, the machine ejects more or less the drink that the punter chose:

Cooperate:
    (--) Coke                       -> Something-Happened
          + Eject-Can-Of-Coke
          + Wait-For-A-Punter
    (--) Spring-Water               -> Something-Happened
          + Eject-Can-Of-Spring-Water
          + Wait-For-A-Punter
    (--) Juice                      -> Something-Happened
          + Eject-Can-Of-Juice
          + Wait-For-A-Punter
    (--) Sweeto-Sap                 -> Something-Happened
          + Eject-Can-Of-Sweeto-Sap
          + Wait-For-A-Punter
    (--) Empty                      ->
          + Switch-Off-All-Lights
          + Stop-The-Programme

If the coke machine's pretty weak internal moral system failed, it supplies the opposite of whatever the punter wanted:

Let's-Be-Nasty:
    (--) Coke                       -> Something-Happened
          + Eject-Can-Of-Juice
          + Wait-For-A-Punter
    (--) Spring-Water               -> Something-Happened
          + Eject-Can-Of-Sweeto-Sap
          + Wait-For-A-Punter
    (--) Juice                      -> Something-Happened
          + Eject-Can-Of-Coke
          + Wait-For-A-Punter
    (--) Sweeto-Sap                 -> Something-Happened
          + Eject-Can-Of-Spring-Water
          + Wait-For-A-Punter
    (--) Empty                      ->
          + Switch-Off-All-Lights
          + Stop-The-Programme

This diagram is a little complex for a first example. Remember however, that we are modelling a rich behaviour pattern that took several hundreds of millions of years to evolve. Real software is usually developed much faster, and should therefore be much simpler, only it isn't.


| << | < | > | >>
| Introduction to Libero | The Coke Machine Example | Example of Using a Telephone | Example of Controlling a Telephone | Source Code For Phone.c | Example of a C/C++ Comment Stripper | Example of Parsing An Arithmetic Expression | Dialogs For Dummies | Frequently Asked Questions
iMatix
Copyright © 1996-97 iMatix