Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 290C: Formal Models for Web Software Lecture 2: Modeling States with Statecharts Instructor: Tevfik Bultan.

Similar presentations


Presentation on theme: "CS 290C: Formal Models for Web Software Lecture 2: Modeling States with Statecharts Instructor: Tevfik Bultan."— Presentation transcript:

1 CS 290C: Formal Models for Web Software Lecture 2: Modeling States with Statecharts Instructor: Tevfik Bultan

2 Statecharts A visual formalism for specifying hierarchical state machines – “A Visual Formalism for Complex Systems,” David Harel, Science of Computer Programming, 1987 Especially useful for modeling reactive systems –systems which continuously react to internal or external events without terminating State machines in UML are based on Statecharts Statecharts is a formal language, it has formal semantics

3 Statecharts Statecharts characteristics: Hierarchical grouping of states –superstates are formed by grouping other states Superstates can be formed using AND composition or OR composition –When the system is in an AND-state it is in all of its substates –When the system is in an OR-state it is in only one of its substates –If a state has no substates it is an atomic state Synchronization and communication between different parts of the system is achieved using events

4 Statecharts A B C D A basic state machine Initial (default) state is A If event x occurs while in state A, the next state will be B If event y occurs while in state A, the next state will be C x y t Given the event sequence: y, x, t Behavior of the state machine is: A  C  D The x event is ignored since it does not enable any transition when the system is in state C Note that we are interested in the behavior of the machine — the paths it takes (including the states it visits. This is different then using the state machines as language recognizers (as in automata theory or compilers) yt

5 Statecharts: OR-states D A C D is an OR-state, its substates are A and C When the system is in state D it is either in A or C (but not both) The default state of D is A. When the system transitions to state D it will go to state A unless it is explicitly specified otherwise While the system is in state A or C (meaning it is in state D) if event t occurs it transitions to B While the system is in state B if event x occurs the system transitions to state D, since the default state in D is A, it transitions to A A hierarchical state machine B x y t z A C B x y t z t Equivalent basic state machine Given the event sequence: x, y, t, z Behavior of the hierarchical state machine: B  D(A)  D(C)  B  D(C) Behavior of the equivalent basic state machine: B  A  C  B  C x z ty xytz

6 Statecharts: Transitions Statechart transitions have a source state and a target state and are labeled as trigger-event [guard-condition] / generated-events trigger-event[guard-condition]/action source statetarget state

7 Statecharts: Transitions The transition is enabled if the trigger-event occurs and the guard-condition is true while the system is in the source state of the transition A transition can be taken if it is enabled The events can be generated by the state machine itself (internal events) or can be generated by the environment (external events) When a transition is taken it generates the generated-events (a set of events) which may enable other transitions and it transitions to its destination state Guard condition is a boolean expression that combines predicates in the form In(state) (such a predicate is true when the system is in the specified state)

8 Statecharts: Transitions Two transitions are conflicting if they cannot be taken at the same time If two conflicting transitions are enabled at the same time, one of them is taken non-deterministically A CB t t Two outgoing transitions from state A are conflicting (they cannot be taken at the same time). When event t occurs if the system is in state A both transitions are enabled. Since they are conflicting one of them is taken nondeterministically

9 Statecharts: AND-states (concurrency) B C xy[in G] AD G E F x t z v R R is and AND-state When the system is in the AND-state R, it is both in state A and state D A and D are OR-states When the system is in state A it is either in state B or in state C (but not in both) When the system is in state D it is in only one of E or F or G A hierarchical state machine B-E C-E B-F x z t B-G C-G C-F y x z v v x x Equivalent basic state machine t Note that event x causes a synchronization between states A and D when A is in B and D is in F. They make a concurrent transition: A goes from B to C D goes from F to G

10 Statecharts: State hierarchy B C xy[in G] AD G E F x t z v R A hierarchical state machine We can think of the state hierarchy as a tree: R(AND) A(OR)D(OR) BCEFG H is the root state B,C, E, F, G are the leaf (atomic) states Other than the atomic (leaf) states each state is marked as an OR or an AND state Given the event sequence: x, z, y, t, v The behavior of the statechart is: R(A(B))-R(D(F))  R(A(C))-R(D(G))  R(A(C))-R(D(F))  R(A(C))-R(D(E))  R(A(C))-R(D(G)) It is hard to read the above representation. We can show the behavior using only the atomic states (assuming they have unique names): B-F  C-G  C-F  C-E  C-G x vt xztv z

11 Statecharts: Internal vs. External events Internal events have higher priority –when an internal event is generated, first the transitions enabled by the internal event is taken before the next external event is considered A CB x/t t y Given the input event sequence: x,y The behavior of the statechart is: A  C  B  C xt y y

12 Statecharts: History States B C xy[in G] AD G R E F x t z v K w H Entering the history state (marked with H in a circle) in state D means that enter the most recently visited substate of D What if substates of D have their own substates? –H* (deep history state) means enter all the most recently visited descendants z Given the input event sequence: x,w,z The behavior of the statechart is: B-F  C-G  K  B-G xwz

13 Statecharts: An Example Alarm Shut Op On Off 1 2 ModeVol t_on start stop t_offt_on incdec Given the input event sequence: start, t_on, inc, dec, stop The behavior of the statechart is: Shut  Off-1  On-1  On-2  On-1  Shut startt_on incdecstop


Download ppt "CS 290C: Formal Models for Web Software Lecture 2: Modeling States with Statecharts Instructor: Tevfik Bultan."

Similar presentations


Ads by Google