0

The state machine pauses at the choice state when the evaluation inside the choice state returns false, instead of moving to the next state.

下面的代码:

国家定义:

@Override
public void configure(StateMachineStateConfigurer<String, String>
states)  throws Exception {
    states
    .withStates()
    .initial("init")
    .choice("S1Choice")
    .state("S1")
    .choice("S2Choice")
    .state("S2")
    .choice("S3Choice")
    .state("S3")
    .state("end");
}

过渡/选择/动作:

@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
    transitions
    .withExternal()
    .source("init")
    .target("S1Choice")
    .event("start")
    .and()
    .withChoice()
    .source("S1Choice")
    .first("S1", new Guard<String, String>() {

        public boolean evaluate(StateContext<String, String> context) {
            System.out.println("In s1 choice");
            /*Map<Object, Object> map = context.getExtendedState().getVariables();
            return !map.containsKey("S1done");*/
            return false;
        }
    })
    .last("S2Choice")
    .and()
    .withLocal()
    .source("S1")
    .target("S2Choice")
    .action(new Action<String, String>() {

        public void execute(StateContext<String, String> context) {
            Map<Object, Object> map = context.getExtendedState().getVariables();
            System.out.println("Executing s1");
            map.put("S1done", Boolean.TRUE);
        }
    })
    .and()
    .withChoice()
    .source("S2Choice")
    .first("S2", new Guard<String, String>() {

        public boolean evaluate(StateContext<String, String> context) {
            System.out.println("In s2 choice");
            Map<Object, Object> map = context.getExtendedState().getVariables();
            return !map.containsKey("S2done");
        }
    })
    .last("S3Choice")
    .and()
    .withLocal()
    .source("S2")
    .target("S3Choice")
    .action(new Action<String, String>() {

        public void execute(StateContext<String, String> context) {
            Map<Object, Object> map = context.getExtendedState().getVariables();
            System.out.println("Executing s2");
            map.put("S2done", Boolean.TRUE);
        }
    })
    .and()
    .withChoice()
    .source("S3Choice")
    .first("S3", new Guard<String, String>() {

        public boolean evaluate(StateContext<String, String> context) {
            System.out.println("In s3 choice");
            Map<Object, Object> map = context.getExtendedState().getVariables();
            return !map.containsKey("S3done");
        }
    })
    .last("end")
    .and()
    .withLocal()
    .source("S3")
    .target("end")
    .and()
    .withLocal()
    .source("end")
    .target("init");
}

主要类:

public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(TasksConfig.class);
    
    StateMachine<String, String> stateMachine = context.getBean(StateMachine.class);
    stateMachine.start();
    stateMachine.getExtendedState().getVariables().put("S1done", Boolean.TRUE);
    
    stateMachine.sendEvent("start");
    //stateMachine.stop();
}

以下是捕获的输出示例:

工作项状态更改为 init

信息:开始 S2 S1 结束初始化 S3 S1Choice S3Choice S2Choice / init /

在 s1 选择

如您所见,它停在状态 -“s1 选择”而不是移动到新状态 -“s2 选择”。

4

1 回答 1

0

看起来您可能使用的 1.0.x 存在链接伪状态问题(解决方法是在这些选项之间放置正常状态)。这些已在 1.1.x 中修复(1.1.0 将于下周发布)。

于 2016-05-18T06:56:07.047 回答