C# - Coding a nested stateflow diagram

Posted by weberc2 on Stack Overflow See other posts from Stack Overflow or by weberc2
Published on 2012-11-30T16:39:13Z Indexed on 2012/11/30 17:04 UTC
Read the original article Hit count: 470

Filed under:
|
|
|
|

I have the following state diagram. I know how to make a simple state machine that transitions between non-nested states; however, I don't know how to transition between nested states. Could someone explain how to do this at an appropriately high level (i.e., you don't need to write the code for me--unless you're feeling particularly generous :P).

State Diagram [EDIT: The bottom "A", "C", and "E" should be "B", "D", and "F" respectively; sorry!]

State Diagram

What I know how to do

public class MyState : State // State enumeration
{
    public static MyState A = new MyState("State A");
    public static MyState B = new MyState("State B");
    public static MyState C = new MyState("State C");
    public static MyState D = new MyState("State D");
    public static MyState E = new MyState("State E");
    public static MyState F = new MyState("State F");
    public static MyState NEUT = new MyState("Neutral");
    public static MyState P = new MyState("P");
    protected MyState(string name) : base(name) { }
}

public class MyEvent : Event // Event enumeration
{
    public static MyEvent X_POS = new MyEvent("X+");
    public static MyEvent X_NEG = new MyEvent("X-");
    public static MyEvent Y_POS = new MyEvent("Y+");
    public static MyEvent Y_NEG = new MyEvent("Y-");

    protected MyEvent(string name) : base(name) { }
}

public class MyStateMachine : StateMachine<MyState, MyEvent> // State Machine implementation
{
    public MyStateMachine() : base(MyState.P) // MyState.P = initial state
    { // Set up the transition table
        // P
        this.addTransition(MYState.P, MyState.NEUT, MyEvent.Y_NEG);

        // NEUTRAL
        this.addTransition(MyState.NEUT, MyState.P, MyEvent.Y_POS);
        this.addTransition(MyState.NEUT, MyState.A, MyEvent.Y_NEG);
        this.addTransition(MyState.NEUT, MyState.B, MyEvent.Y_POS);
        this.addTransition(MyState.NEUT, MyState.C, MyEvent.Y_NEG);
        this.addTransition(MyState.NEUT, MyState.D, MyEvent.Y_POS);
        this.addTransition(MyState.NEUT, MyState.E, MyEvent.Y_NEG);
        this.addTransition(MyState.NEUT, MyState.F, MyEvent.Y_POS);

        // A
        this.addTransition(MyState.A, MyState.NEUT, MyEvent.Y_POS);

        // B
        this.addTransition(MyState.B, MyState.NEUT, MyEvent.Y_NEG);

        // C
        this.addTransition(MyState.C, MyState.NEUT, MyEvent.Y_POS);

        // D
        this.addTransition(MyState.D, MyState.NEUT, MyEvent.Y_NEG);

        // E
        this.addTransition(MyState.E, MyState.NEUT, MyEvent.Y_POS);

        // F
        this.addTransition(MyState.F, MyState.NEUT, MyEvent.Y_NEG);
    }

    public void move(MyEvent eevent)
    {
        try
        {
            this.moveNext(eevent);
        }
        catch (Exception e) { }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about uml