Welcome
Click or drag to resize
StaMaWelcome
About the StaMa State Machine Controller Library

StaMa provides an execution algorithm for state machines. Advanced state machine concepts like composite state, orthogonal sub-region, fork transition, join transition and history state are supported.

Using StaMa a state machine is created by setting up a structure of state, transition and region objects in a compact chunk of code using C#. The structure directly reflects the tree structure of the state machine diagram. Translating a UML state machine diagram into StaMa state, transition and region objects is straightforward.

Based on the Microsoft Visio drawing application the StaMa State Machine Controller Library additionally provides a set of prepared graphic objects (shapes) to draw state machine diagrams in Microsoft Visio and generate the StaMa related C# code from it. The code generator requires the Microsoft Visio 32-bit version (more info).

Particular attention has been given to the aspect that StaMa shall not use any operating system functionality like threads, timers, locks and other synchronisation mechanisms. This enhances the use of the library in a broad range of projects with an existing threading, synchronization and tracing infrastructure.

StaMa is available as a NuGet package, see Installing StaMa on a Developer Machine for details.

The following sample diagram and code shows how to setup the StaMa state machine structure for a diagram and how to send events to the state machine to trigger the execution of transitions.

Welcome Sample
Figure 1: Disk player controller
WelcomeSample.cs
using System;
using StaMa;

class Program
{
    static void Main(string[] args)
    {
        StateMachineTemplate t = new StateMachineTemplate();
        t.Region("Stopped", false);
            t.State("Stopped");
                t.Transition("T1", "Running", "Play");
            t.EndState();
            t.State("Loaded", StartMotor, StopMotor);
                t.Transition("T2", "Stopped", "Stop");
                t.Region("Running", false);
                    t.State("Running", EngageHead, ReleaseHead);
                        t.Transition("T3", "Paused", "Pause");
                    t.EndState();
                    t.State("Paused");
                        t.Transition("T4", "Running", "Play");
                    t.EndState();
                t.EndRegion();
            t.EndState();
        t.EndRegion();

        StateMachine stateMachine = t.CreateStateMachine();

        stateMachine.Startup();
        stateMachine.SendTriggerEvent("Play");
        stateMachine.SendTriggerEvent("Pause");
        stateMachine.SendTriggerEvent("Stop");
        stateMachine.Finish();
    }

    private static void StartMotor(StateMachine stateMachine, object triggerEvent, EventArgs eventArgs)
    {
        System.Console.WriteLine("StartMotor");
    }

    private static void StopMotor(StateMachine stateMachine, object triggerEvent, EventArgs eventArgs)
    {
        System.Console.WriteLine("StopMotor");
    }

    private static void EngageHead(StateMachine stateMachine, object triggerEvent, EventArgs eventArgs)
    {
        System.Console.WriteLine("EngageHead");
    }

    private static void ReleaseHead(StateMachine stateMachine, object triggerEvent, EventArgs eventArgs)
    {
        System.Console.WriteLine("ReleaseHead");
    }
}

The "Play", "Pause" or "Stop" events trigger the execution of transitions and as a result the exit actions and entry actions StartMotor, StopMotor, EngageHead and ReleaseHead of the involved states.

One or more state machine instances may be created from the same immutable state machine template structure in order to economize resources.

Older Versions
About this document

This helpfile contains information about how to use the StaMa State Machine Controller Library.

It is not intended to be a general introduction to state machine modelling.

The main parts are

  • Getting Started: How to deploy StaMa for the different target environments.

  • Programming State Machines: How to use StaMa to implement basic and advanced state machine concepts like composite state, orthogonal sub-region, fork transition, join transition and history state.

  • Samples: Explanations for the samples in the package.

  • Using Visio Shapes: How to use the attached Microsoft Visio shapes to draw a state machine diagram and generate the state machine code from the drawing.

About the author

Not really about me but about my point of view:

Within the many years of my professional life as an application and system developer I was exposed to only a moderate number of components where a state machine approach seemed to be reasonable. If however the problem is suitable, the state machine approach can unfold its power and yields a software implementation with unequaled robustness, predictability of behavior and ability to analyse the behavior.

It seems that the real complexity of software components with dynamic behavior is often understood very late during software development process. Sometimes only after the code has been completed and behavior is somewhat settled but leaves some bugs or unexpected behavior. Availability of an established state machine execution engine might encourage developers to take the step and design the component to use the state machine approach.

A graphical representation of the state machine is essential for efficient communication and understanding of the software. The state machine diagram is the street map of the behavior, without it the developer is lost in the urban canyons of the code. Needless to say that the graphical representation must be in sync with the implementation at any time. This can easiest be achieved by generating the code from the graphical representation. Round trip or reverse generation of the graphical representation from the code turned out to be unsatisfactory.