Finite State Machine utility for RL2

Scott Enders's Avatar

Scott Enders

22 Jun, 2013 12:16 AM

Hello All,
I have been using RL1 for some time and love it. I am just now trying to get my feet wet with RL2. With RL1, I always use the robotlegs-utilities-StateMachine utility. But I am not sure if I can still use this utility in RL2 as is or must it be altered to work in RL2? Is there a new better option for RL2? Thanks!

  1. Support Staff 1 Posted by Ondina D.F. on 22 Jun, 2013 07:29 AM

    Ondina D.F.'s Avatar

    Hello Enders,
    Joel stated on github that he removed all dependencies to Robotlegs from the StateMachine. That means you could use it with rl2 as well.
    There is also Neil’s statemachine(s) : https://github.com/AS3StateMachine
    I can’t say whether it works with rl2 though.

    Not the same as a StateMachine, but you might want to take a look at rl2’s Lifecycle:
    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...

    hth

    Ondina

  2. 2 Posted by Scott Enders on 22 Jun, 2013 02:35 PM

    Scott Enders's Avatar

    Hello Ondina,
    Thanks for your help. Would it be suggested to initialize the StateMachine related code in an extension?

  3. Support Staff 3 Posted by Ondina D.F. on 22 Jun, 2013 02:57 PM

    Ondina D.F.'s Avatar

    Hi Scott,

    You’re welcome!

    Would it be suggested to initialize the StateMachine related code in an extension?

    Yes, I think it would be a good idea.

    Ondina

  4. 4 Posted by Scott Enders on 24 Jun, 2013 05:25 PM

    Scott Enders's Avatar

    Hello again,
    I tried initializing the StateMachine code in a custom IExtension class, but I was unable to get reference to the IEventDispatcher from the extension. Trying to inject it would return null. An instance of the IEventDispatcher is necessary for the StateMachine class constructor.

    So, I put the code in an IConfig class instead. From the config file I am able to get reference to the eventDispatcher (via injection). I tested out the functionality of the StateMachine code and it works as expected.

    [Inject]
    public var eventDispatcher : IEventDispatcher;
    
    public function configure() : void {
        var fsmInjector : FSMInjector = new FSMInjector(fsm);
        var stateMachine : StateMachine = new StateMachine(eventDispatcher);
        fsmInjector.inject(stateMachine);
    }
    

    I am wondering if this is best practice for such a thing. Is this the way to go or should I have this in an extension? If the extension is best practice, what am I missing to get it to work properly?

  5. Support Staff 5 Posted by Ondina D.F. on 25 Jun, 2013 09:18 AM

    Ondina D.F.'s Avatar

    Hi Scott,

    I think you’ve already read this:

    https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...

    Perhaps you forgot to install YourExtension?

    context = new Context()
                    .install(MVCSBundle)
                    .install(YourExtension)//<==
                    .configure(new ContextView(view));
    

    Also, take a look at the various rl extensions, for example:
    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...

    Let us know if that helped.

    Ondina

  6. Support Staff 6 Posted by creynders on 25 Jun, 2013 10:05 AM

    creynders's Avatar

    TBH I do think it's a good idea to put it in a config file, instead of an
    extension. Extensions are like plugins, they should add to the
    functionality.
    If I remember correctly extensions aren't injected with anything except for
    the context as a parameter to the `extend` method.
    Which means you could access the Event Dispatcher instance in the `extend`
    method with

    @@@
    context.injector.getInstance(IEventDispatcher);
    @@@

    But as I said, I wouldn't recommend it.

  7. 7 Posted by Scott Enders on 25 Jun, 2013 01:25 PM

    Scott Enders's Avatar

    @Ondina - Yeah, I did install my extension. The problem was I did not know how to get reference to anything other than the context (via the parameter of extend method). I was trying to inject the eventDispatcher using the Inject meta tag. But it always came back null. I guess injection isn't allowed in extensions.

    @creynders - Thanks for the info. I guess I will keep it in the config then. That seems like a better fit.

    Thanks for all your help.

  8. Scott Enders closed this discussion on 25 Jun, 2013 01:26 PM.

  9. Ondina D.F. re-opened this discussion on 25 Jun, 2013 02:02 PM

  10. Support Staff 8 Posted by Ondina D.F. on 25 Jun, 2013 02:02 PM

    Ondina D.F.'s Avatar

    Scott, this should actually work:

    context = new Context()
        .install(MVCSBundle)
        .install(SomeExtension)
        .configure(new ContextView(view));
    
    public class SomeExtension implements IExtension
    {
        private var _eventDispatcher:IEventDispatcher;
    
        public function SomeExtension(eventDispatcher:IEventDispatcher = null)
        {
            _eventDispatcher = eventDispatcher || new EventDispatcher();
        }
    }
    

    Or, like creynders suggested:

    public class SomeExtension implements IExtension
    {
    
        private var _context:IContext;
    
        private var _eventDispatcher:IEventDispatcher;
    
        public function SomeExtension()
        {
        }
    
        public function extend(context:IContext):void
        {
            _context = context;
            _eventDispatcher = context.injector.getInstance(IEventDispatcher)
        }
    }
    
  11. 9 Posted by Scott Enders on 25 Jun, 2013 08:42 PM

    Scott Enders's Avatar

    For everyone's benefit, here is the config class I came up with while testing:

    package scott.config {
        import robotlegs.bender.framework.api.IConfig;
        import robotlegs.bender.framework.api.IContext;
    
        import org.robotlegs.utilities.statemachine.FSMInjector;
        import org.robotlegs.utilities.statemachine.StateMachine;
    
        import flash.events.IEventDispatcher;
        /**
         * @author Scott Enders
         */
        public class StateMachineConfig implements IConfig {
    
            //--------------------------------------------------------------------------------------------------
            //  PUBLIC PROPERTIES 
            //--------------------------------------------------------------------------------------------------
    
            [Inject]
            public var context : IContext;
    
            [Inject]
            public var eventDispatcher : IEventDispatcher;
    
            //--------------------------------------------------------------------------------------------------
            //  PRIVATE PROPERTIES 
            //--------------------------------------------------------------------------------------------------
    
            private var fsm : XML = <fsm initial="start">
                    <state name="start">
                        <transition action="toEnd" target="end"/>
                    </state>
                    <state name="end">
                        <transition action="toStart" target="start"/>
                    </state>
                </fsm>;
    
            //--------------------------------------------------------------------------------------------------
            //  PUBLIC METHODS 
            //--------------------------------------------------------------------------------------------------
    
            public function configure() : void {
                trace("StateMachineConfig.configure()");
                var fsmInjector : FSMInjector = new FSMInjector(fsm);
                var stateMachine : StateMachine = new StateMachine(eventDispatcher);
                fsmInjector.inject(stateMachine);
                
                // Allow the instance of the StateMachine to be injected into mediators
                context.injector.map(StateMachine).toValue(stateMachine);
            }
        }
    }
    
  12. Support Staff 10 Posted by Ondina D.F. on 27 Jun, 2013 07:56 AM

    Ondina D.F.'s Avatar

    Thanks for sharing your solution!

  13. Ondina D.F. closed this discussion on 27 Jun, 2013 07:56 AM.

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

Keyboard shortcuts

Generic

? 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