2

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).

What I want to model [Modified, based on suggestions]

State Diagram: what I'm trying to implement

What I know how to implement (code below)

State Diagram: I know how to implement this

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) { }
}

// State Machine implementation
public class MyStateMachine : StateMachine<MyState, MyEvent>
{
    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) { }
    }
}

Edit: I think the crux of the issue is how to illustrate/implement transitions from a substate of a given superstate to a substate of a different superstate when the transition depends on the current substate in the original superstate. To use my example, if the current state is "P" inside superstate "Neutral", how can I illustrate/implement my state machine to show that event Y+ will transition out of the Neutral superstate altogether, into the "Not Neutral" superstate, and specifically into the "P" substate.

weberc2
  • 7,423
  • 4
  • 41
  • 57
  • Make Neutral a StateMachine as well that has an additional state ("Not In Neutral"). Whenever it enters that state trigger a state change in the parent state machine. – Chris Pfohl Nov 30 '12 at 16:41
  • 1
    I guess your edit also goes for the bottom C beeing actually a D and F...? – CSharpie Nov 30 '12 at 16:59
  • It does indeed. Thanks for the catch. – weberc2 Nov 30 '12 at 17:01
  • Windows Workflow will be a great tool for this ..see this video .. http://channel9.msdn.com/Shows/Workflow-TV/Workflow-TV-Tracking-StateMachines .. Also is it possible for you to add a description for one the transitions – Amitd Nov 30 '12 at 17:05
  • Hmm, perhaps I'll look into that in the future; however, this is just a quick utility for a single project. – weberc2 Nov 30 '12 at 17:07
  • I removed my suggestion to avoid confusion, due to my limited english skills i fail to explain what i mean. – CSharpie Nov 30 '12 at 17:32
  • I see. I don't suppose you speak French? ;) – weberc2 Nov 30 '12 at 17:34
  • 1
    @CSharpie Also, I updated my question, by the way. – weberc2 Nov 30 '12 at 17:35

1 Answers1

0

You might want to actually create statemachines for P, AB, CD and EF moving via X. Each state will be configured with substatemachines. The "main" statemachine will move thru P, AB, CD and EF via X transitions while the substatemachines will move in the Neutral Not Neutral Direction via Y transitions. Please let me know if I need to elaborate this in more detail.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • Could you elaborate on how to implement (i.e., code or pseudo-code) the passing of control between the X-based state machines and the Y-based? – weberc2 Dec 03 '12 at 13:43