[Qt-interest] unable to use more than 1 transition added to a state (QT StateMachine Framework)

Sean Harmer sean.harmer at maps-technology.com
Thu Mar 18 13:05:28 CET 2010


Hi,

On Thursday 18 March 2010 11:35:59 Mandeep Sandhu wrote:
> Thanks for the test program Sean.
> 
> But unfortunately, this won't work for me. What I had shown in my SM
> was just a sample. The real SM has _many_ more states and transitions.
> Eg, there's an ERROR transition that can occur from ~ 14 different
> states, which means I'd have to add those many transition for this
> event alone (and there are around ~11-12 such events).

Ah OK sorry this wasn't clear to me form your original messages.

> The Qt SM framework is based on the hierarchical SM as shown here:
> http://www.wisdom.weizmann.ac.il/~dharel/SCANNED.PAPERS/Statecharts.pdf
> (Harrel's statecharts)
> 
> I need to make use of state groups (parallel/normal) to implement the
> SM with less number of transitions...as adding so many would be
> cumbersome and possibly error prone.
> 
> One of the soln's that I had thought for the second SM (with s3), was:
> 
> - put s0,s2 in sg1 and add a transition for e1 with target as s1. from
> s1 add transition to s2
> - put sg1, s1 in another group sg2. add transition for e3 to goto s3.
> - set initial state of sg2 as sg1 and initial state of sg1 as s0

Yes that will work.

> Now this has a problem that we can transition from s0 to s3 on e3
> (which is not valid according to the state dia).

> So I put a "dummy" transition on s0 (for e3) with target state as s0
> itself. So when in s0, and we get e3, the SM stays in s0.
> 
> I can extend this idea to my SM except that this would require quite a
> lot of dummy transitions. I'm not convinced that this is the only way.
> 
> Anybody has suggestion?

Yes, you can easily get around this by two methods:

(1) Do not group s0 with s2 in sg1 since s0 is a special case. ie Ask yourself 
does s0 and s2 really have a common behavioral parent state that it should 
inherit from or are you just grouping them under a common parent for 
convenience. That is, are you really decomposing your system into the correct 
hierarchy of states in the first place?

(2) If they really should be grouped together under a common parent but you 
wish to prevent the transition from sg1 to s1 if you are actually in the state 
s0, then this can be achieved by adding an extra conditional test for the 
transition between sg1 and s1 which checks that the SM is not in the state s0.

To do this, inherit a class from the CustomEventTransition class that I sent 
you and reimplement the bool eventTest( QEvent* e ) function again. The new 
derived class implementation should call the base class implementation, then 
if that passes check its own conditions. For e.g.:

bool MySpecialTransition::eventTest(QEvent* e)
{
    if ( !CustomEventTransition::eventTest( e ) )
        return false;

    // Check that we are not in state s0
    // m_s0 is a pointer to the s0 state object
    QSet<QAbstractState> configuration = machine()->configuration();
    return !configuration.contains( m_s0 ); 
}

This way you can "override" the behaviour that a child state inherits from its 
parent. All you need to do is to provide a pointer to the s0 state to this 
specialised transition class when you create it so that it knows what to 
check.

This will do away with the need for all of your "dummy" transitions and keeps 
your SM neat by simply providing some extra guards on the transitions where 
needed.

HTH,

Sean




More information about the Qt-interest-old mailing list