Qt State Machine Framework

Preview:

DESCRIPTION

The next version of Qt adds a framework for defining and executing hierarchical finite state machines in Qt applications. With Qt State Machines you can effectively model how components react to events over time; these state machines are a natural extension to Qt's event-driven programming model. State machines allow you to express the behavior of your application in a more rigid, explicit way, resulting in code that's easier to test, maintain and extend. This session presents the core concepts and functionality of Qt State Machines. Presentation by Kent Hansen held during Qt Developer Days 2009. http://qt.nokia.com/developer/learning/elearning

Citation preview

Qt State Machine Framework09/25/09

About Me (Kent Hansen)

• Working on Qt since 2005

• QtScript

• Qt State Machine framework

• Plays harmonica and Irish whistle

2

Goals For This Talk

• Introduce the Qt State Machine Framework (SMF)

• Show how to incorporate it in your Qt application

• Inspire you to think about how you would use it

3

Agenda

• State machines – what and why?

• Statecharts tour

• Qt State Machine tour

• Wrap-up

4

Qt State Machine Framework (SMF)

• Introduced in Qt 4.6

• Part of QtCore module (ubiquitous)

• Originated from Qt-SCXML research project

5

Qt State Machine Framework (SMF)

• Provides C++ API for creating hierarchical finite

state machines (HFSMs)

• Provides “interpreter” for executing HFSMs

6

State Chart XML (SCXML)

• “A general-purpose event-based state machine

language”

• W3C draft (http://www.w3.org/TR/scxml/)–Defines tags and attributes–Defines algorithm for interpretation

7

Statecharts – Some use cases

• State-based (“fluid”) UIs

• Asynchronous communication

• AI

• Gesture recognition

• Controller of Model-View-Controller

• Your new, fancy product (e.g. “Hot dog oven”)

8

Mmm, hot dogs...

9

Why State Machines in Qt? (I)

?

10

Why State Machines in Qt? (II)

• Program = Structure + Behavior

• C++: Structure is language construct

• C++: Event-driven behavior is not language

construct

11

Why State Machines in Qt? (III)

• Qt already has event-based infrastructure

• Event representation (QEvent)

• Event dispatch

• Event handlers

• So what's the problem?

12

13

The spaghetti code incident (I)

“On button clicked:if X, do thiselse, do that”

14

The spaghetti code incident (II)

“On button clicked:if X, do thiselse, if Y or Z

if I and not J do thatelse, do that other thing

else, go and have a nap”

15

ifs can get iffy; whiles can get wiley

• if-statements --> state is implicit

• Control flow and useful work jumbled together

• Hard to understand and maintain

• Hard to extend

16

Can we do better...?

17

Qt SMF Mission

It shouldn't be your job to implement a general-

purpose HFSM framework!

18

There's flow...

19

… and there's control

20

What's in it for YOU?

• Write more robust code

• Have your design and implementation speak the

same language

• Cope with incremental complexity

21

Qt + State Machines = Very Good Fit

• Natural extension to Qt's application model

• Integration with meta-object system

• Nested states fit nicely with Qt ownership model

22

The right tool for the job...

23

When NOT to use Qt SMF?

• Lexical analysis, parsing, image decoding

• When performance is critical

• When abstraction level becomes too low

• Not everything should be implemented as a (Qt)

state machine!

24

Agenda

• State machines – what and why?

• Statecharts overview

• Qt State Machine tour

• Wrap-up

25

Statecharts overview

• Composite (nested) states

• Behavorial inheritance

• History states

26

A composite state

27

A composite state decomposed

28

“Get ready” state decomposed

29

“Speak” state decomposed

30

Composite states

• “Zoom in”: Consider more details

• “Zoom out”: Abstract away

• Black box vs white box

31

Like Father, Like Son...

32

Behavioral Inheritance

• States implicitly “inherit” the transitions of their

ancestor state(s)

• Enables grouping and specialization

• Analogue: Class-based inheritance

33

Behavioral Inheritance example (I)

34

Behavioral Inheritance example (II)

35

History matters...

36

History states

• Provide “pause and resume” functionality

• State machine remembers active state

• State machine restores last active state

37

History state example from real life

38

History state example

39

Agenda

• State machines – what and why?

• Statecharts overview

• Qt State Machine tour

• Wrap-up

40

Qt State Machine tour

• API introduction w/ small example

• Events and transitions

• Working with states

• Using state machines in your application

41

Qt State Machine API

• Classes for representing states

• Classes for representing transitions

• Classes for state machine-specific events

• QStateMachine class (container & interpreter)

42

My First State Machine

43

My First State Machine

“Show me the code!”

44

State machine set-up recipe

• Create QStateMachine

• Create states

• Create transitions between states

• Hook up to state signals (entered(), finished())

• Set the initial state

• Start the machine

45

State machine event processing

• State machine runs its own event loop

• QEvent-based

• Use QStateMachine::postEvent() to post an event

46

Transitions (I)

• Abstract base class: QAbstractTransition–bool eventTest(QEvent*);

• Has zero or more target states

• Add to source state using QState::addTransition()

47

Transitions (II)

• Convenience for Qt signal transitions:

addTransition(object, signal, targetState)

• Standard Qt event (e.g. QMouseEvent) transitions

also supported– QEventTransition

48

Responding to state changes

• QAbstractState::entered() signal

• QAbstractState::exited() signal

• QAbstractTransition::triggered() signal

• QState::finished() signal

49

Composite states

• Follows Qt object hierarchy

• Pass parent state to state constructor

50

QState *s1 = new QState();

QState *s11 = new QState(s1);

QState *s12 = new QState(s1);

QFinalState *s13 = new QFinalState(s1);

Paralell state group

• Set the state's childMode property

51

QState *s1 = new QState();

s1->setChildMode(QState::ParallelStates);

QState *s11 = new QState(s1);

QState *s12 = new QState(s1);

History states

• QHistoryState class

• Create as child of composite state

• Use the history state as target of a transition

52

QState *s1 = new QState();

QHistoryState *s1h = new QHistoryState(s1);

s2->addTransition(foo, SIGNAL(bar()), s1h);

How to use state machines...?

53

Scenario: Game (I)

• Many different types of game objects

• Each type's behavior modeled as composite state

• Events trigger transitions– Input (e.g. key press)

• States operate on the game object–Setting properties (e.g. velocity)–Calling slots

54

Scenario: Game (II)

• Each game object has its own state machine

• The machines run independently

• Separate, top-level state machine that

“orchestrates”–Game menus & modes–Start/quit

55

Scenario: Game (III)

• Presence of a state machine is encapsulated

• Up to each type of object

• “Simple” objects don't need to use a state machine

56

States and animations

• Integrates with Qt animation framework (also new

in Qt 4.6)

• QAbstractTransition::addAnimation()

• Almost all Qt animation examples use Qt SMF

57

My tips (I)

• Use the meta-object system integration–assignProperty(object, propertyName, value)–entered() and exited() signals

58

My tips (II)

• Use composition–Build complex behavior from simple states–Take advantage of behavioral inheritance!–Don't subclass unnecessarily

59

My tips (III)

• Always draw the statechart first–Visualizing the design from C++ is hard–The statechart is the design document

60

Agenda

• State machines – what and why?

• Statecharts tour

• Qt State Machine tour

• Wrap-up

61

Summary (I)

• Statecharts are a powerful tool for modeling

complex, event-driven systems–General-purpose–Well-defined semantics

62

Summary (II)

• With the Qt State Machine Framework, you can

build and run statecharts

• Write more robust code

• You need to consider when/where/how to use it

63

The Future (Research)

• Qt-SCXML to become part of Qt?

• Qt state machine compiler

• Visual design tool?

• Your feedback matters!

64

Relevant resources

● http://labs.qt.nokia.com

● http://lists.trolltech.com

● Qt Quarterly issue 30

● irc.freenode.net: #qt-labs

65

Thank You!

Questions?

66

Recommended