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