我在一个非常简单的场景中配置SSM时遇到了问题:
罪魁祸首是触发 log_on 事件时需要执行的“验证”操作。我重读了SSM文档,不确定这是“fork”,“guard”还是分层配置。
'动作'在这里执行:
@Component
public class LogonValidationAction implements Action<PickStates, PickEvents> {
Logger logger = LogManager.getLogger();
volatile int counter = 0;
@Override
public void execute(StateContext<PickStates, PickEvents> context) {
String eventName = context.getEvent().name();
logger.info("executing {} for event {}", LogonValidationAction.class.getName(), eventName);
// basically, if success, send success, if failure, send failure
// for testing
if(counter % 2 == 0)
context.getStateMachine().sendEvent(PickEvents.logon_sucess);
else
context.getStateMachine().sendEvent(PickEvents.logon_fail);
}
}
我在测试中使用'counter'来指示流程为'failure'(logged_off状态保持)或'success'(状态更改为logged_on)。
这是配置:
@Configuration
@EnableStateMachineFactory
public class Config extends EnumStateMachineConfigurerAdapter<PickStates, PickEvents>{
@Autowired
StateMachineListener stateMachineListener;
@Autowired
LogonValidationAction logonValidationAction;
@Override
public void configure(StateMachineStateConfigurer<PickStates, PickEvents> states) throws Exception {
states
.withStates()
.initial(PickStates.logged_off)
.state(PickStates.logged_on)
.states(EnumSet.allOf(PickStates.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<PickStates, PickEvents> transitions) throws Exception {
transitions
.withExternal()
.source(PickStates.logged_off)
.target(PickStates.logged_on)
.event(PickEvents.log_on)
.action(logonValidationAction)
.and()
.withExternal()
.source(PickStates.logged_on)
.target(PickStates.logged_off)
.event(PickEvents.log_off);
}
@Override
public void configure(StateMachineConfigurationConfigurer<PickStates, PickEvents> config) throws Exception {
config
.withConfiguration()
.autoStartup(true)
.listener(stateMachineListener);
}
}
这是失败的测试(当操作触发logon_fail事件时,期望'logged_off'):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class EzpickStateMachineTest {
Logger logger = LogManager.getLogger();
@Autowired
StateMachineFactory<PickStates, PickEvents> stateMachineFactory;
// for testing so can toggle success/failure when logging in
@Autowired
LogonValidationAction logonValidationAction;
@Test
public void failLogon() throws Exception {
StateMachine<PickStates, PickEvents> stateMachine = stateMachineFactory.getStateMachine();
stateMachine.start();
// when sm starts, state is 'logged_off'
assertThat(stateMachine.getState().getId().name(), is(PickStates.logged_off.name()));
// odd numbers fire 'failure' event
logonValidationAction.setCounter(1);
stateMachine.sendEvent(PickEvents.log_on);
// if logon fails, state is 'logged_off'
assertThat(stateMachine.getState().getId().name(), is(PickStates.logged_off.name()));
}
}
答案 0 :(得分:0)
您可以尝试发送logon_sucess
或logon_fail
,但在机器配置中,您尚未为这些事件定义任何转换,因此计算机不会执行任何操作。
其他注意事项,不要在动作bean中存储counter
,为它使用机器扩展状态变量,因为它使用机器或StateContext
对每个组件都可见。
当counter
在扩展状态下可用时,您可以使用从扩展状态使用该计数器的保护来创建匿名转换。另一种可能性是使用选择伪状态和保护来在目标状态之间做出选择。您对计数器的使用正是维基在其扩展状态部分https://en.wikipedia.org/wiki/UML_state_machine#Extended_states