0% found this document useful (0 votes)
9 views

Advanced Behavioral Modeling - states only - 1 hr

Uploaded by

Miguel Martínez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Advanced Behavioral Modeling - states only - 1 hr

Uploaded by

Miguel Martínez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Advanced Behavioral Modeling

State Machines in an hour

Bruce Powel Douglass, Ph.D.


Chief Evangelist, Global Technology Ambassador
IBM IoT
[email protected]
Twitter: @IronmanBruce
www.bruce-douglass.com

© Bruce Powel Douglass, 2019 1


“If I hear, I forget.
If I see, I remember.
If I do, I understand.”
- Confucius

Law of Douglass #35

© Bruce Powel Douglass, 2019 2


State machines

A state is a distinguishable, disjoint,


orthogonal condition of existence
of an object that persists for a
significant period of time

A transition is a response
to an event of interest moving
the object from a state
to a state.

© Bruce Powel Douglass, 2019 3


State Machines

An action is a run-to-completion
behavior. The object will not accept
or process any new events until
the actions associated with the
current event are complete.

Order of action execution


(1) exit actions of current state
(2) transition actions
(3) entry actions of next state

© Bruce Powel Douglass, 2019 4


Basic Statechart Syntax

State name
Event name Guard Action list

Entry action

Exit action

Internal Transitions
(“Reactions in state”)
State

© Bruce Powel Douglass, 2019 5


Statechart Syntax – OR States

• An object must always be in exactly one OR-state at a given level


of abstraction.

© Bruce Powel Douglass, 2019 6


A Simple FSM

• You are implementing a reliable transmission service for an OSI-


compliant protocol stack.
• A message is sent that requires the receiver to return an ACK.
• If an ACK does not occur, retransmit the message
• If the message is transmitted 5 times without an ACK, then inform
the sender.

© Bruce Powel Douglass, 2019 7


Where’s the State Machine?

• Create a state machine to


reliably deliver a message
over a communication
protocol
1. Create the class model
2. Identify the reactive
(State-owning) class
3. Write the state machine

© Bruce Powel Douglass, 2019 8


Message Transaction FSM
This “default pseudostate”
identifies which state is entered
to start (REQUIRED)
This “condition pseudostate”
uses guards to select which
outgoing transition will be taken

This “terminal
pseudostate”
identifies that the
state machine no
longer receives and
processes events
(OPTIONAL)

© Bruce Powel Douglass, 2019 9


Types of Events

• UML defines 4 kinds of events


– Signal Event
• Asynchronous signal received e.g. evStart
• Requires queuing
– Call Event
• Operation call received e.g. opCall(a,b,c)
• Implementation “body” is shown on the action list of the transition
– Change Event
• Change in value occurred
– Time Event
• Absolute time arrived
• Relative time elapsed e.g. tm(PulseWidthTime)
– Are Classifiers and so can be generalized and
specialized

© Bruce Powel Douglass, 2019 10


Timeout Events

• What happens if the


object receives the
stopPolling event when in
the Delay state?
• Will the system always
print
tick tock
as long as it is in the
Polling state?

© Bruce Powel Douglass, 2019 11


Timeout Events

• Let’s see …

© Bruce Powel Douglass, 2019 12


Statechart Syntax – Nested States

© Bruce Powel Douglass, 2019 13


Order of Nested Actions

doExitStuff();
doTransitionIntoStuff();
doOuterStuffOnEntry();
doMiddleStuffOnEntry();
doInnerEntryStuff();

doInnerExitStuff();
doMiddleStuffOnExit();
• Execute from outermost - in on entry doOuterStuffOnExit();
• Execute from innermost - out on exit doTransitionOutOffStuff();
doEntryStuff();

© Bruce Powel Douglass, 2019 14


Statechart Syntax – AND States
for simultaneously true conditions

Each region
must have its
own default

This transition
leaves all
nested and-
states

© Bruce Powel Douglass, 2019 15


And-States Notes

• FSMs can always be represented without using And-states


– It requires the development of the “cross product of the state spaces”
Without And-States

With And-States

© Bruce Powel Douglass, 2019 16


State explosion – up close and personal

© Bruce Powel Douglass, 2019 17


UML Pseudostates

Symbol Symbol Name Symbol Symbol Name

Branch Pseudostate (type H (Shallow) History Pseudostate


C or
of junction pseudostate)

H* (Deep) History Pseudostate


T or Terminal or Final Pseudostate

Initial or Default Pseudostate

Fork Pseudostate Junction Pseudostate

Merge Junction Pseudostate


Join Pseudostate
(type of junction pseudostate)

[g] Entry Point Pseudostate


Choice Point Pseudostate label

[g] Exit Point Pseudostate


label

© Bruce Powel Douglass, 2019 18


Statechart Syntax – Pseudostates

History
Join

Conditional

Fork

Default
Termination Diagram
© Bruce Powel Douglass, 2019 (Final state) (must be a pair)
19
History: Deep and Shallow

e6 Deep (all levels


below)

e10

e9
e7

e8
e1

e2
e5 e3 e4

Shallow
(only 1
level)

© Bruce Powel Douglass, 2019 20


Submachines - Parent

Entry
Connector

Navigate
to Child

Exit
Connector
© Bruce Powel Douglass, 2019 21
Submachines - Child

© Bruce Powel Douglass, 2019 22


Poorly-Formed Statecharts
Law of Douglass 55: Any language rich enough to say something
useful, is rich enough to say something stupid. Don’t say stupid things.
Race
Must be same
condition
event

No Defaults

Conflicting
Transitions

Likely run-to-
completion
violation
Longer timeout
will never fire Overlapping
© Bruce Powel Douglass, 2019 guards 23
Did I mention you can view states in state
tables too?

• The most common form of a state table is state x transition


• Strength
– Explicitly showing missing transitions (for checking completeness)
• Weakness
– Can’t easily show actions

Composite-states

And-states

© Bruce Powel Douglass, 2019 24


Debugging State Machines:
There are levels of sophistication

1. There’s always the printf approach to debugging


– Add cout or printf statements to actions on state machine
2. Animation of state machines adds the ability to
interactively insert events, change values, and observe
state behavior
3. You can use more advanced tools like Webify or Panel
diagrams to construct user views to drive and observe
the state machine
4. Can use the UML Testing Profile (such as with Test
Conductor) to automate testing of state machines

© Bruce Powel Douglass, 2019 25


Example: Gas Mixer Use Case

• In this case, we constructed a state machine of a patient


ventilator system use case named Mix Gases. This state
machine models the requirements. (and not the design)
of the use case.
– These include the range of acceptable gases, flow rates, and
concentrations (e.g. can not be “hypoxic” (< 21% O2))
• We created a representations of the actors (as blocks)
with state behavior to “drive” the use case state
machine
• We will add panel diagram elements to one of the actor
blocks to make it easy to drive and observe the behavior
• We will capture the interactions as “animated sequence
diagrams” which will later serve as specification for
system verification test cases
© Bruce Powel Douglass, 2019 26
Gas Mixer Use Case Execution Context

© Bruce Powel Douglass, 2019 27


Gas Mixer Use Case Block State Machine

© Bruce Powel Douglass, 2019 28


Gas Mixer Use Case Block State Submachines

© Bruce Powel Douglass, 2019 29


Physician Actor State Machine

Text controls to see


or change flow values

Panel
buttons to
send
events

© Bruce Powel Douglass, 2019 30


Testing the model

Physician actions Physician actions


1. Select the O2 gas 7. Select the He gas
2. Set the flow to 20000 l/min 8. Set the flow to 5000 l/min
3. Push the Set O2 flow button 9. Push the Set H2 Flow button
4. Select the N2 gas 10. Enable mixing
5. Set the flow to 10,000 l/min System determines mixture is not hypoxic
6. Push the Set N2 Flow button and with valid ranges and so starts
© Bruce Powel Douglass, 2019
delivering. 31
Output (Animated Sequence Diagram)

© Bruce Powel Douglass, 2019 32


Putting it all Together

Sequence diagrams: State diagrams:

• Good to show discrete • Good for discrete modal


interactions among sets of behavior
elements • Applies to a single classifier

Timing Diagrams:
• Good for showing the change
of value or state over time

Flowcharts:
• Good for single-threaded
Activity diagrams: algorithmic flow especially for
operations
• Good for algorithmic flow and
continuous flows (SysML)
• Uses token flow semantics
© Bruce Powel Douglass, 2019 33
Want to know more?

© Bruce Powel Douglass, 2019 34

You might also like