// Transitions from NEW state .addTransition(RMAppState.NEW, RMAppState.NEW, RMAppEventType.NODE_UPDATE, new RMAppNodeUpdateTransition()) .addTransition(RMAppState.NEW, RMAppState.NEW_SAVING, RMAppEventType.START, new RMAppNewlySavingTransition()) ...// 若干状态转换的定义 .installTopology();
/** * State machine topology. * This object is semantically immutable. If you have a * StateMachineFactory there's no operation in the API that changes * its semantic properties. * * @param <OPERAND> The object type on which this state machine operates. * @param <STATE> The state of the entity. * @param <EVENTTYPE> The external eventType to be handled. * @param <EVENT> The event object. * */ finalpublicclassStateMachineFactory <OPERAND, STATEextendsEnum<STATE>, EVENTTYPEextendsEnum<EVENTTYPE>, EVENT> {...}
@Override publicvoidapply (StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> subject){ Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>> transitionMap = subject.stateMachineTable.get(preState); if (transitionMap == null) { // I use HashMap here because I would expect most EVENTTYPE's to not // apply out of a particular state, so FSM sizes would be // quadratic if I use EnumMap's here as I do at the top level. transitionMap = new HashMap<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>(); subject.stateMachineTable.put(preState, transitionMap); } transitionMap.put(eventType, transition); } }
// I use EnumMap here because it'll be faster and denser. I would // expect most of the states to have at least one transition. stateMachineTable = new EnumMap<STATE, Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>>(prototype); // 将TransitionsListNode链中的ApplicableSingleOrMultipleTransition入stack for (TransitionsListNode cursor = transitionsListNode; cursor != null; cursor = cursor.next) { stack.push(cursor.transition); } // 将ApplicableSingleOrMultipleTransition出stack, // 调用apply对stateMachineTable进行赋值 while (!stack.isEmpty()) { stack.pop().apply(this); } }
下面看下ApplicableSingleOrMultipleTransition.apply方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicvoidapply (StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> subject){ // 从stateMachineTable中拿到preState对应的transitionMap Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>> transitionMap = subject.stateMachineTable.get(preState); // transitionMap为null则new一个transitionMap的HashMap if (transitionMap == null) { // I use HashMap here because I would expect most EVENTTYPE's to not // apply out of a particular state, so FSM sizes would be // quadratic if I use EnumMap's here as I do at the top level. transitionMap = new HashMap<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>(); subject.stateMachineTable.put(preState, transitionMap); } // 将eventType对应的Transition放入transitionMap中 transitionMap.put(eventType, transition); }
// StateMachineFactory.doTransition private STATE doTransition (OPERAND operand, STATE oldState, EVENTTYPE eventType, EVENT event) throws InvalidStateTransitonException { // We can assume that stateMachineTable is non-null because we call // maybeMakeStateMachineTable() when we build an InnerStateMachine , // and this code only gets called from inside a working InnerStateMachine . Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>> transitionMap = stateMachineTable.get(oldState); if (transitionMap != null) { Transition<OPERAND, STATE, EVENTTYPE, EVENT> transition = transitionMap.get(eventType); if (transition != null) { return transition.doTransition(operand, oldState, event, eventType); } } thrownew InvalidStateTransitonException(oldState, eventType); }
此流程结束之后RMAppMaster的状态由RMAppState.NEW转移为RMAppState.NEW_SAVING,这套流程是在addTransition(RMAppState.NEW, RMAppState.NEW_SAVING, RMAppEventType.START, new RMAppNewlySavingTransition())中规定的。