index.md (4998B)
1 +++ 2 title = 'Modeling behavior with UML State Machines' 3 +++ 4 ## Modeling behavior with UML State Machines 5 ### Introduction 6 every object has finite set of states during life. 7 8 state machine diagram is used to: 9 * model possible states of system/object 10 * show how state transitions occur as consequence of events 11 * show behavior of system in each state 12 13 ![Simple example](simple-example.png) 14 15 ### States 16 states are the nodes of state machine 17 18 when a state is active: 19 * object is in that state 20 * all internal activities in that state can be executed: 21 * `entry/Activity` - when object enters the state 22 * `do/Activity` - while object remains in this state 23 * `exit/Activity` - when object exits the state 24 25 ### Transitions 26 change from one state to another 27 28 ![Transition diagram](transition-diagram.png) 29 30 Syntax of transitions: 31 32 ![Syntax of transitions](syntax-of-transitions.png) 33 34 * Event (trigger) 35 * can trigger state transition 36 * Guard (condition) 37 * boolean expression 38 * if event occurs, guard is checked 39 * if guard is true: 40 1. all activities in current state are terminated 41 2. exit activity is executed 42 3. transition happens 43 * Activity (effect) 44 * sequence of actions that happen during transition 45 46 Types: 47 * internal: 48 49 ![Internal state transition](internal-state-transition.png) 50 51 * if `event1` happens, object stays in `state1` and `Activity3` runs 52 53 * external: 54 55 ![External state transition](external-state-transition.png) 56 57 * if `event1` happens: 58 * object leaves `state1`, `Activity2` runs 59 * `Activity3` runs 60 * object enters `state1` and `Activity1` runs 61 62 Timing of transitions: 63 64 ![Table of transition timing](table-of-transition-timing.png) 65 66 ### Types of events 67 * *Signal event:* receipt of a signal (`rightmousedown`, `sendSMS(message)`) 68 * *Call event:* operation call (`occupy(user, lectureHall)`, `register(exam)`) 69 * *Time event:* time-based state transition (relative or absolute time) 70 * *Any receive event:* when any event occurs that doesn't trigger another transition from the active state 71 * *Completion event:* automatic when everything is completed in the current state 72 * *Change event:* permanently checking when a condition becomes true 73 74 A change event is permanently checked. A guard is only checked when the event occurs. 75 76 ### Types of states 77 Initial state: 78 * "start" of the diagram 79 * pseudo-state, system can't remain in this state 80 * no incoming edges 81 * outgoing edges have to be mutually exclusive and at least one target must be reachable. no events allowed. 82 * system immediately switches from initial state. 83 * notation: ![Initial state notation](initial-state-notation.png) 84 85 Final state: 86 * real state 87 * end of sequence of states 88 * can remain in this state forever 89 * notation: ![Final state notation](final-state-notation.png) 90 91 Terminate node: 92 * pseudo-state 93 * terminates state machine 94 * modeled object is deleted 95 * notation: ![Terminate node notation](terminate-node-notation.png) 96 97 Decision node: 98 * pseudo-state 99 * used for alternative transitions 100 * notation: ![Decision node notation](decision-node-notation.png) 101 102 Parallelization node: 103 * pseudo-state 104 * splits control flow into multiple concurrent flows 105 * 1 incoming edge, >1 outgoing edges 106 * notation: ![Parallelization node notation](parallelization-node-notation.png) 107 108 Synchronization node: 109 * pseudo-state 110 * merges multiple concurrent flows 111 * >1 incoming edge, 1 outgoing edge 112 * notation: ![Synchronization node notation](synchronization-node-notation.png) 113 114 Composite state: 115 * contains substates, with only one of them active at any time 116 * arbitrary nesting depth 117 * higher level events take priority 118 119 ![Composite state diagram](composite-state-diagram.png) 120 121 Orthogonal state: 122 * composite state divided into two or more regions, separated by dashed line 123 * one state of each region is always active at some point (concurrent substates) 124 * final state has to be reached in all regions to trigger completion 125 126 ![Orthogonal state diagram](orthogonal-state-diagram.png) 127 128 Submachine state (SMS) 129 * to reuse parts of state machine diagrams in other ones 130 * as soon as submachine state is activated, behavior of submachine is executed (subroutine) 131 * notation: `state:submachineState` 132 133 ![Submachine state diagram](submachine-state-diagram.png) 134 135 History state: 136 * remembers the last active substate of a composite state 137 * activates 'old' substate and all entry activities run sequentially from outside to inside of composite state 138 * exactly one outgoing edge of history state points to a substate. used if the composite state was never active, or it was exited via final state. 139 * shallow history state restores state on the same level of the composite state (`H`) 140 * deep history state restores last active substate over _all levels_ (`H*`) 141 142 ### Entry and exit points 143 Encapsulation mechanism: a composite state shall be entered/exited via a state other than initial and final states. 144 145 external transition must/need not know structure of composite state. 146