Submachine doesn't work with states on a same level

- resolves #20
- create correct state type when substate mixed with
  normal states.
This commit is contained in:
Janne Valkealahti
2015-02-22 16:01:55 +00:00
parent ee2f7b2eb6
commit 714b8131ff
2 changed files with 65 additions and 9 deletions

View File

@@ -97,7 +97,7 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
};
// use two stack, first for states and second for machines
Stack<StateMachine<S, E>> machineStack = new Stack<StateMachine<S, E>>();
Stack<MachineStackItem<S, E>> machineStack = new Stack<MachineStackItem<S, E>>();
Stack<StateData<S, E>> stateStack = new Stack<StateData<S, E>>();
Iterable<Node<StateData<S, E>>> postOrderTraversal = traverser.postOrderTraversal(tree.getRoot());
@@ -131,11 +131,11 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
if (machineStack.isEmpty()) {
machine = buildSimpleMachine(stateMap, stateDatas, transitionsData, getBeanFactory());
machineStack.push(machine);
machineStack.push(new MachineStackItem<S, E>(machine, peek.getParent()));
} else {
StateMachine<S, E> pop = machineStack.pop();
MachineStackItem<S, E> pop = machineStack.pop();
machine = buildSubMachine(stateMap, pop, stateDatas, transitionsData, getBeanFactory());
machineStack.push(machine);
machineStack.push(new MachineStackItem<S, E>(machine, null));
}
stateStack.push(stateData);
}
@@ -144,6 +144,19 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
return machine;
}
private static class MachineStackItem<S, E> {
StateMachine<S, E> machine;
Object parent;
public MachineStackItem(StateMachine<S, E> machine, Object parent) {
super();
this.machine = machine;
this.parent = parent;
}
}
private Collection<TransitionData<S, E>> resolveTransitionData(Collection<TransitionData<S, E>> in, Collection<StateData<S, E>> stateDatas) {
ArrayList<TransitionData<S, E>> out = new ArrayList<TransitionData<S,E>>();
@@ -222,14 +235,21 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
}
private static <S extends Enum<S>, E extends Enum<E>> StateMachine<S, E> buildSubMachine(
Map<S, State<S, E>> stateMap, StateMachine<S, E> submachine, Collection<StateData<S, E>> stateDatas,
Map<S, State<S, E>> stateMap, MachineStackItem<S, E> stackItem, Collection<StateData<S, E>> stateDatas,
Collection<TransitionData<S, E>> transitionsData, BeanFactory beanFactory) {
Collection<State<S, E>> states = new ArrayList<State<S,E>>();
State<S, E> state = null;
State<S, E> initialState = null;
for (StateData<S, E> stateData : stateDatas) {
state = new StateMachineState<S, E>(stateData.getState(), submachine, stateData.getDeferred(),
stateData.getEntryActions(), stateData.getExitActions(), new DefaultPseudoState(
PseudoStateKind.INITIAL));
if (stackItem.parent == null || stateData.getState().equals(stackItem.parent)) {
state = new StateMachineState<S, E>(stateData.getState(), stackItem.machine, stateData.getDeferred(),
stateData.getEntryActions(), stateData.getExitActions(), new DefaultPseudoState(
PseudoStateKind.INITIAL));
initialState = state;
} else {
state = new EnumState<S, E>(stateData.getState(), stateData.getDeferred(),
stateData.getEntryActions(), stateData.getExitActions(), null);
}
states.add(state);
stateMap.put(stateData.getState(), state);
}
@@ -251,7 +271,7 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
}
}
EnumStateMachine<S, E> machine = new EnumStateMachine<S, E>(states, transitions, state, null);
EnumStateMachine<S, E> machine = new EnumStateMachine<S, E>(states, transitions, initialState, null);
machine.afterPropertiesSet();
if (beanFactory != null) {

View File

@@ -16,6 +16,8 @@
package org.springframework.statemachine;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -34,6 +36,7 @@ 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 org.springframework.statemachine.event.StateMachineEventPublisherConfiguration;
import org.springframework.statemachine.state.DefaultPseudoState;
import org.springframework.statemachine.state.EnumState;
import org.springframework.statemachine.state.PseudoState;
@@ -371,6 +374,19 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
}
@Test
public void testMixedStates() throws Exception {
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config2.class);
context.refresh();
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")
EnumStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
machine.start();
assertThat(machine, notNullValue());
assertThat(machine.getState().getIds(), contains(TestStates.S1, TestStates.S10));
}
@Configuration
@EnableStateMachine
@@ -436,4 +452,24 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
}
@Configuration
@EnableStateMachine
static class Config2 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.S1)
.state(TestStates.S1)
.state(TestStates.S2)
.and()
.withStates()
.parent(TestStates.S1)
.initial(TestStates.S10)
.state(TestStates.S10);
}
}
}