Docsity

Understanding State Machines and Statecharts: A Comprehensive Guide - Prof. Wei-Tek Tsai, Exams of Computer Science

Partial preview of the text

Download Understanding State Machines and Statecharts: A Comprehensive Guide - Prof. Wei-Tek Tsai and more Exams Computer Science in PDF only on Docsity!

1

Testing State-Based Systems

Wei-Tek Tsai Department of Computer Science and Engineering Arizona State University Tempe, AZ 85287

2

State Machines

3

What is a State Machine?

  • A state machine is a system whose output is determined by both current and past input. The effect of previous inputs is represented by a state. In contrast, the output of a combinational system is determined by current input only.

4

State

  • A state machine has a fixed set of possible states. The properties and behavior of the state machine are identical whenever it is in a particular state.

7

Action

  • Associated with a particular state and event may be an action. An action may include a transition to a new state, but may also result in an output from the state machine.

8

Transition and state

  • A transaction takes a system from one state to another. A transition has a pair of states: the accepting state and the resultant state. A machine may be in only one state at a time.
  • The initial state is the state in which the first event is accepted. The final state is one in which the machine stops accepting events.

9

State Transition Diagram

  • A state transition diagram is a graphic representation of a state machine. - Nodes represents states. - Arrows represent transitions. - The annotations on the edges represent events and actions. State State State

Initial State Final State

Self-Transaction

Event Trigger / Action

10

Guarded Transitions

  • A guard is a predicate expression associated with an event. A guard transition cannot fire unless (1) the system is in the accepting state for this transition, (2) the guarded event occurs, and (3) the guard predicate evaluates to true.

event-name argument-list ‘[‘ guard-condition ‘]’ ‘/’ action-expression

13

State and Transition

  • A directed graph of states (nodes) connected by transitions (directed arcs)

State Name stateVar : type = value entry/ entry activity do/ activity exit/ exit activity

event(args) [guard condition] ^target.sendEvent(args)/ operation(args)

State Event

Transition

14

Initial and Final State

  • Initial state
    • The state entered when an object is created
    • Mandatory
    • Can only have one initial state
  • Final state
    • Indicates the object’s end of life
    • Optional
    • May have more than one

State

State

15

Events

  • One event may trigger the sending of another event
  • An activity can also send an event to another object

State B do: ^TargetObject.event

State A

event ^TargetObject.event

16

Transitions

  • For each state, determine what events cause transitions to what states, including guard conditions, when needed
  • Transitions describe what happens in response to the receipt of an event

State A

State B do: activity

event[ condition ] / action

State C

19

Standard Events for a State

  • Standard events may be associated with each state. - Entry - specifies actions on entry to a state - Exit - specifies actions on exit from a state - Do - specifies activities to be performed while in a state

State Name stateVar : type = value entry/ entry action do/ activityexit/ exit action

20

Example of State Machine

checking

waiting

Cancelled

delivered

dispatching

[Not all items checked]/ getNextItem

[All items checked &&Some items not in stock]

[All items checked &&All items available] do/check item

Item Received[Some items not In stock]^ cancelled

do/initiate delivery

cancelled

transhipped

21

State History

  • History provides the capability to return to the most recently visited state.
  • This is useful if it it necessary to interrupt or reverse an activity.
  • History is shown by adding the history icon, a circle with an H, inside a super-state.
  • A return to the super-state causes it to resume the state that it had last. H

22

Composite State

  • Substate is a method of hierarchical decomposition. A state that has substate is called a composite state.
  • A state nested inside another state. A substate is a state that is nested in another state.
  • A state that has no substates is call simple state.
  • Substate may be nested to any level.

25

Importance of Testing Statecharts

  • Statecharts play crucial role in modeling.
    • Safety-critical systems
    • Networking protocols
    • GUIs (Graphical User Interfaces)
    • OOD (Object-Oriented Design)

26

State-based Testing

  • Use state diagram to determine a sequence of events.
  • Tests should cover all states.
  • All transitions should be tested al least once.
  • Test also invalid and unrelated events.

27

Methodology of Testing

Graphical Specification

Intermediate Specification

Test Specifications

Notation specific translation

General purpose test generation technique

Behavior defined by complex notation specific semantics

Explicit specification of the behavior under test in a common notation

28

Coverage Metrics

  • State Coverage
    • Ratio of number of states covered and total number of states in the given state model
  • Event Coverage
    • Ratio of number of events covered and total number of events in the given state model
  • Transition Coverage
    • Ratio of number transitions exercised and total number of transitions in the given state model
  • State-Event Coverage
    • Ratio of state-event pairs exercised and number of states multiply number of events in the given sate model

31

Modeling Languages

  • The three views of the system model are described: - Structure: Module-charts - Functionality:Activity-charts - Behavior: Statecharts
  • Enable to simulate
  • Support from analysis phase to implement

32

Module-charts

  • A module-chart can be regarded as a certain kind of data-flow diagram. Module-charts are used to describe the modules that constitute the implementation of the system, its division into hardware and software blocks and their inner components, and the communication between them.

33

Module-charts

E

Shared Data

E

E

C E C

B C

A

Environment module Storage module

Modules Sub-modules

34

Activity-charts

  • Activity-charts can be viewed as multi-level data-flow diagrams. They capture functions, or activities, as well as data-stores, all organized into hierarchies and connected via the information that flows between them.

37

Statecharts

B C D E

F

G

H

I

J

K L

A

m

n o p q

r

s

x

u v

t

w y

38

Relations between Three Views

39

Dynamic Stepping

  • User emulates environment
  • View all consequences of one dynamic step
  • STATEMATE highlights all resulting active states and actions

40

Simple Example

  • This is an example of switch control.