Spring 未调用加入后的状态机内部转换操作

Spring Statemachine internal transition action after join is not invoked

我正在尝试使用 forkjoin 配置状态机。加入后,我想使用内部转换对加入状态调用一个操作。问题是未触发为 withInternal() 配置的操作。我试过 .guard(context -> true) hack,我也玩过 .timer().timerOnce(),但也没有用。

配置如下:

private void configureStates(StateMachineBuilder.Builder<String, String> builder) throws Exception {
        builder.configureStates()
                .withStates()
                .initial("A")

                .fork("B")
                .join("C")

                .state("A")
                .state("B_")
                .state("C")
                .state("D")
                .state("E")
                .and()

                .withStates()
                .parent("B_")
                .initial("B1")
                .end("C1")
                .and()

                .withStates()
                .parent("B_")
                .initial("B2")
                .end("C2")
                .and()

                .withStates()
                .parent("B_")
                .initial("B3")
                .end("C3")

                .end("E");
    }

转换配置:

private void configureTransitions(StateMachineBuilder.Builder<String, String> builder) throws Exception {
        builder.configureTransitions()
                .withExternal()
                .source("A")
                .target("B")
                .event("E0")
                .action(context -> log.info("From A to B"))
                .and()

                .withInternal()
                .source("B")
                .guard(stateContext -> true)
                .action(context -> log.info("At B"))
                .timerOnce(50)
                .and()

                .withFork()
                .source("B")
                .target("B_")
                .and()

                .withExternal()
                .source("B1")
                .target("C1")
                .event("E1")
                .and()

                .withExternal()
                .source("B2")
                .target("C2")
                .event("E2")
                .and()

                .withExternal()
                .source("B3")
                .target("C3")
                .and()

                .withExternal()
                .source("C3")
                .target("A")
                .event("E3")
                .and()

                .withJoin()
                .source("B_")
                .target("C")
                .and()

                .withInternal()
                .source("C")
                .guard(context -> true)
                .action(context -> log.info("At C"))
                .timerOnce(50)
                .state("C")
                .and()

                .withExternal()
                .source("C")
                .target("D")
                .action(context -> log.info("At D"))
                .and()

                .withInternal()
                .source("D")
                .guard(stateContext -> true)
                .action(stateContext -> log.info("At internal D"))
                .timer(10)
                .and()

                .withExternal()
                .source("D")
                .event("E4")
                .target("E");
    }

我还给状态机添加了一个监听器:

private StateMachineListener<String, String> listener() {
        return new StateMachineListenerAdapter<String, String>() {
            @Override
            public void stateChanged(State<String, String> from, State<String, String> to) {
                log.info("State transited from [{}] to [{}]",
                        from == null ? null : from.getId(),
                        to == null ? null : to.getId());
            }
        };
    }

最终配置为:

private StateMachine<String, String> buildMachine() throws Exception {
        StateMachineBuilder.Builder<String, String> builder = StateMachineBuilder.builder();

        builder.configureConfiguration()
                .withConfiguration()
                .listener(listener())
                .autoStartup(true);

        configureStates(builder);

        configureTransitions(builder);

        return builder.build();
    }

问题是调用了 none 个内部转换操作。

我已经为给定的配置创建了一个小测试:

@Test
    public void testForkJoin() throws Exception {
        StateMachine<String, String> machine = buildMachine();

        StateMachineTestPlan<String, String> plan = StateMachineTestPlanBuilder.<String, String>builder()
                .defaultAwaitTime(3)
                .stateMachine(machine)

                .step()
                .expectStates("A")
                .and()

                .step()
                .sendEvent("E0")
                .expectStates("B_", "B1", "B2", "C3")
                .and()

                .step()
                .sendEvent("E1")
                .expectStates("B_", "C1", "B2", "C3")
                .and()

                .step()
                .sendEvent("E3")
                .expectState("A")
                .and()

                .step()
                .sendEvent("E0")
                .expectStates("B_", "B1", "B2", "C3")
                .and()

                .step()
                .sendEvent("E1")
                .expectStates("B_", "C1", "B2", "C3")
                .and()

                .step()
                .sendEvent("E2")
                .expectStates("D")
                .and()

                .step()
                .sendEvent("E4")
                .expectState("E")
                .and()

                .build();

        plan.test();
    }

作为解决方法,我添加了几个外部转换(从 CD),但事实是我想省略状态 D 并直接转换到 E 通过执行现有操作作为内部转换操作。

I would like to omit state D and transit directly to E by executing the existing actions as an internal transition action.

简答:你不能。

Fork/Join 伪状态不应引入行为规范(例如 Action)。 Fork/Join 仅用于模拟 SM(瞬态伪状态)中的并行操作(fork)和同步(join)。

Spring 状态机实现遵循 UML specification,因此与 fork/join 关联的操作未执行。

动作与特定的转换或状态相关联。

与转换相关的操作:

进行 JOIN 时,您可能有 N (>=2) 个源(J1E、J2E - 其他区域的最后阶段),因此可以在从 J1E 过渡到 JOIN 阶段(action=A1)时定义不同的操作) 并从 J2E 到 JOIN 阶段 (action=A2).

与状态相关的操作:

如果您有一个共同的操作需要在并行操作同步后执行,您可以将其定义为下一个转换的一部分(例如,我相信您的 SM 案例是从 C 转换到 D)。