Click or drag to resize
StaMaTracing State Changes, Transition Testing and Event Dispatching

Explains how to monitor what is going on in the state machine when events are sent to the state machine

In a sense a state machine is a black box that receives events and executes actions in response to the events. Although the rules are clearly defined what shall happen in case an event is sent to the state machine, it can sometimes turn out as complex to figure out what is actually going on in a non-trivial state machine.

StaMa provides a few hooks in the state machine execution algorithm that assist tracking the current state and the progress of evaluation of the events sent to the state machine.

All hooks are solely intended to provide tracing and debug information. They shall never send events to the state machine or change any data evaluated within guard conditions.

 

Tracing State Changes

Set the StateMachine.TraceStateChange property to track whenever the active state of a StateMachine instance changes.

TraceStateChange callback implementation
private static void TraceStateChange(StateMachine stateMachine, StateConfiguration stateConfigurationFrom, StateConfiguration stateConfigurationTo, Transition transition)
{
    string info = "ActiveState=\"" + stateConfigurationTo.ToString() + "\"" + " Transition=" + ((transition != null) ? "\"" + transition.Name + "\"" : "Startup/Finish");
    System.Console.WriteLine(info);
}

 

Tracing When Events Are Dequeued and What the Run to Completion Execution Does

Set the StateMachine.TraceDispatchTriggerEvent property to track whenever the state machine fetches an event instance from the queue as part of the run-to-completion processing and dispatches it to the state machine algorithm.

TraceDispatchTriggerEvent callback implementation
private void TraceDispatchTriggerEvent(StateMachine stateMachine, object triggerEvent, EventArgs eventArgs)
{
    String message = String.Format("Dispatch event {0} in state {1}",
                                   (triggerEvent != null) ? triggerEvent.ToString() : "*",
                                   stateMachine.ActiveStateConfiguration.ToString());
    System.Console.WriteLine(info);
}

 

Tracing Which Transitions Are Tested While an Event Is Evaluated by the State Machine

Set the StateMachine.TraceTestTransition property to track whenever the state machine reached a transition and will test if the transition can be executed.

TraceTestTransition callback implementation
private void TraceTestTransition(StateMachine stateMachine, Transition transition, object triggerEvent, EventArgs eventArgs)
{
    String message = String.Format("Test transition {0} with event {1} in state {2}",
                                   transition.ToString(),
                                   (triggerEvent != null) ? triggerEvent.ToString() : "*",
                                   stateMachine.ActiveStateConfiguration.ToString());
    System.Console.WriteLine(info);
}