下面的代码抛出了一个NPE,我不知道出了什么问题。我找不到初始化currentState的代码。有人可以帮忙吗?
public class TestSMApplication {
StateMachine<String, String> buildMachine1() throws Exception {
Builder<String, String> builder = StateMachineBuilder.builder();
builder.configureConfiguration().withConfiguration()
.listener(listener());
builder.configureStates()
.withStates()
.initial("OPEN")
.end("CLOSED")
.states(new HashSet<String>(Arrays.asList("OPEN", "PROGRESS",
"CLOSED")));
builder.configureTransitions().withExternal().source("OPEN")
.target("PROGRESS").event("RUN").and().withExternal()
.source("OPEN").target("TERMINATE").event("STOP");
StateMachine<String, String> sm = builder.build();
return sm;
}
public org.springframework.statemachine.listener.StateMachineListener<String, String> listener() {
return new StateMachineListenerAdapter<String, String>() {
@Override
public void stateChanged(State<String, String> from,
State<String, String> to) {
System.out.println("State change to " + to.getId());
}
};
}
public static void main(String[] args) throws Exception {
TestSMApplication tsm = new TestSMApplication();
StateMachine<String, String> m1 = tsm.buildMachine1();
m1.start();
m1.sendEvent(Events.PROCESSING_SUCCEEDED.name());
}
}
我正在使用: org.springframework.statemachine:弹簧的statemachine核:1.1.0.M2
感谢Fredy
答案 0 :(得分:0)
package demo.sm;
import java.util.Arrays;
import java.util.HashSet;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;
public class AnotherTest {
StateMachine<String, String> buildMachine1() throws Exception {
Builder<String, String> builder = StateMachineBuilder.builder();
builder.configureConfiguration().withConfiguration()
.listener(listener());
builder.configureStates()
.withStates()
.initial("OPEN")
.end("CLOSED")
.states(new HashSet<String>(Arrays.asList("OPEN", "PROGRESS",
"CLOSED")));
builder.configureTransitions()
.withExternal()
.source("OPEN")
.target("PROGRESS")
.event("RUN")
.and()
.withExternal()
.source("PROGRESS")
.target("CLOSED")
.event("STOP");
StateMachine<String, String> sm = builder.build();
return sm;
}
public org.springframework.statemachine.listener.StateMachineListener<String, String> listener() {
return new StateMachineListenerAdapter<String, String>() {
@Override
public void stateChanged(State<String, String> from,
State<String, String> to) {
System.out.println("State change to " + to.getId());
}
};
}
public static void main(String[] args) throws Exception {
TestSMApplication tsm = new TestSMApplication();
StateMachine<String, String> m1 = tsm.buildMachine1();
m1.start();
m1.sendEvent("RUN");
m1.sendEvent("STOP");
}
}