I am trying to configure the state machine from a yml File using a SpringStateMachine Builder. It works fine with the states and transition, but when I tried to include some child states, I'm getting the below exception.
org.springframework.statemachine.config.model.MalformedConfigurationException: Initial state not set at org.springframework.statemachine.config.model.verifier.BaseStructureVerifier.verify(BaseStructureVerifier.java:59) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.model.verifier.CompositeStateMachineModelVerifier.verify(CompositeStateMachineModelVerifier.java:43) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:173) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.AbstractStateMachineFactory.getStateMachine(AbstractStateMachineFactory.java:143) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.statemachine.config.StateMachineBuilder$Builder.build(StateMachineBuilder.java:155) ~[spring-statemachine-core-2.1.3.RELEASE.jar:2.1.3.RELEASE]
Here is my yml File
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
Here is my Spring State machine Class
@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);
}
}
}
When I ran your code on my local system I found out the issue. It comes out that the issue is in your configureState method,where you have done the state configuration, it should be this way:-
private void configureState(String state, List<String> childStateList,
StateConfigurer<String, String> stateConfigurer) {
stateConfigurer.state(state);
if (!childStateList.isEmpty()) {
childStateList.forEach(childState -> {
try {
stateConfigurer.and().withStates().parent(state).initial(childState).state(childState);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
For more info you can refer the Link