public class FSMT {
public static void main(String[] args)throws Exception {
StateMachineBuilder.Builder builder = StateMachineBuilder.builder();
builder.configureConfiguration()
.withConfiguration()
.machineId("a").listener(listener())
;
//必须指定initial 过程中有多种状态变更需要指定 state states 指定end后当状态变更为end指定状态 后续流程将不再执行 比如BE.REFUND_2 event
builder.configureStates()
.withStates()
.initial(AE.INIT).states(EnumSet.allOf(AE.class)).end(AE.END)
// .and().withStates().initial(AE.STA).end(AE.ING)
;
//可以指定多个状态变更流程 event是钥匙 需要后续StateMachine发送这个事件
builder.configureTransitions()
.withExternal().event(BE.REFUND_FAILED).source(AE.INIT).target(AE.ING).action(BC.builder().build())
.and().withExternal().source(AE.ING).target(AE.INIT).event(BE.CLOSE_REFUND).action(AC.builder().build())
.and().withExternal().source(AE.INIT).target(AE.END).event(BE.REFUND_1).action(AC.builder().build())
.and().withExternal().source(AE.END).target(AE.INIT).event(BE.REFUND_2).action(AC.builder().build())
;
StateMachine sm = builder.build();
sm.start();
//3. 发送当前请求的状态
System.out.println(sm.sendEvent(BE.REFUND_FAILED));
System.out.println(sm.sendEvent(BE.CLOSE_REFUND));
System.out.println(sm.sendEvent(BE.REFUND_1));
System.out.println(sm.sendEvent(BE.REFUND_2));
}
public static StateMachineListenerlistener() {
return new StateMachineListenerAdapter() {
StringstateChanged ="stateChanged";
@Override
public void stateChanged(State from, State to) {
if (!Objects.isNull(from)) {
System.out.println(from.getId() +"~~~~~~~~" +stateChanged +"from");
}
if (!Objects.isNull(to)) {
System.out.println(to.getId() +"~~~~~~~~" +stateChanged +"to");
}
}
@Override
public void transition(Transition transition) {
System.out.println(transition.getTarget().getId() +"~~~~~~~~" +"transition");
}
};
}
}