package com.iflytop.gd.app.config; import com.iflytop.gd.common.enums.CraftEvents; import com.iflytop.gd.common.enums.CraftStates; import org.springframework.context.annotation.Configuration; import org.springframework.statemachine.config.EnableStateMachineFactory; import org.springframework.statemachine.config.StateMachineConfigurerAdapter; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; import java.util.EnumSet; @Configuration @EnableStateMachineFactory public class CraftsStateMachineConfig extends StateMachineConfigurerAdapter { @Override public void configure(StateMachineStateConfigurer states) throws Exception { states .withStates() .initial(CraftStates.READY) .states(EnumSet.allOf(CraftStates.class)); } @Override public void configure(StateMachineTransitionConfigurer trans) throws Exception { trans // 启动 .withExternal() .source(CraftStates.READY) .target(CraftStates.RUNNING) .event(CraftEvents.START) .and() // 单步完成(保持 RUNNING) .withInternal() .source(CraftStates.RUNNING) .event(CraftEvents.STEP_COMPLETE) .and() // 暂停 .withExternal() .source(CraftStates.RUNNING) .target(CraftStates.PAUSED) .event(CraftEvents.PAUSE) .and() // 恢复 .withExternal() .source(CraftStates.PAUSED) .target(CraftStates.RUNNING) .event(CraftEvents.RESUME) .and() // 用户手动停止:RUNNING → STOPPED .withExternal() .source(CraftStates.RUNNING) .target(CraftStates.STOPPED) .event(CraftEvents.STOP) .and() // 用户手动停止:PAUSED → STOPPED .withExternal() .source(CraftStates.PAUSED) .target(CraftStates.STOPPED) .event(CraftEvents.STOP) .and() // 出错 .withExternal() .source(CraftStates.RUNNING) .target(CraftStates.ERROR) .event(CraftEvents.ERROR_OCCUR) .and() // 正常完成 .withExternal() .source(CraftStates.RUNNING) .target(CraftStates.FINISHED) .event(CraftEvents.FINISH); } }