appccelerate / statemachine

A .net library that lets you build state machines (hierarchical, async with fluent definition syntax and reporting capabilities).
Apache License 2.0
496 stars 129 forks source link

Malfunctioning transition events? #60

Closed jgiacobbi closed 4 years ago

jgiacobbi commented 4 years ago

Hello, it appears that the transition completed event refers to the state you transitioned from. Is this intentional? Am I misunderstanding something? These are my events:

        private void TransitionBeginEvent(object sender, TransitionEventArgs<States, Events> e)
        {
            Log.Logger.Debug("{Event} satisfied, transitioning from {State}", e.EventId, e.StateId);
        }

        private void TransitionCompletedEvent(object sender, TransitionCompletedEventArgs<States, Events> e)
        {
            Log.Logger.Debug("TransitionCompleted Entered state {State}", e.StateId);
        }

This is a print function I added to each state to get the actual state I transitioned into:

            builder.In(States.Idle)
                .ExecuteOnEntry(SetIdle)
        private void SetIdle()
        {
            Log.Logger.Debug("Entered State {0}", States.Idle);
        }

This is the result:

[10:53:29 DBG] Entered State Idle
[10:53:29 DBG] Start satisfied, transitioning from Idle
[10:53:29 DBG] Leaving idle
[10:53:29 DBG] Entered State Disconnected
[10:53:29 DBG] Firing Connect
[10:53:29 DBG] TransitionCompleted Entered state Idle

Somewhat related, there doesn't appear to be a way to query the machine to get the state it's currently in. Again if I'm just wrong and someone can point me in the right direction I would really appreciate it.

It works great, it just seems a little difficult to get in flight logs about what it's doing. Thanks for your time.

ursenzler commented 4 years ago

Hi,

What do you try to accomplish? If you want to log then you should take a look at the state machine extensions. They provide you with much more information than the state machine events.

You can add an extension with machine.AddExtension(myExtension). You can derive your extension from Appccelerate.StateMachine.Extensions.ExtensionBase (non async/await version) or Appccelerate.StateMachine.AsyncMachine.AsyncExtensionBase (async/await version).

The events are a legacy, but NewStateId should get you the target state. StateId always points to the source state.

jgiacobbi commented 4 years ago

I'll look into extensions, sounds like that's what I was missing. Thank you.