66
Qt State Machine Framework 09/25/09

Qt State Machine Framework

Embed Size (px)

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

Page 1: Qt State Machine Framework

Qt State Machine Framework09/25/09

Page 2: Qt State Machine Framework

About Me (Kent Hansen)

• Working on Qt since 2005

• QtScript

• Qt State Machine framework

• Plays harmonica and Irish whistle

2

Page 3: Qt State Machine Framework

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

Page 4: Qt State Machine Framework

Agenda

• State machines – what and why?

• Statecharts tour

• Qt State Machine tour

• Wrap-up

4

Page 5: Qt State Machine Framework

Qt State Machine Framework (SMF)

• Introduced in Qt 4.6

• Part of QtCore module (ubiquitous)

• Originated from Qt-SCXML research project

5

Page 6: Qt State Machine Framework

Qt State Machine Framework (SMF)

• Provides C++ API for creating hierarchical finite

state machines (HFSMs)

• Provides “interpreter” for executing HFSMs

6

Page 7: Qt State Machine Framework

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

Page 8: Qt State Machine Framework

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

Page 9: Qt State Machine Framework

Mmm, hot dogs...

9

Page 10: Qt State Machine Framework

Why State Machines in Qt? (I)

?

10

Page 11: Qt State Machine Framework

Why State Machines in Qt? (II)

• Program = Structure + Behavior

• C++: Structure is language construct

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

construct

11

Page 12: Qt State Machine Framework

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

Page 13: Qt State Machine Framework

13

Page 14: Qt State Machine Framework

The spaghetti code incident (I)

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

14

Page 15: Qt State Machine Framework

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

Page 16: Qt State Machine Framework

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

Page 17: Qt State Machine Framework

Can we do better...?

17

Page 18: Qt State Machine Framework

Qt SMF Mission

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

purpose HFSM framework!

18

Page 19: Qt State Machine Framework

There's flow...

19

Page 20: Qt State Machine Framework

… and there's control

20

Page 21: Qt State Machine Framework

What's in it for YOU?

• Write more robust code

• Have your design and implementation speak the

same language

• Cope with incremental complexity

21

Page 22: Qt State Machine Framework

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

Page 23: Qt State Machine Framework

The right tool for the job...

23

Page 24: Qt State Machine Framework

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

Page 25: Qt State Machine Framework

Agenda

• State machines – what and why?

• Statecharts overview

• Qt State Machine tour

• Wrap-up

25

Page 26: Qt State Machine Framework

Statecharts overview

• Composite (nested) states

• Behavorial inheritance

• History states

26

Page 27: Qt State Machine Framework

A composite state

27

Page 28: Qt State Machine Framework

A composite state decomposed

28

Page 29: Qt State Machine Framework

“Get ready” state decomposed

29

Page 30: Qt State Machine Framework

“Speak” state decomposed

30

Page 31: Qt State Machine Framework

Composite states

• “Zoom in”: Consider more details

• “Zoom out”: Abstract away

• Black box vs white box

31

Page 32: Qt State Machine Framework

Like Father, Like Son...

32

Page 33: Qt State Machine Framework

Behavioral Inheritance

• States implicitly “inherit” the transitions of their

ancestor state(s)

• Enables grouping and specialization

• Analogue: Class-based inheritance

33

Page 34: Qt State Machine Framework

Behavioral Inheritance example (I)

34

Page 35: Qt State Machine Framework

Behavioral Inheritance example (II)

35

Page 36: Qt State Machine Framework

History matters...

36

Page 37: Qt State Machine Framework

History states

• Provide “pause and resume” functionality

• State machine remembers active state

• State machine restores last active state

37

Page 38: Qt State Machine Framework

History state example from real life

38

Page 39: Qt State Machine Framework

History state example

39

Page 40: Qt State Machine Framework

Agenda

• State machines – what and why?

• Statecharts overview

• Qt State Machine tour

• Wrap-up

40

Page 41: Qt State Machine Framework

Qt State Machine tour

• API introduction w/ small example

• Events and transitions

• Working with states

• Using state machines in your application

41

Page 42: Qt State Machine Framework

Qt State Machine API

• Classes for representing states

• Classes for representing transitions

• Classes for state machine-specific events

• QStateMachine class (container & interpreter)

42

Page 43: Qt State Machine Framework

My First State Machine

43

Page 44: Qt State Machine Framework

My First State Machine

“Show me the code!”

44

Page 45: Qt State Machine Framework

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

Page 46: Qt State Machine Framework

State machine event processing

• State machine runs its own event loop

• QEvent-based

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

46

Page 47: Qt State Machine Framework

Transitions (I)

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

• Has zero or more target states

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

47

Page 48: Qt State Machine Framework

Transitions (II)

• Convenience for Qt signal transitions:

addTransition(object, signal, targetState)

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

also supported– QEventTransition

48

Page 49: Qt State Machine Framework

Responding to state changes

• QAbstractState::entered() signal

• QAbstractState::exited() signal

• QAbstractTransition::triggered() signal

• QState::finished() signal

49

Page 50: Qt State Machine Framework

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);

Page 51: Qt State Machine Framework

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);

Page 52: Qt State Machine Framework

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);

Page 53: Qt State Machine Framework

How to use state machines...?

53

Page 54: Qt State Machine Framework

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

Page 55: Qt State Machine Framework

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

Page 56: Qt State Machine Framework

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

Page 57: Qt State Machine Framework

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

Page 58: Qt State Machine Framework

My tips (I)

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

58

Page 59: Qt State Machine Framework

My tips (II)

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

59

Page 60: Qt State Machine Framework

My tips (III)

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

60

Page 61: Qt State Machine Framework

Agenda

• State machines – what and why?

• Statecharts tour

• Qt State Machine tour

• Wrap-up

61

Page 62: Qt State Machine Framework

Summary (I)

• Statecharts are a powerful tool for modeling

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

62

Page 63: Qt State Machine Framework

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

Page 64: Qt State Machine Framework

The Future (Research)

• Qt-SCXML to become part of Qt?

• Qt state machine compiler

• Visual design tool?

• Your feedback matters!

64

Page 65: Qt State Machine Framework

Relevant resources

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

● http://lists.trolltech.com

● Qt Quarterly issue 30

● irc.freenode.net: #qt-labs

65

Page 66: Qt State Machine Framework

Thank You!

Questions?

66