[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 11:56:49 CET 2010
Hi,
On Thursday 18 March 2010 10:16:15 Mandeep Sandhu wrote:
> On Thu, Mar 18, 2010 at 3:07 PM, Sean Harmer
>
> <sean.harmer at maps-technology.com> wrote:
> > Hi,
> >
> > On Wednesday 17 March 2010 08:56:01 Mandeep Sandhu wrote:
> >> On further debugging this problem I found that the issue is because of
> >> attaching the same transition to more than 1 state.
> >>
> >> Can't a transition be attached to more than 1 state?
> >>
> >> Eg. If i define my transition as "On event E1, move to State S1"
> >>
> >> I attach this transition to S3 and S6...now if an event E1 occurs
> >> while the state machine is in S3 or S6, it should transition to S1.
> >> Correct?
> >>
> >> Or is this a bug in the state machine framework?
> >
> > This is not a bug in the SM framework. You need to use multiple instances
> > of your specialised guarded transition class. The reason is because each
> > instance
>
> Having multiple instances of my transition for the _same_ event would
> again violate the principle of Hierarchical SM's.
>
> I'll illustrate my SM which needed the same transition to be attached
> to 2 states:
>
> s0-(e1)->s1-(e2)->s2
> ^ |
>
> |__(e1)__|
>
> (this dia looks proper with fixed width font!:))
>
> So, on event e1 the SM transitions to s1, whether its in s0 or s2.
>
> To avoid 2 e1 transitions, I can club s0 and s2 in a super-group (sg1)
> and apply a single transition for e1 to this super-group. This would
> be within the principle of HSM's (there'll also be a transition from
> s1 to s2 on e2).
>
> Now, I add another state/transitions to the same SM as below:
>
> s1-(e3)->s3 and s2-(e3)->s3
>
> This (if seen alone) would require another super-group (sg2) which has
> s1 and s2 and 1 transition for e3 attached to sg2.
>
> But how do I combine sg1 & 2. Basically s2 exists in both these
> groups...While reading Harrel's statecharts its mentioned that
> sometimes for overlapping states duplication might be needed.
>
> Any ideas on how this canbe resolved the proper HSM way?
Yes, there is no need to introduce grouping states in this case from what you
have explained. With my poor ascii-art skills I have created the following
state chart (fixed width font required):
s3<-------
^ |
| e3 | e3
| |
s0-------->s1------->s2
e1 ^ e2 |
| |
`---------'
e1
which combines all the information that you gave. This is very easily created
with a guarded transition class instantiated a number of times like so:
// Define some custom event offsets
int e1 = 1024;
int e2 = 1025;
int e3 = 1026;
// Create states
QState* s0 = new QState( machine );
QState* s1 = new QState( machine );
QState* s2 = new QState( machine );
QState* s3 = new QState( machine );
// Create transitions
CustomEventTransition* s0Tos1 = new CustomEventTransition( QEvent::Type(
QEvent::User + e1 ), s0 );
s0Tos1->setTargetState( s1 );
CustomEventTransition* s1Tos2 = new CustomEventTransition( QEvent::Type(
QEvent::User + e2 ), s1 );
s1Tos2->setTargetState( s2 );
CustomEventTransition* s2Tos1 = new CustomEventTransition( QEvent::Type(
QEvent::User + e1 ), s2 );
s1Tos2->setTargetState( s1 );
CustomEventTransition* s1Tos3 = new CustomEventTransition( QEvent::Type(
QEvent::User + e3 ), s1 );
s1Tos3->setTargetState( s3 );
CustomEventTransition* s2Tos3 = new CustomEventTransition( QEvent::Type(
QEvent::User + e3 ), s2 );
s1Tos3->setTargetState( s3 );
// Add transitions to states
s0->addTransition( s0Tos1 );
s1->addTransition( s1Tos2 );
s1->addTransition( s1Tos3 );
s2->addTransition( s2Tos1 );
s2->addTransition( s2Tos3 );
The attached tarball includes the CustomEventTransition class and the above
example - it even compiles ;-). That should get you started. You just need to
add in the code that posts your e1, e2, e3 events to the state machine.
Good luck,
Sean
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sm-test.tar.gz
Type: application/x-compressed-tar
Size: 1211 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100318/4c706e6e/attachment.bin
More information about the Qt-interest-old
mailing list