UML Discussion on State Machines
Perfectly static system is intensely uninteresting Because nothing ever happens
Real Systems Dynamic in nature and always something is happening internally or externally. ATM Machine. Robot. Chemical Reaction. Network Router.
Objects Objects work together to realize higher-order behaviors defined by use cases. Object Behavior with respect to time Simple State Continuous
What is State Machine Its abstract It has finite state of conditions of existence called states It has set of actions performed in each state It has set of events which causes state change to well defined rule set.
State Machine Example Possible States RED YELLOW GREEN Flashing Yellow Flashing Red …
What things exhibit state ? Classes Use cases State Machine can be applied to object oriented systems as well as functionally decomposed ones.
Behavioral Elements
State Transition State Machine Guard Event
State Machine Using state machine you can Model behavior of individual object. State Machine illustrates sequential behavior of object. Statechart Diagrams. State to State. Activity Diagrams. Activity to Activity.
Example Payment Authorization Authorizing Authorized Delivered Rejected
Web Application Not yet logged in. Action: Display the login page Logged in and viewing the account balance Display the account balance Display logout button, and view transaction button. Logged in and viewing recent transactions Display recent transactions Display logout button, and view account balance button. Logged out Display “Goodbye page”
Validate Login/PIN
Word Processing
Brew Controller of coffee machine
Coffee Machine Tank
Sequential Sub states
Concurrent Substates
Food For Thought…. Identify States
Hypothetical State Machine Event Queue When event is received it is placed in queue of target Event Dispatcher When delivered to state machine Event Processor Consumed and event is complete
State Machine Concepts Context An association to the model element that whose behavior is specified by this state machine States Transitions Event
State Machine Concepts Context States models a situation during which some (usually implicit) invariant condition holds. Transitions Event
State Simple State No Substates Initial State Object comes into being Final State The entire state machine has completed A final state cannot have any outgoing transitions
State Machine Concepts Context States Transitions A transition is a directed relationship between a source state and target state Event
Transition Guard Trigger Source Target Effects
Guard A guard is a Boolean expression that is attached to a transition as a fine-grained control over its firing. If the guard is true at that time, the transition is enabled, otherwise, it is disabled.
State Machine Concepts Context States Transitions Event An event is a specification of a type of observable occurrence
Events Call Event Reception of request to synchronously invoke operation. Creation of object Deletion of object Time Event Models expiration of specific deadline.
Events… Signal Event Reception of asynchronous signal
Events… Change Event Implicit Results as a change in some value of object attribute
Practical Implementations Mapping of Statechart to efficient code Common Approaches Double Nested Switch Statements Action state tables Generic state machine interpreters State Patterns in OO Paradigm
/* States. */ #define IDLE 0 #define DIAL 1 #define ALERTING 2 #define CONNECTED 3 #define BUSY 4 #define RINGING 5 /* Possible inputs */ #define ON_HOOK_IND 0 #define OFF_HOOK_IND 1 #define DIGITS_IND 2 #define CONNECTED_IND 3 #define INCOMING_CALL_IND 4 #define TIMEOUT_IND 5 #define PICKUP_IND 6
int transtab[][] = /* transtab[Input][CurrentState] => NextState */ { /* IDLE DIAL ALERTING CONNECTED BUSY RINGING */ { IDLE, IDLE, BUSY, IDLE, IDLE, IDLE }, /* ON_HOOK_IND */ { DIAL, DIAL, BUSY, IDLE, IDLE, IDLE }, /* OFF_HOOK_IND */ { IDLE, ALERTING, IDLE, IDLE, IDLE, IDLE }, /* DIGITS_IND */ { IDLE, DIAL, ALERTING, IDLE, IDLE, IDLE }, /* CONNECTED_IND */ { RINGING, DIAL, IDLE, CONNECTED, BUSY, RINGING }, /* INCOMING_CALL_IND */ { IDLE, DIAL, BUSY, IDLE, IDLE, IDLE }, /* TIMEOUT_IND */ { IDLE, DIAL, BUSY, IDLE, IDLE, CONNECTED }, /* PICKUP_IND */ }; curstate = transtab[event][curstate]; int curstate = IDLE;
Who defines state transitions ? May Define StateMachine
Who defines statemachine.. Context States Themselves State Tables
Guidelines Create A State Chart When Behavior Differs Based on State Place The Initial State In The Top-Left Corner Place The Final State In The Bottom-Right Corner State Names Should be Simple but Descriptive Question “Black Hole” States Question “Miracle” States Create a Hierarchy of State Charts for Very Complex Entities Name Transition Events in Past Tense
What Next Identify the implementation schemes Use XMI Aim for changes in statemachine which will need no or minimal changes to existing state implementations
Reference