我有以下状态图。我知道如何制作一个在非嵌套状态之间转换的简单状态机;但是,我不知道如何在嵌套状态之间转换。有人能解释一下如何在适当的高层次上做到这一点(即,你不需要为我编写代码——除非你感觉特别慷慨:P)。
我要建模的内容 [根据建议修改]
我知道如何实现(下面的代码)
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) { }
}
}
编辑:我认为问题的症结在于当转换取决于原始超状态中的当前子状态时,如何说明/实现从给定超状态的子状态到不同超状态的子状态的转换。使用我的示例,如果当前状态是超状态“中性”内的“P”,我如何说明/实现我的状态机以显示事件 Y+ 将完全从中性超状态过渡到“非中性”超状态,并具体进入“P”子状态。