StateMachine Utility map command dynamically

vincent's Avatar


25 Aug, 2011 03:33 PM


I've implemented the state machine utility in my development using the joel hooks' example (
All work fine.

I'm wondering if we can omit the constant class and dynamically map events with command.
I thought I could add a "command" attribute in state nodes which would contain the command class path.
When configuring the state machine, I could iterate the state list and map events using the getDefinitionByName() method.
Class listed in the XML are not compiled in the application so I get the classic ReferenceError.

Is there any way to bypass the error without having to reference the class in my app ?

Thanks. lulu.

  1. 1 Posted by neil on 25 Aug, 2011 05:00 PM

    neil's Avatar

    What I do is to add an addClass method to the FSMInjector. I then do some reflection on the class ref, and keep the stats. The addClass ensures the class is included. I will add a link to illustrate later as I am mobile at the mo.

  2. 2 Posted by vincent on 25 Aug, 2011 05:45 PM

    vincent's Avatar

    Yes, an example would be great!

  3. 3 Posted by neil on 25 Aug, 2011 08:28 PM

    neil's Avatar

    hope this explains it:

    if not, feel free to ask more questions.

  4. 4 Posted by LulumOriss on 26 Aug, 2011 09:34 AM

    LulumOriss's Avatar

    Thanks for your reply and your code sample.
    My point is to save importing command classes when I initialize the state machine process. All I want to have to do is to create the command class and reference its path in the XML file.
    I don't know if it's a good idea after all. What do you think ?

  5. 5 Posted by neil on 26 Aug, 2011 10:16 AM

    neil's Avatar

    1) you will have to import your cmds somewhere, otherwise they wont compile.
    2) the alternative is to create a command factory
    3) You'll also have to map them at some point to the commandMap
    ( I assume thats what you want to do) surely the best place to map them is when you initialise the FSM

    not entirely sure what you are trying to do

  6. 6 Posted by vincent on 26 Aug, 2011 10:24 AM

    vincent's Avatar

    Yes, I may be not clear.
    I find the mapping process quite painful and I want to automate it. I initialize the state machine like this :

    var l_sti:FSMInjector = new FSMInjector( stmProxy.descriptor );
    var l_stm:StateMachine = new StateMachine( eventDispatcher );

    DisplayApplicationCommand; // I would like to avoid this line

    var l_slt:XMLList = stmProxy.descriptor..state;
    var l_ste:XML;
    for each( l_ste in l_slt )
          if( [email blocked]() == 0 || [email blocked]() == 0 )
          commandMap.mapEvent( l_ste.@changed[ 0 ], getDefinitionByName( l_ste.@command[ 0 ] ) as Class );

    l_sti.inject( l_stm );

    What's a command factory?

  7. 7 Posted by neil on 26 Aug, 2011 11:17 AM

    neil's Avatar

    A factory encapsulates the creation of classes.
    basically, you need some where to include the classes, I'm afraid there's no getting away with it, as AS3 doesn't support class loading

  8. 8 Posted by vincent on 26 Aug, 2011 11:26 AM

    vincent's Avatar

    Ok. Thanks a lot.

  9. vincent closed this discussion on 26 Aug, 2011 11:26 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac