gpt4 book ai didi

java - StateMachine - 对状态变化的操作

转载 作者:行者123 更新时间:2023-11-29 04:08:45 25 4
gpt4 key购买 nike

我使用 Spring 指南编写了状态机的实现。

但我无法对更改状态做出任何反应,尽管状态本身已成功更改。也许我误解了 Beans 类的目标?我需要实现状态变化时自动执行closeDoor()和startMoving()方法。

控制台方法中的这些消息不显示:

import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;

@WithStateMachine
public class Beans {

@OnTransition(target = "CLOSED_DOOR")
void closeDoor() {
System.out.println("closeDoor method");
}

@OnTransition(target = "GOING")
void startMoving() {
System.out.println("startMoving method");
}
}

配置:

import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;;

import java.util.EnumSet;


@org.springframework.context.annotation.Configuration
@EnableStateMachine
public class Configuration extends EnumStateMachineConfigurerAdapter<States, Events> {
@Override
public void configure(StateMachineStateConfigurer<States, Events> states)
throws Exception {
states
.withStates()
.initial(States.STAY)
.states(EnumSet.allOf(States.class));
}

@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
throws Exception {
transitions
.withExternal()
.source(States.STAY).target(States.CLOSED_DOOR)
.event(Events.CLOSE_DOOR)
.and()
.withExternal()
.source(States.CLOSED_DOOR).target(States.GOING)
.event(Events.MOVE);
}
}

以及启动(根据事件,状态在控制台中正确显示):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;

public class App {
@Autowired
static StateMachine<States, Events> stateMachine;

public static void main(String[] args) {

stateMachine = Builder.getMachine();
stateMachine.start();

stateMachine.sendEvent(Events.CLOSE_DOOR);
System.out.println(stateMachine.getState()); // ObjectState [getIds()=[CLOSED_DOOR]

stateMachine.sendEvent(Events.MOVE);
System.out.println(stateMachine.getState()); // ObjectState [getIds()=[GOING]
}

依赖只有一个

<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

我做错了什么?

最佳答案

最后,我设法让它发挥作用。您的问题是 Spring DI 机制。您尝试使用 @WithStateMachine 启用转换监听器,但随后您使用 .getMachine() 创建机器对象。它不会那样工作,您需要决定是否要使用 Spring Context。我已经使用上下文创建了一个解决方案,但您也可以保留它并仅使用手动构建器,但是您需要更改您的监听器以使用手动方法而不是 Spring 上下文注释。

将您的主类更改为:

public class App {

public static void main(String[] args) {

ConfigurableApplicationContext context = new AnnotationConfigApplicationContext("machine");
final StateMachine<States, Events> stateMachine = context.getBean(StateMachine.class);

stateMachine.start();
System.out.println(stateMachine.getState()); // ObjectState [getIds()=[STAY]

stateMachine.sendEvent(Events.CLOSE_DOOR);
System.out.println(stateMachine.getState()); // ObjectState [getIds()=[CLOSED_DOOR]

stateMachine.sendEvent(Events.MOVE);
System.out.println(stateMachine.getState()); // ObjectState [getIds()=[GOING]
}
}

让我知道它是否适合您以及您是否理解它。

关于java - StateMachine - 对状态变化的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com