// 4. Use State Machine | 使用状态机 UntypedStateMachinefsm= builder.newStateMachine("A"); fsm.fire(FSMEvent.ToB, 10);
System.out.println("Current state is " + fsm.getCurrentState()); }
}
输出:
1 2 3 4 5 6 7 8 9 10
16:25:32.202 [main] DEBUG org.squirrelframework.foundation.fsm.impl.StateImpl - State "A" entry. 16:25:32.206 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'STATE_ENTRY__A' invoked. 16:25:32.210 [main] DEBUG org.squirrelframework.foundation.fsm.impl.StateImpl - State "A" exit. 16:25:32.211 [main] DEBUG org.squirrelframework.foundation.fsm.impl.StateImpl - State "B" entry. 16:25:32.211 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'STATE_EXIT__A' invoked. Transition from 'A' to 'B' on event 'ToB' with context '10'. 16:25:32.216 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'TRANSITION__A-[ToB, Always, 1, EXTERNAL]->B' invoked. Entry State 'B'. 16:25:32.216 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'STATE_ENTRY__B' invoked. Current state is B
StateMachineBuilder<...> builder = StateMachineBuilderFactory.create( MyStateMachine.class, MyState.class, MyEvent.class, MyContext.class); builder.externalTransition().from(A).to(B).on(toB).callMethod("fromAToB"); // 所有的转换行为方法都跟随状态机类 // All transition action method stays with state machine class publicclassMyStateMachine<...> extendsAbstractStateMachine<...> { protectedvoidfromAToB(MyState from, MyState to, MyEvent event, MyContext context) { // this method will be called during transition from "A" to "B" on event "toB" // the action method parameters types and order should match ... } }
protectedvoidtransitFromAToBOnGoToB(MyState from, MyState to, MyEvent event, MyContext context)
命名为transitFrom[SourceStateName]To[TargetStateName]On[EventName],参数化为[MyState, MyState, MyEvent, MyContext]将被添加到转换“A-(GoToB)->B”动作列表中。当接收到事件’ GoToB ‘触发状态’ A ‘转换到状态’ B ‘时,将调用此方法。
1
protectedvoidtransitFromAnyToBOnGoToB(MyState from, MyState to, MyEvent event, MyContext context)
transitFromAnyTo[TargetStateName]On[EventName]该方法将在事件’ GoToB ‘上从任何状态转移到状态’ B ‘时调用。
1
protectedvoidexitA(MyState from, MyState to, MyEvent event, MyContext context)
该方法将在退出状态’ A ‘时被调用。entry[StateName], beforeExitAny/afterExitAny和beforeEntryAny/afterEntryAny也是如此。
// since 0.3.1 // the same effect as add method transitFromAnyToCOnToC in your state machine | 和transitFromAnyToCOnToC效果类似 builder.transit().fromAny().to("C").on("ToC").callMethod("fromAnyToC"); // the same effect as add method transitFromBToAnyOnToC in your state machine | 和transitFromBToAnyOnToC效果类似 builder.transit().from("B").toAny().on("ToC").callMethod("fromBToAny"); // the same effect as add method transitFromBToAny in your state machine | 和transitFromBToAny效果类似 builder.transit().from("B").toAny().onAny().callMethod("fromBToAny");
// transitions(A->B@A2B=>a2b, A->C@A2C=>a2c, A->D@A2D) will be defined at once builder.transitions().from(State._A).toAmong(State.B, State.C, State.D). onEach(Event.A2B, Event.A2C, Event.A2D).callMethod("a2b|a2c|_");
// transitions(A->_A@A2ANY=>DecisionMaker, _A->A@ANY2A) will be defined at once builder.localTransitions().between(State.A).and(State._A). onMutual(Event.A2ANY, Event.ANY2A). perform( Lists.newArrayList(newDecisionMaker("SomeLocalState"), null) );
@Transitions({ @Transit(from = "A", to = "B", on = "toB", callMethod = "fromAToB"), @Transit(from = "B", to = "C", on = "toC"), @Transit(from = "C", to = "D", on = "toD"), @Transit(from = "D", to = "A", on = "toA", callMethod = "transitFromDToAOntoA") }) @StateMachineParameters(stateType = String.class, eventType = TestEvent.class, contextType = Integer.class) staticclassUntypedStateMachineSampleextendsAbstractUntypedStateMachine { // No need to specify constructor anymore since 0.2.9 // protected UntypedStateMachineSample(ImmutableUntypedState initialState, // Map<Object, ImmutableUntypedState> states) { // super(initialState, states); // }
protectedvoidfromAToB(String from, String to, TestEvent event, Integer context) { // transition action still type safe ... System.out.println("Transition from '" + from + "' to '" + to + "' on event '" + event + "' with context '" + context + "'."); }
protectedvoidtransitFromDToAOntoA(String from, String to, TestEvent event, Integer context) { // transition action still type safe ... System.out.println("Transition from '" + from + "' to '" + to + "' on event '" + event + "' with context '" + context + "'."); } }
publicstaticvoidmain(String[] args) { UntypedStateMachineBuilderbuilder= StateMachineBuilderFactory.create(UntypedStateMachineSample.class); // state machine builder not type safe anymore // builder.externalTransition().from("D").to("A").on(TestEvent.toA); // UntypedStateMachine fsm = builder.newStateMachine("A"); UntypedStateMachinefsm= builder.newStateMachine("D"); fsm.fire(TestEvent.toA, 8); } }
输出:
1 2 3 4 5 6 7 8
18:31:10.629 [main] DEBUG org.squirrelframework.foundation.fsm.impl.StateImpl - State "D" entry. 18:31:10.633 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'STATE_ENTRY__D' invoked. 18:31:10.635 [main] DEBUG org.squirrelframework.foundation.fsm.impl.StateImpl - State "D" exit. 18:31:10.636 [main] DEBUG org.squirrelframework.foundation.fsm.impl.StateImpl - State "A" entry. 18:31:10.636 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'STATE_EXIT__D' invoked. Transition from 'D' to 'A' on event 'toA' with context '8'. 18:31:10.638 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'TRANSITION__D-[toA, Always, 1, EXTERNAL]->A' invoked. 18:31:10.638 [main] DEBUG org.squirrelframework.foundation.fsm.impl.AbstractExecutionService - Actions within 'STATE_ENTRY__A' invoked.
@Override protectedvoidafterTransitionCausedException(Object fromState, Object toState, Object event, Object context) { ThrowabletargeException= getLastException().getTargetException(); // recover from IllegalArgumentException thrown out from state 'A' to 'B' caused by event 'ToB' if(targeException instanceof IllegalArgumentException && fromState.equals("A") && toState.equals("B") && event.equals("ToB")) { // do some error clean up job here | 执行异常清理工作 // ... | 异常恢复逻辑 // after recovered from this exception, reset the state machine status back to normal setStatus(StateMachineStatus.IDLE); // 异常逻辑恢复之后设置状态机为正常状态 } elseif(...) { // recover from other exception ... } else { super.afterTransitionCausedException(fromState, toState, event, context); } }
// defines two region states "RegionState1" and "RegionState2" under parent parallel state "Root" builder.defineParallelStatesOn(MyState.Root, MyState.RegionState1, MyState.RegionState2);