(stdin)

    1	#!/usr/bin/env tclsh
    2	
    3	package require moore
    4	
    5	# The model command specifies the characterististics of the state model
    6	moore model wmachine {
    7	    # The state model is defined by executing commands to specify the
    8	    # states and transitions of the model.
    9	
   10	    # The "State" command defines a state and the Tcl code that is run when the
   11	    # state is entered.
   12	    State Idle {} {
   13	        # Stop motor
   14	        puts "$self: Stopping motor"
   15	
   16	        # For testing purpose, we want to be able to run the Tcl event loop and
   17	        # when we are done with the cycle we will use "vwait" to determine that
   18	        # we have finished.
   19	        set ::wmachine($self) Idle
   20	    }
   21	
   22	    # The "Transition" command defines what happens when an event is received
   23	    # in a state. Notice that there is no command to define the events to which
   24	    # the model responds. The events are simply gleened from the "Transition"
   25	    # commands. The "-" and "->" arguments are simply syntactic sugar to
   26	    # suggest the transition from current state to new state that the event
   27	    # causes.
   28	    Transition Idle - Run -> FillingForWashing
   29	
   30	    State FillingForWashing {} {
   31	        # Open water valve
   32	        puts "$self: Opening water valve"
   33	
   34	        # At this point we need the washing tub sensor to tell us that the
   35	        # tub is full. For simplicity, we will simulate the sensor by
   36	        # means of the Tcl "after" command.
   37	        # SIMULATING THE TUB SENSOR
   38	        after 5000 [list moore generate {} - Full -> $self]
   39	    }
   40	    Transition FillingForWashing - Full -> Agitating
   41	
   42	    State Agitating {} {
   43	        # Close water value
   44	        puts "$self: Closing water valve"
   45	        # Set motor to agitate
   46	        puts "$self: Setting motor to agitate"
   47	        # Generate "Done" to self delayed by the wash time.
   48	        moore delay [$self cget wash] $self - Done -> $self
   49	    }
   50	    Transition Agitating - Done -> EmptyingWashWater
   51	
   52	    State EmptyingWashWater {} {
   53	        # Stop motor
   54	        puts "$self: Stopping motor"
   55	        # Start pump
   56	        puts "$self: Starting pump"
   57	
   58	        # SIMULATING THE TUB SENSOR
   59	        after 5000 [list moore generate {} - Empty -> $self]
   60	    }
   61	    Transition EmptyingWashWater - Empty -> FillingForRinse
   62	
   63	    State FillingForRinse {} {
   64	        # Stop pump
   65	        puts "$self: Stopping pump"
   66	        # Open water valve
   67	        puts "$self: Opening water valve"
   68	
   69	
   70	        # SIMULATING THE TUB SENSOR
   71	        after 5000 [list moore generate {} - Full -> $self]
   72	    }
   73	    Transition FillingForRinse - Full -> Rinsing
   74	
   75	    State Rinsing {} {
   76	        # Close water valve
   77	        puts "$self: Closing water valve"
   78	        # Set motor to agitate
   79	        puts "$self: Setting motor to agitate"
   80	        # Generate Done to self delayed by the rinse time
   81	        moore delay [$self cget rinse] $self - Done -> $self
   82	    }
   83	    Transition Rinsing - Done -> EmptyingRinseWater
   84	
   85	    State EmptyingRinseWater {} {
   86	        # Stop motor
   87	        puts "$self: Stopping motor"
   88	        # Start pump
   89	        puts "$self: Starting pump"
   90	
   91	        # SIMULATING THE TUB SENSOR
   92	        after 5000 [list moore generate {} - Empty -> $self]
   93	    }
   94	    Transition EmptyingRinseWater - Empty -> Spinning
   95	
   96	    State Spinning {} {
   97	        # Stop pump
   98	        puts "$self: Stopping pump"
   99	        # Set motor to spin
  100	        puts "$self: Setting motor to spin"
  101	        # Generate Done to self delayed by the spin time
  102	        moore delay [$self cget spin] $self - Done -> $self
  103	    }
  104	    Transition Spinning - Done -> Idle
  105	}
  106	
  107	# Draw the "as implemented" model
  108	wmachine draw {-Tsvg -o%s.svg -Gsize=7.5,10}
  109	# Create a state machine from the model
  110	wmachine machine wm1
  111	# Set up the parameters for the state machine.
  112	wm1 configure wash 3000 rinse 3000 spin 4000
  113	# Turn on tracing
  114	wm1 configure trace true
  115	moore::log::setlevel info
  116	# Kick things off
  117	moore generate {} - Run -> wm1
  118	# Wait until the cycle is complete
  119	vwait ::wmachine(::wm1)
  120	# Clean up
  121	wm1 destroy

Generated by GNU Enscript 1.6.5.90.