Simple state machine
framework (SMS) tutorial
Run-time changes to SM behavior
definitions
State machine behavior definition API
SMS is software framework that allows creation, execution and debugging of finate state machines (SMs). Background information about state machines and their visual representation – statechart can be found in the following articles: Harel David, "Statecharts: A Visual Formalism for Complex Systems", UML statecharts.
In few words state machines allow modeling of an object behavior by defining set of object states, set of events handled by the object and set of state transitions to be performed during handling of the events. Statecharts are visual representations of the state machines. The following is the statechart generated by SMS debugger for state machine from the SMS usage example:
The above diagram represents:
1. Three states: initState, demoViewClosedState and demoViewProvidedState
2. Three events: SayEvent, ViewProvisioningEvent and ViewClosedEvent
3. State transitions associated with the states and events:
a. initState to demoViewProvidedState transition on the arrival of ViewProvisioningEvent, method transition() of ViewProvisioningTrans class will be executed during this state transition
b. initState to initState transition on the arrival of SayEvent, method transition() of TooEarlyTrans class will be executed during this state transition
c. demoViewProvidedState to demoViewClosedState transition on the arrival of ViewClosedEvent, there is not any transition specific logic associated with this state transition
d. demoViewClosedState to demoViewProvidedState transition on the arrival of ViewProvisioningEvent, method transition() of ViewProvisioningTrans class will be executed during this state transition
e. demoViewClosedState to demoViewClosedState transition on the arrival of SayEvent, method transition() of TooLateTrans class will be executed during this state transition
4.
Icon in demoViewProvidedState means that this state
is composite. Icon
means that this state has entry state transition.
SMS provides:
· State machine(SM) behavior (state, events, transitions) definition API. This API allows application code to define SM states, events and state transitions
· SM scheduling API. This API allows applications to post events to SMs or call SM directly
· SMS debugger. The debugger provides visual representation of SMs runtime information such as generated statechart, current state, handled events and preformed transitions.
· Event handling according to the currently defined states, events, transitions and current state
· Predefined new state calculation according SM definitions can be overridden by the transition code
· Any state can become composite state and have substates. During event handling a state will attempt to pass event to its substates, if not consumed will attempt to handle it with its own transitions. Composite state implements deep history that may be overridden with entry transition.
· Entry/exit transitions can be defined for any state. A state can have more then one entry/exit transitions. Entry transitions performed every time the state becomes current. Exit transitions performed every time the state replaced by another state as the current state. Substate entry/exit transitions performed as in the case of regular states besides the following two cases:
o Entry transition of a substate is not performed in case its composite state becomes current and the substate becomes current as the results of the deep history mechanism
o Exit transition of a substate is preformed if its composite state stops to be current
It is possible to define inheritance relation between SMs. It is done by defining inheritance between parent and derived SM classes. Derived SM inherits all the behavior definitions of the parent SM. Derived SM can override the following definitions:
· Add new states and event and transitions on these states
· Add new events and transitions to derived states
· Overload transition logic of the derived transitions on the derived states
· Overload new state definitions for the derived transitions
· Extend the logic of the derived transitions
SM behavior definitions and scheduling API are thread-safe. It is possible to update SM behavior definitions by SM transitions or external application code during run-time and during event handling. The changes will be immediately reflected in the SM event handling and in the SMS debugger generated statecharts.
ANT in order to build SMS and SMSDemo.
XMLParser - crimson.jar
included in the SMS package
The following simple application SMSDemo illustrates the usage of SMS API.
SMSDemo source code available at SMSDemo.zip.
In order to run it use runDemo.bat, in order to build use
The application consists of:
· SMSDemo GUI controller – allows interaction with the application and illustrates the usage of “State machine scheduling API”. Resides in SMSDemoController.java.
· SMSDemo GUI view – provides GUI feedback Resides in SMSDemoView.java.
· SMSDemo model – binds all the components together. Resides in SMSDemoModel.java.
· SMSDemo state machine – implements the logic of the application and illustrates the usage of “State machine behavior definition API”. Resides in SMSDemoSM.java.
The following classes implement demo state machine:
· SMSDemoSM is the main state machine class. It uses SimpleStateMachine API in order to create states, transitions and events. SMSDemoSM inherits from SimpleStateMachine, alternatively state machine may hold internal copy of SimpleStateMachine instance and delegate to it.
o ViewProvisioningEvent, ViewClosedEvent, DayTimeEvent, NightTimeEvent, SayEvent are classes implementing events handled by SM. All these classes inherit from SMEvent, all SM events should inherit from this class.
o SMStates is the enumeration that declares SM states
o DemoViewProvidedStates is the enumeration that defines demoViewProvidedState substates
o ViewProvisioningTrans, TooEarlyTrans, TooLateTrans, DaySayTrans, NightSayTrans are classes implementing transition actions. All state transition actions should implement StateTransition interface.
o SMSDemoSM() constructor implements the following SM init states:
§ defines SM and demoViewProvidedState behavior with setSMMatrix method
§ adds demoViewProvidedState entry transition that will set demoViewProvidedState initial substate
§ sets machine initial state
The controller uses SMS scheduler to post events to the machine, the following few lines illustrate the usage:
model.scheduler.postEvent(
model.demoSM,
new SMSDemoSM.ViewProvisioningEvent(demoView)
);
SMS JavaDoc API documentation can be found at index.html
SimpleStateMachineIF, SMStateIF interfaces provide SM behavior definition API
SMScheduler provides SM scheduling API
SMS debugger provides visual representation of the running SMs in the form of their statecharts with the indication of current states and handled events.
In order to run SMS debugger Java system property SMSDebuggerClass has to be set to com.taserv.SMSDebug.SMSDebugger
SM navigator window will popup with the creation of the first SM if SMS debugger started as described above. SM navigator lists all the created SMs. In order to open SM statechart double click on SM name in SM navigator list.
The statechart includes all the defined states, event and transitions. It is possible to change statechart layout by dragging states and transition connection points. It is possible to toggle on/off events and transition names with View menu.
The layout saved automatically when statechart window is closed. The user adjusted layout will be restored next time the statechart will open.
icon means the
state is composite, double clicking on the state will open its statechart
icon means the state has defined entry
transitions
icon means the state has defined exit
transitions
Copyright © 2004 TAServ. All Rights Reserved.