SysML: How to Use State Diagrams to Model Systems Behavior

A state machine diagram (also known as statechart, state transition diagram or state diagram) is a kind of behavior diagram; like an activity diagram and a sequence diagram, it presents a dynamic view of a system. Unlike an activity diagram and a sequence diagram, a state machine diagram focuses attention on how a structure within a system (object, or block instance) changes state in response to event occurrences over time.

SysML diagram types

A state machine diagram is well suited to serve as detailed design of a particular classifier (that is, an input into development). Like a sequence diagram, a state machine diagram is a precise and unambiguous specification of behavior. Thus, it is often be used for system design and simulation/code generation.

Why State Machine Diagrams?

State machine diagrams typically are used to describe state-dependent behavior for an object. An object responds differently to the same event depending on what state it is in. State machine diagrams are usually applied to objects but can be applied to any element that has behavior to other entities such as actors, use cases, methods, subsystems systems, etc. and they are typically used in conjunction with interaction diagrams (usually sequence diagrams).

For example:

Consider you have $100,000 in a bank account. The behavior of the withdraw function would be: balance:= balance – withdrawAmount; provided that the balance after the withdrawal is not less than $0; this is true regardless of how many times you have withdrawn money from the bank. In such situations, the withdrawals do not affect the abstraction of the attribute values, and hence the gross behavior of the object remains unchanged.

However, if the account balance would become negative after a withdrawal, the behavior of the withdraw function would be quite different. This is because the state of the bank account is changed from positive to negative; in technical jargon, a transition from the positive state to the negative state is fired.

The abstraction of the attribute value is a property of the system, rather than a globally applicable rule. For example, if the bank changes the business rule to allow the bank balance to be overdrawn by 2000 dollars, the state of the bank account will be redefined with condition that the balance after withdrawal must not be less than $2000 in deficit.

What is a State?

James Rumbaugh (the 3 Amigos of UML) defines that:

“A state is an abstraction of the attribute values and links of an object. Sets of values are grouped into a state according to properties that affect the gross behavior of the object.”

State Notation

State machine diagram notation: State example

State

A state is a constraint or a situation in the life cycle of an object, in which a constraint holds, the object executes an activity or waits for an event.

A state machine diagram is a graph consisting of:

  • States (simple states or composite states)
  • State transitions connecting the states

Example:

Characteristics of State

  • State represents the conditions of objects at certain points in time.
  • Objects (or Systems) can be viewed as moving from state to state
  • A point in the lifecycle of a model element that satisfies some condition, where some particular action is being performed or where some event waits

Initial and Final States

  • The initial state of a state machine diagram, known as an initial pseudo-state, is indicated with a solid circle. A transition from this state will show the first real state
  • The final state of a state machine diagram is shown as concentric circles. An open-loop state machine represents an object that may terminate before the system terminates, while a closed-loop state machine diagram does not have a final state; if it is the case, then the object lives until the entire system terminates.

Example:

State machine diagram notation: Initial and Final state

Events

An event signature is described as Event-name (comma-separated-parameter-list). Events appear in the internal transition compartment of a state or on a transition between states. An event may be one of four types:

  • Signal event – corresponding to the arrival of an asynchronous message or signal
  • Call event – corresponding to the arrival of a procedural call to an operation
  • Time event – a timed event occurs after a specified time has elapsed
  • Change event – a change event occurs whenever a specified condition is met

Characteristics of Events

  • Represents incidents that cause objects to transition from one state to another.
  • Internal or External Events trigger some activity that changes the state of the system and some of its parts
  • Events pass information, which is elaborated by Object operations. Objects realize Events
  • Design involves examining events in a state machine diagram and considering how those events will be supported by system objects

Transition

Transition lines depict the movement from one state to another. Each transition line is labeled with the event that causes the transition.

  • Viewing a system as a set of states and transitions between states is very useful for describing complex behaviors
  • Understanding state transitions is part of system analysis and design
  • A Transition is a movement from one state to another state

Transitions between states occur as follows:

  1. An element is in a source state
  2. An event occurs
  3. An action is performed
  4. The element enters a target state
  • Multiple transitions occur either when different events result in a state terminating or when there are guard conditions on the transitions
  • A transition without an event and action is known as automatic transitions

Actions

Action is an executable atomic computation, which includes operation calls, the creation or destruction of another object, or the sending of a signal to an object. An action is associated with transitions and during which an action is not interruptible – e.g., entry, exit

Activity

Activity is associated with states, which is a non-atomic or ongoing computation. Activity may run to completion or continue indefinitely. An Activity will be terminated by an event that causes a transition from the state in which the activity is defined

Characteristics of Action and Activities

  • States can trigger actions
  • States can have a second compartment that contains actions or activities performed while an entity is in a given state
  • An action is an atomic execution and therefore completes without interruption
  • Five triggers for actions: On Entry, Do, On Event, On Exit, and Include
  • An activity captures complex behavior that may run for a long duration – An activity may be interrupted by events, in which case it does not completely occur when an object arrives in a state.

Entry and Exit Actions

Entry and Exit actions specified in the state. It must be true for every entry/exit occurrence. If not, then you must use actions on the individual transition arcs

  • Entry Action executed on entry into a state with the notation: Entry/action
  • Exit Action executed on exit from a state with the notation: Exit/action

Example – Entry / Exit Action (Check Book Status)

This example illustrates a state machine diagram derived from a Class – “BookCopy”:

State machine diagram notation: Class and state

Note:

  • This state machine diagram shows the state of an object myBkCopy from a BookCopy class
  • Entry action : any action that is marked as linked to the entry action is executed whenever the given state is entered via a transition
  • Exit action : any action that is marked as linked to the exit action is executed whenever the state is left via a transition

Substates

A simple state is one which has no substructure. A state which has substates (nested states) is called a composite state. Substates may be nested to any level. A nested state machine may have at most one initial state and one final state. Substates are used to simplify complex flat state machines by showing that some states are only possible within a particular context (the enclosing state).

Substate Example – Heater

State machine diagram notation: subsystem

State Machine Diagrams are often used for deriving testing cases, here is a list of possible test ideas:

  • Idle state receives Too Hot event
  • Idle state receives Too Cool event
  • Cooling/Startup state receives Compressor Running event
  • Cooling/Ready state receives Fan Running event
  • Cooling/Running state receives OK event
  • Cooling/Running state receives Failure event
  • Failure state receives Failure Cleared event
  • Heating state receives OK event
  • Heating state receives Failure event

History States

Unless otherwise specified, when a transition enters a composite state, the action of the nested state machine starts over again at the initial state (unless the transition targets a substate directly). History states allow the state machine to re-enter the last substate that was active prior to leaving the composite state. An example of history state usage is presented in the figure below.

State machine diagram notation: History state

Concurrent State

As mentioned above, states in state machine diagrams can be nested. Related states can be grouped into a single composite state. Nesting states inside others is necessary when an activity involves concurrent sub-activities. The following state machine diagram models an auction with two concurrent substates (known as orthogonal regions): processing the bid and authorizing the payment limit. These two regions will respond to event occurrences independently of each other.

Concurrent State Machine Diagram Example – Auction Process

In this example, the state machine first entering the Auction requires a fork at the start of two separate start threads. Each substate has an exit state to mark the end of the thread. Unless there is an abnormal exit (Canceled or Rejected), the exit from the composite state occurs when both substrates have exited.

State machine diagram notation: Concurrent state

State Machine Diagram Notation Illustrated

A state machine consists of states, linked by transitions. A state is a condition of an object in which it performs some activity or waits for an event. A transition is a relationship between two states which is triggered by some event, which performs certain actions or evaluations, and which results in a specific end-state.

The annotated diagram examples below provide the element summary for state machine diagram as shown in the following figures below:

State machine diagram notation

Turn every software project into a successful one.

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK