Object Machine Demo Program Introduction: The Object Machine Demo program (OBJMCH.EXE) demonstrates a simple finite- state machine (FSM). The FSM monitors mouse button events (Up/Down) and produces "Click" and "Double Click" messages at the appropriate transitions. It models the method that Visual Basic uses to manage "Click" and "Double Click" events. (It is interesting to note that different Window's apps do not all recognize mouse clicks in a uniform way.) OBJMCH draws a state diagram and then monitors mouse button events. As mouse events are recognized the program changes state colors indicating the current state of the FSM (see color codes below). When a click or double click message transition occurs the FSM generates a message as a pop-up message box that disappears after a moment. OBJMCH uses an exaggerated time scale so that the state transitions occur slow enough to watch. The user can adjust the timeout duration to slow down the FSM or to speed it up (see timer operation below). Operation: OBJMCH.EXE requires the Visual Basic run-time DLL VBRUN100.DLL (It was not bundled with the program to save down-load time.) Copy this DLL to either c:\windows\system or to the home directory of OBJMCH.EXE With VRUN100.DLL installed, launch OBJMCH.EXE from Windows. The program draws a state diagram. You can resize the program's window, however, at small scales the diagram will appear cluttered. Notice that the Startup state is green indicating that it is the current state. Also, the initial state transition is blue indicating the path to the Startup state. Move the mouse cursor anywhere over the program's state diagram and press any mouse button down (hold it down). Observe the state transition that occurred from the Startup state to state FirstDown. Hold the button down for two seconds and the FSM will transition to state LongDown. Let the button up and the FSM returns to the Startup state. Now press a mouse button down and let it up. This causes two state transitions ending up at state OneUp and producing a Click message. (The click message disappears after a brief moment.) Pressing a mouse button down again before time expires produces a Double Click message. The transition from state Startup to FirstDown starts a timer which counts down in a window at the bottom of the diagram. If you study the Click and Double Click events produced by Visual Basic you will see that this state diagram models the method that Visual Basic uses to generate these events. Note that there are many variations possible. Test yourself with the following questions: (You may find these questions trivial, however, they help to emphasize operational subtleties that can be difficult to specify without a state diagram). 1) How would you change the state diagram so that there is no Click message generated before a Double Click message? (Of course, there must still be a Click message generated when no Double Click occurs.) 2) Visual Basic doesn't recognize a click until the mouse button goes up. What changes are necessary to recognize a click when the mouse button first goes down? 3) Can you draw a state diagram to recognize the Morse code message "SOS" ( . . . - - - . . . )? How about a state diagram to decipher any Morse code message? State Diagram Color Codes: States and state transitions are color coded to indicate the current FSM state. The previous state is colored red, the current state is green, and the transition between them is colored blue. All other states and transition are colored grey. Timer Operation: The FSM uses a timer to distinquish two clicks from a double click. The timer duration can be set by the user. It defaults to two seconds. Making the timeout duration longer gives the user more time to recognize a double click. For More Information: For more information about finite-state machines (FSM) and applying FSMs to C++ objects, send your name and address plus $5 for handling to: The Aerosoft Company 51 N. Cromwell Fairfield, IA 52556 Attention: Object Machines Request article, "Object Machines". This article describes the theory of FSMs and discusses their application to object-oriented programming. It includes C++ examples. You can mail comments or questions to the above address or send them via CompuServe at 72260,3551.