different versions of defining a state machine

This commit is contained in:
2025-07-13 22:24:40 +02:00
parent a387016587
commit ec24cdc38d
3 changed files with 57 additions and 4 deletions

52
src/TODO.md Normal file
View File

@@ -0,0 +1,52 @@
1. extract state machines from this as well
```java
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.builders.StateMachineBuilder;
import java.util.EnumSet;
public class DynamicStateMachineExample {
public enum States {
DRAFT, REVIEW, APPROVED
}
public enum Events {
SUBMIT, APPROVE
}
public static StateMachine<States, Events> buildStateMachine() throws Exception {
StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();
builder.configureStates()
.withStates()
.initial(States.DRAFT)
.states(EnumSet.allOf(States.class));
builder.configureTransitions()
.withExternal()
.source(States.DRAFT).target(States.REVIEW).event(Events.SUBMIT)
.and()
.withExternal()
.source(States.REVIEW).target(States.APPROVED).event(Events.APPROVE);
return builder.build();
}
public static void main(String[] args) throws Exception {
StateMachine<States, Events> sm = buildStateMachine();
sm.start();
System.out.println("Current State: " + sm.getState().getId());
sm.sendEvent(Events.SUBMIT);
System.out.println("After SUBMIT: " + sm.getState().getId());
sm.sendEvent(Events.APPROVE);
System.out.println("After APPROVE: " + sm.getState().getId());
}
}
```