Click or drag to resize
StaMaCreate a Basic State Diagram and Generate Code from It Using StaMa Visio Shapes

Explains how to create a basic state diagram in Microsoft Visio and generate C# code for a StaMa state machine from the diagram.

State diagrams for the StaMa code generator are created through the StaMaShapes.vst Microsoft Visio template. The StaMaShapes.vst template contains all neccessary shapes to draw a state diagram plus a set of VBA macros to generate code and a default state diagram as the starting point. Double clicking the StaMaShapes.vst template file in Windows Explorer launches Microsoft Visio and creates a normal Microsoft Visio drawing which is a copy of the entire template file. Hence the new drawing is self-contained and contains all the shapes and VBA macros neccessary to extend the default state diagrams and to generate code.

This tutorial assumes that Microsoft Visio 32-bit version (more info) is installed and the reader has basic knowledge about how to use the application. The StaMaShapes.vst template doesn't require a specific Microsoft Visio version and should be compatible with all versions since 2002, however is currently tested only with Microsoft Visio 2010.

Prerequisite: Preparing the Microsoft Visual Studio project as a target for the generated code

  1. Depending on the target framework:

    Execute the steps from Implementing a Simple State Machine Application for .NET Micro Framework or use the StaMa_State_Machine_Controller_Library\Samples\netmf\SampleSimpleStateMachineNETMF\SampleSimpleStateMachineNETMF.csproj project from the StaMa_State_Machine_Controller_Library_Vx.y.zip release package.

    -or-

    Execute the steps from Implementing a Simple State Machine Application for .NET Windows Desktop Framework or use the StaMa_State_Machine_Controller_Library\Samples\netmf\SampleSimpleStateMachine\SampleSimpleStateMachineNETMF.csproj project from the StaMa_State_Machine_Controller_Library_Vx.y.zip release package.

  2. Compile and run the Microsoft Visual Studio project to ensure its quality.

The above steps yield a minimal executable state machine project that can be used as the target for the code generator.

Creating the Microsoft Visio diagram file

  1. Locate the StaMaShapes.vst template file from the StaMa_State_Machine_Controller_Library_Vx.y.zip release package in Windows Explorer.

  2. Double click the StaMaShapes.vst file. Microsoft Visio will start with a new diagram created from the StaMaShapes.vst file.

    -or-

    Start Microsoft Visio. In the File ribbon select New. In the Other Ways to Get Started panel at the bottom of the application window click New from existing. A file selection dialog appears. Select the StaMaShapes.vst file and press Create New.

    -or-

    Install the StaMaShapes.vst file according to Installing StaMa on a Developer Machine. In the File ribbon select New. In the Template Categories panel double click StaMaShapes.

  3. Save the new diagram with the name SampleSimpleStateMachine.vsd in the project directory as a sibling file to the .csproj.

The StaMaShapes.vst template brings with a diagram containing a root region with two states State1 and State2, a transition from State1 to State2 that can be triggered through the event Event1, and a reverse transition that automatically returns from State2 to State1 (triggered through a completion event).

Also there are two boxes outside of the root region that represent attributes for the code generator.

Generating C# code for a StaMa state machine from the Microsoft Visio diagram

  1. Locate the rectangular code generator shapes in the diagram. Check that these Shapes are connected with the root region execution order box.

    Check that the path value of the Target File property matches the SampleSimpleStateMachineNETMF.cs file of the Microsoft Visual Studio project.

  2. In the Microsoft Visio Add-Ins ribbon locate and press the StaMa menu button. A menu appears. From the menu execute the Generate code menu entry to generate the code into the target file.

    -or-

    Ensure Microsoft Visio is the active application and no dialogs are open. Then press Ctrl+G to generate the code into the target file.

  3. Open or activate Microsoft Visual Studio with the target project.

    Open the SampleSimpleStateMachineNETMF.cs file that is already part of the project.

    Check that there is a block of text similar to the following:

    SampleSimpleStateMachineNETMF.cs
    //## Begin StateMachineTemplateNN
    // Generated from <file:...\SampleSimpleStateMachineNETMF.vsd>
    // at DD-MM-YYYY HH:MM:SS using StaMaShapes Version 2
    t.Region(State1, false);
        t.State(State1, null, ExitState1);
            t.Transition(Transi4, State2, Event1, null, null);
        t.EndState();
        t.State(State2, EnterState2, null);
            t.Transition(Transi6, State1, null, null, null);
        t.EndState();
    t.EndRegion();
    //## End StateMachineTemplateNN

    Check that the DD-MM-YYYY HH:MM:SS values reflect the timepoint when the code was generated.

    Move the entire block including the lines marked with //## to the location where the similar manually written code resides and remove the manually written code. Save the SampleSimpleStateMachineNETMF.cs file.

  4. Compile and run the Microsoft Visual Studio project to ensure its quality.

Generating code again from the SampleSimpleStateMachine.vsd Microsoft Visio diagram will now search for the //## Begin StateMachineTemplateNN and //## End StateMachineTemplateNN markers and generate the code into this location by overwriting the previous content. The previous file content is kept with a .bak file extension.

See Also