Я пытаюсь настроить конечный автомат из файла yml с помощью SpringStateMachine Builder. Он прекрасно работает с состояниями и переходами, но когда я попытался включить некоторые дочерние состояния, я получаю следующее исключение.
org.springframework.statemachine.config.model.MalformedConfigurationException: Initial state не задан в org.springframework.statemachine.config.model.verifier.BaseStructureVerifier.verify (BaseStructureVerifier. java: 59) ~ [spring-statemachine-core-2.1.3.RELEASE.jar: 2.1.3.RELEASE] в org.springframework. .statemachine.config. getStateMachine (AbstractStateMachineFactory. java: 143) ~ [spring-statemachine-core-2.1.3.RELEASE.jar: 2.1.3.REL EASE] at org.springframework.statemachine.config.StateMachineBuilder $ Builder.build (StateMachineBuilder. java: 155) ~ [spring-statemachine-core-2.1.3.RELEASE.jar: 2.1.3.RELEASE]
Вот мой файл yml
states:
initial: INITIAL_STEP
end: END_STEP
states:
INITIAL_STEP :
SECOND_STEP :
- SECOND_STEP_ONE
- SECOND_STEP_TWO
- SECOND_STEP_THREE
END_STEP :
transList:
-
source: INITIAL_STEP
target: SECOND_STEP
event: INITIAL_STEP_UP
action: initialAction
-
source: SECOND_STEP
target: END_STEP
event: SECOND_STEP
action: secondAction
Вот мой Spring State машина Класс
@Configuration
public class MyStateMachineEngine {
private static Logger logger = LogManager.getLogger(MyStateMachineEngine.class);
@Autowired
private StateConfig stateconfig;
@Bean(name = "authStateMachine")
@SessionScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public StateMachine<String, String> buildStateEngine() {
Builder<String, String> builder = StateMachineBuilder.builder();
try {
if (stateconfig != null) {
Map<String,List<String>> stateListMap = stateconfig.getStates();
List<TransProp> transList = stateconfig.getTransList();
final StateConfigurer<String, String> stateConfigurer = builder.configureStates().withStates()
.initial(stateconfig.getInitial()).end(stateconfig.getEnd());
stateListMap.keySet().forEach(state -> configureState(state,stateListMap.get(state), stateConfigurer));
logger.info(transList.size());
transList.forEach(trans -> addTransaction(builder, trans));
builder.configureConfiguration().withConfiguration().autoStartup(true).beanFactory(null)
.machineId("OSTMACHINE");
} else {
logger.error("State initialization error please verify state config");
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.build();
}
private void configureState(String state, List<String> childStateList, StateConfigurer<String, String> stateConfigurer) {
stateConfigurer.state(state);
if(!childStateList.isEmpty()) {
childStateList.forEach(childState -> stateConfigurer.parent(state).initial(childState).state(childState));
}
}
private void addTransaction(Builder<String, String> builder, TransProp transProp) {
try {
if (null != transProp) {
if (StringUtils.isBlank(transProp.getGuard())) {
builder.configureTransitions().withExternal().source(transProp.getSource())
.target(transProp.getTarget()).event(transProp.getEvent());
} else {
builder.configureTransitions().withExternal().source(transProp.getSource())
.target(transProp.getTarget()).guard(new BaseGuard()).event(transProp.getEvent());
}
}
} catch (Exception e) {
logger.error("Error when configuring states ", e);
new RuntimeException(e);
}
}
}