[Qt-interest] Qml & State Machines

Volodimir.Burlik at nokia.com Volodimir.Burlik at nokia.com
Tue Nov 30 21:40:26 CET 2010


Hi,
I've learned that Qml states are not designed for full blown state machines and intended for UI elements. However here the form that I think would help to make it as such:

states: [
        State {
            name: "INIT"
            onEntry:  { doOnEntry1 } 

            event: event id1            action: { doSomething1 }       next: "statename1"
            event: event id2            action: { doSomething2 }       next: "statename2"
        },
        State {
            name: "IDLE"
            onEntry:  { doOnEntry2 } 

            event: event id3            action: { doSomething3 }       next: "WAITFORSMTHG"
            event: event id4            action: { doSomething4 }       next: ""
        },	
        State {
            name: "WAITFORSMTHG"
            heartbeatrate: 1000
            heartbeats: 10

            onEntry:  { doOnEntry2 } 
            event: event idx            action: { doSomething1 }       next: "statenamex"
            event: event idy            action: { doSomething2 }       next: "statenamey"
            event: heartBeat            action: { doSomethingx }       next: ""			// system event on each heartbeat - every sec
            event: lastHeartBeat        action: { doSomethingz }       next: ""			// system event on last heartbeat - in 10 sec
        }
    ]
- It is the clean separation b/n functional(what) and behavioral(how) views. Code is broken down to pure logic and linear-ish action functions
- This form allows describing life cycle of elements and life cycle of each state - no need for separate timers
- It supports N->1 and 1->N state transitions
- IMPORTANT! State machine transitions are prerogative of state machine body ONLY!
  This is a NO-NO:  if( state == "WHO_CARES")state = "DONT_DO_IT";

If this seems to be an overkill for UI components here is another approach. Use Qml to depict logic of your application-engine-native code. Qml file that contains a state machine which gets "compiled" during first instance instantiation. 
    property variant smBody: {
        "name": "TellMeWhatToDo",
        "states": [
            {
                "name":"EXCEPTIONS",		// place where all non-processed events go to
                "todolist": []
            },
            {
                "name":"OUTSIDE",
                "todolist": [
                    {"event": "MOUSE_INSIDE",    "action": "SHOW_TEXT_INSIDE",   "next": "INSIDE"}
                ]
            },
            {
                "name": "INSIDE",
                "heartbeatrate": 1000,
                "heartbeats": 2,
                "todolist": [
                    {"event": "MOUSE_OUTSIDE",   "action": "SHOW_TEXT_OUSIDE",   "next": "OUTSIDE"}
                    {"event": "HEARTBEAT",       "action": "STILL_THINKING_?",   "next": ""}
                    {"event": "LASTHEARTBEAT",   "action": "YOU_ARE_TOO_SLOW",   "next": "EXIT"}
                ]
            }
        ]
    }

    StateMachine {		// defined in plugin
        id: myStateMachine
    }

    Component.onCompleted: {
         myStateMachine.start(smBody);
    }

What do you think guys?

Thanks
Vlad



More information about the Qt-interest-old mailing list