Spring 状态机不会改变状态

Spring State Machine wont change state

我正在尝试组装一个非常简单的 Spring 状态机,但它似乎不想更改状态:
(重要的是要注意,即使我正在使用 Spring 状态机,并且应用程序的其余部分是一个 Spring 应用程序,我不能使用注释驱动的方法来定义机器。我必须使用建设者)

    StateMachineBuilder.Builder<String, String> builder = StateMachineBuilder.builder();

    try {
        builder.configureConfiguration()
                .withConfiguration()
                .beanFactory(new StaticListableBeanFactory())
                .listener(new StateMachineListenerAdapter<String, String>() {
                    @Override
                    public void stateChanged(State<String, String> from, State<String, String> to) {
                        System.out.println("State change to " + to.getId());
                    }
                });

        builder.configureStates()
                .withStates()
                .initial("initialState")
                .states(Stream.of("initialState", "state1", "state2", "state3").collect(Collectors.toSet()))
                .end("state3");

        builder.configureTransitions()
                .withExternal()
                .state("initialState")
                .target("state1")
                .event("event1")
                .action(stateContext ->
                    System.out.println(String.format("Event %s triggered", stateContext.getEvent()))
                );

    } catch (Exception e) {
        e.printStackTrace();
    }

    StateMachine<String, String> stateMachine = builder.build();

    stateMachine.start();

    boolean complete = stateMachine.isComplete();
    System.out.println("Statemachine is complete: " + complete);

    State<String, String> state = stateMachine.getState();
    System.out.println(state.getId());

    stateMachine.sendEvent("event1");

    state = stateMachine.getState();
    System.out.println(state.getId());

当我 运行 以上时,我期望第一个状态是 initialState,当我发送事件 event1 时,状态将更改为 state1.我得到以下输出:

State change to initialState
Statemachine is complete: false
initialState
initialState

这告诉我当机器第一次启动并设置初始状态时侦听器触发一次,由于某种原因状态机报告它不完整,在我发送事件之前状态是 initialState,并且在我发送事件后状态仍然是 initialState
启动状态机或发送事件时都没有错误,并且转换中的操作永远不会 运行.

我错过了什么?

我发现了问题 - 我错误地配置了转换!
应该是这样的:

    builder.configureTransitions()
            .withExternal()
            .source("initialState")
            .target("state1")
            .event("event1")
            .action(stateContext ->
                System.out.println(String.format("Event %s triggered", stateContext.getEvent()))
            );

即。我本来叫 .state,应该是 .source