Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: StateMachine - crossing many states  (Read 7113 times)
trevorhartman
Newbie
*
Posts: 5


View Profile Email
« on: March 06, 2009, 02:43:55 »

Hi,

I'm attempting to use StateMachine for the first time and have a question about states and transitions.  In the open/close/lock door example I see how it's possible to limit which states are available from the current state.  However, in my app, using SWFAddress a user could conceivably go TO any state FROM any state by typing in a direct URL, BUT, it might be necessary to travel through multiple states to get there.

For simplicity sake, lets use an example of an app that moves a box to the 4 corners of your screen, each corner represented by a state.  Changing state tweens the box to the correct position, but it only moves horizontally and vertically, in a clockwise fashion.  States:

:
<fsm initial="TOP_LEFT">

<state name="TOP_LEFT">
<transition action="TO_TOP_RIGHT" target="TOP_RIGHT" />
</state>

<state name="TOP_RIGHT">
<transition action="TO_BOTTOM_RIGHT" target="BOTTOM_RIGHT" >
</state>

<state name="BOTTOM_RIGHT">
<transition action="TO_BOTTOM_LEFT" target="BOTTOM_LEFT" >
</state>

<state name="BOTTOM_LEFT">
<transition action="TO_TOP_LEFT" target="TOP_LEFT" />
</state>

</fsm>



So the site inits the box at TOP_LEFT and the user types in http://sweetboxapp.com/#/bottomright.  The box needs to move from TOP_LEFT to TOP_RIGHT, then from TOP_RIGHT to BOTTOM_RIGHT, essentially moving through states.  While this example is simple, in the app I'm developing a user could move through a maximum of 6 states.  It's important (or at least I think it is) to maintain the idea of moving through states because of the transitions that are required.  ie. TOP_LEFT can't go to BOTTOM_RIGHT directly, it must move through TOP_RIGHT.

Am I thinking about this correctly?

thanks,
Trevor

« Last Edit: March 06, 2009, 03:29:12 by trevorhartman » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: March 07, 2009, 11:45:51 »

The state machine only deals with one state at a time and the valid transitions out of it. You'll need to have some actor that understands what the path is between two disparate states. Let's say its a DestinationStateMediator. It gets registered with a State object as its (headless) view component by a command that took the incoming url and parsed out a State from it. This mediator listens for StateMachine.CHANGED note, which will contain the destination state. This thing needs to know the current state to start. (Check to see if current state public on StateMachine. Probably not, I may need to add the ability to query it. If it is public, we just retrieve the StateMachine and find out).

Anyway this DestinationStateMediator would have to say 'ok you're in the top left and headed toward bottom right. So I need to send you to top left now'. And it would send the ACTION not to push it into the topright state. Once there, a CHANGED note would show up saying the machine is at top right state, and this mediator would say 'oh, you're at top right and my destination state tels me you're headed for bottom right, and sends the note that scoots it to the bottom right position. Then once again it gets a StateMachine.CHANGED note and says 'oh, I se you're in the bottom right position now, and hey, what do you know, we've reached our destination!' And it promptly removes itself with a 'facade.removeMediator(this.getMediatorName());

For anything more than an immediate state transition you've got to have an intelligent driver that survives all those State changes and doggedly pushes you toward the destination.

-=Cliff>
Logged
trevorhartman
Newbie
*
Posts: 5


View Profile Email
« Reply #2 on: March 09, 2009, 10:03:45 »

Thanks for your detailed reply, Cliff.  I will try to implement your suggestion.
Logged
Pages: [1]
Print