Fix base support for region config
- Fixes #46 - Add some region/group info in a state configurer so that we know which states are in a same reqion. Just a simple uuid for now. - Send machine started event at end of start and properly register listener with regions. - Fix factory not to mess things up with submachines so correctly track if we have multiple initial states.
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.statemachine;
|
||||
|
||||
import org.springframework.statemachine.listener.StateMachineListener;
|
||||
import org.springframework.statemachine.region.Region;
|
||||
import org.springframework.statemachine.state.State;
|
||||
|
||||
@@ -44,11 +43,4 @@ public interface StateMachine<S, E> extends Region<S, E> {
|
||||
*/
|
||||
ExtendedState getExtendedState();
|
||||
|
||||
/**
|
||||
* Adds the state listener.
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
void addStateListener(StateMachineListener<S, E> listener);
|
||||
|
||||
}
|
||||
|
||||
@@ -94,30 +94,11 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
|
||||
// find a correct mappings because they use state id's, not actual
|
||||
// states.
|
||||
final Map<S, State<S, E>> stateMap = new HashMap<S, State<S, E>>();
|
||||
|
||||
Tree<StateData<S, E>> tree = new Tree<StateData<S, E>>();
|
||||
|
||||
for (StateData<S, E> stateData : stateMachineStates.getStateDatas()) {
|
||||
Object id = stateData.getState();
|
||||
Object parent = stateData.getParent();
|
||||
tree.add(stateData, id, parent);
|
||||
}
|
||||
|
||||
TreeTraverser<Node<StateData<S, E>>> traverser = new TreeTraverser<Node<StateData<S, E>>>() {
|
||||
@Override
|
||||
public Iterable<Node<StateData<S, E>>> children(Node<StateData<S, E>> root) {
|
||||
return root.getChildren();
|
||||
}
|
||||
};
|
||||
|
||||
Stack<MachineStackItem<S, E>> regionStack = 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());
|
||||
Iterator<Node<StateData<S, E>>> iterator = postOrderTraversal.iterator();
|
||||
|
||||
Map<Object, StateMachine<S, E>> machineMap = new HashMap<Object, StateMachine<S,E>>();
|
||||
|
||||
Iterator<Node<StateData<S, E>>> iterator = buildStateDataIterator();
|
||||
while (iterator.hasNext()) {
|
||||
Node<StateData<S, E>> node = iterator.next();
|
||||
StateData<S, E> stateData = node.getData();
|
||||
@@ -145,58 +126,48 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
|
||||
}
|
||||
|
||||
Collection<StateData<S, E>> stateDatas = popSameParents(stateStack);
|
||||
int initialCount = getInitialCount(stateDatas);
|
||||
Collection<Collection<StateData<S, E>>> regionsStateDatas = splitIntoRegions(stateDatas);
|
||||
Collection<TransitionData<S, E>> transitionsData = getTransitionData(iterator.hasNext(), stateDatas);
|
||||
|
||||
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory(), contextEvents, defaultExtendedState);
|
||||
// TODO: last part in if feels a bit hack
|
||||
// (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))
|
||||
if (peek.isInitial() || (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))) {
|
||||
machineMap.put(peek.getParent(), machine);
|
||||
}
|
||||
if (peek.getParent() == null) {
|
||||
regionStack.push(new MachineStackItem<S, E>(machine, peek.getParent(), peek));
|
||||
}
|
||||
stateStack.push(stateData);
|
||||
}
|
||||
|
||||
// TODO: usage of initials is a temporary fix to workaround for missing
|
||||
// full support of regions
|
||||
int initials = 0;
|
||||
if (regionStack.size() > 1) {
|
||||
Collection<TransitionData<S, E>> transitionsData = resolveTransitionData2(stateMachineTransitions.getTransitions());
|
||||
Collection<StateData<S, E>> stateDatas = new ArrayList<StateData<S, E>>();
|
||||
Iterator<MachineStackItem<S, E>> i = regionStack.iterator();
|
||||
while (i.hasNext()) {
|
||||
MachineStackItem<S, E> next = i.next();
|
||||
stateDatas.add(next.stateData);
|
||||
if (next.stateData.isInitial()) {
|
||||
initials++;
|
||||
if (initialCount > 1) {
|
||||
for (Collection<StateData<S, E>> regionStateDatas : regionsStateDatas) {
|
||||
machine = buildMachine(machineMap, stateMap, regionStateDatas, transitionsData, getBeanFactory(),
|
||||
contextEvents, defaultExtendedState);
|
||||
if (peek.isInitial() || (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))) {
|
||||
machineMap.put(peek.getParent(), machine);
|
||||
}
|
||||
regionStack.push(new MachineStackItem<S, E>(machine, peek.getParent(), peek));
|
||||
}
|
||||
}
|
||||
|
||||
if (initials == 1) {
|
||||
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory(), contextEvents, defaultExtendedState);
|
||||
}
|
||||
}
|
||||
|
||||
if (initials > 1) {
|
||||
Collection<Region<S, E>> regions = new ArrayList<Region<S, E>>();
|
||||
for (MachineStackItem<S, E> si : regionStack) {
|
||||
if (si.parent == null) {
|
||||
Collection<Region<S, E>> regions = new ArrayList<Region<S, E>>();
|
||||
for (MachineStackItem<S, E> si : regionStack) {
|
||||
regions.add(si.machine);
|
||||
}
|
||||
}
|
||||
if (regions.size() > 1) {
|
||||
RegionState<S, E> rstate = new RegionState<S, E>(null, regions);
|
||||
RegionState<S, E> rstate = new RegionState<S, E>(null, regions, null, null, null, new DefaultPseudoState(PseudoStateKind.INITIAL));
|
||||
Collection<State<S, E>> states = new ArrayList<State<S, E>>();
|
||||
states.add(rstate);
|
||||
EnumStateMachine<S, E> m = new EnumStateMachine<S, E>(states, null, rstate,
|
||||
EnumStateMachine<S, E> m = new EnumStateMachine<S, E>(states, new ArrayList<Transition<S, E>>(), rstate,
|
||||
null, null, defaultExtendedState);
|
||||
if (contextEvents != null) {
|
||||
m.setContextEventsEnabled(contextEvents);
|
||||
}
|
||||
if (getBeanFactory() != null) {
|
||||
m.setBeanFactory(getBeanFactory());
|
||||
}
|
||||
m.afterPropertiesSet();
|
||||
|
||||
machine = m;
|
||||
|
||||
|
||||
} else {
|
||||
machine = buildMachine(machineMap, stateMap, stateDatas, transitionsData, getBeanFactory(), contextEvents, defaultExtendedState);
|
||||
if (peek.isInitial() || (!peek.isInitial() && !machineMap.containsKey(peek.getParent()))) {
|
||||
machineMap.put(peek.getParent(), machine);
|
||||
}
|
||||
}
|
||||
|
||||
stateStack.push(stateData);
|
||||
}
|
||||
|
||||
return machine;
|
||||
@@ -206,6 +177,29 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
|
||||
this.contextEvents = contextEvents;
|
||||
}
|
||||
|
||||
private int getInitialCount(Collection<StateData<S, E>> stateDatas) {
|
||||
int count = 0;
|
||||
for (StateData<S, E> stateData : stateDatas) {
|
||||
if (stateData.isInitial()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private Collection<Collection<StateData<S, E>>> splitIntoRegions(Collection<StateData<S, E>> stateDatas) {
|
||||
Map<Object, Collection<StateData<S, E>>> map = new HashMap<Object, Collection<StateData<S, E>>>();
|
||||
for (StateData<S, E> stateData : stateDatas) {
|
||||
Collection<StateData<S, E>> c = map.get(stateData.getRegion());
|
||||
if (c == null) {
|
||||
c = new ArrayList<StateData<S,E>>();
|
||||
}
|
||||
c.add(stateData);
|
||||
map.put(stateData.getRegion(), c);
|
||||
}
|
||||
return map.values();
|
||||
}
|
||||
|
||||
private Collection<TransitionData<S, E>> getTransitionData(boolean roots, Collection<StateData<S, E>> stateDatas) {
|
||||
if (roots) {
|
||||
return resolveTransitionData(stateMachineTransitions.getTransitions(), stateDatas);
|
||||
@@ -335,6 +329,10 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
|
||||
}
|
||||
|
||||
if (transitionData.getKind() == TransitionKind.EXTERNAL) {
|
||||
// TODO can we do this?
|
||||
if (stateMap.get(source) == null && stateMap.get(target) == null) {
|
||||
continue;
|
||||
}
|
||||
DefaultExternalTransition<S, E> transition = new DefaultExternalTransition<S, E>(stateMap.get(source),
|
||||
stateMap.get(target), transitionData.getActions(), event, transitionData.getGuard(), trigger);
|
||||
transitions.add(transition);
|
||||
@@ -364,4 +362,25 @@ public class EnumStateMachineFactory<S extends Enum<S>, E extends Enum<E>> exten
|
||||
return machine;
|
||||
}
|
||||
|
||||
private Iterator<Node<StateData<S, E>>> buildStateDataIterator() {
|
||||
Tree<StateData<S, E>> tree = new Tree<StateData<S, E>>();
|
||||
|
||||
for (StateData<S, E> stateData : stateMachineStates.getStateDatas()) {
|
||||
Object id = stateData.getState();
|
||||
Object parent = stateData.getParent();
|
||||
tree.add(stateData, id, parent);
|
||||
}
|
||||
|
||||
TreeTraverser<Node<StateData<S, E>>> traverser = new TreeTraverser<Node<StateData<S, E>>>() {
|
||||
@Override
|
||||
public Iterable<Node<StateData<S, E>>> children(Node<StateData<S, E>> root) {
|
||||
return root.getChildren();
|
||||
}
|
||||
};
|
||||
|
||||
Iterable<Node<StateData<S, E>>> postOrderTraversal = traverser.postOrderTraversal(tree.getRoot());
|
||||
Iterator<Node<StateData<S, E>>> iterator = postOrderTraversal.iterator();
|
||||
return iterator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.statemachine.state.State;
|
||||
public class StateData<S, E> {
|
||||
|
||||
private Object parent;
|
||||
private Object region;
|
||||
private S state;
|
||||
private Collection<E> deferred;
|
||||
private Collection<? extends Action<S, E>> entryActions;
|
||||
@@ -41,13 +42,14 @@ public class StateData<S, E> {
|
||||
private Action<S, E> initialAction;
|
||||
private boolean end = false;
|
||||
|
||||
public StateData(Object parent, S state, Collection<E> deferred,
|
||||
public StateData(Object parent, Object region, S state, Collection<E> deferred,
|
||||
Collection<? extends Action<S, E>> entryActions, Collection<? extends Action<S, E>> exitActions) {
|
||||
this.state = state;
|
||||
this.deferred = deferred;
|
||||
this.entryActions = entryActions;
|
||||
this.exitActions = exitActions;
|
||||
this.parent = parent;
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public S getState() {
|
||||
@@ -74,6 +76,14 @@ public class StateData<S, E> {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Object getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(Object region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
@@ -100,8 +110,9 @@ public class StateData<S, E> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StateData [parent=" + parent + ", state=" + state + ", deferred=" + deferred + ", entryActions="
|
||||
+ entryActions + ", exitActions=" + exitActions + ", initial=" + initial + ", end=" + end + "]";
|
||||
return "StateData [parent=" + parent + ", region=" + region + ", state=" + state + ", deferred=" + deferred
|
||||
+ ", entryActions=" + entryActions + ", exitActions=" + exitActions + ", initial=" + initial + ", end="
|
||||
+ end + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.statemachine.action.Action;
|
||||
import org.springframework.statemachine.config.StateData;
|
||||
@@ -27,12 +28,22 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu
|
||||
import org.springframework.statemachine.config.builders.StateMachineStates;
|
||||
import org.springframework.statemachine.config.common.annotation.AnnotationConfigurerAdapter;
|
||||
|
||||
/**
|
||||
* Default implementation of a {@link StateConfigurer}.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*
|
||||
* @param <S> the type of state
|
||||
* @param <E> the type of event
|
||||
*/
|
||||
public class DefaultStateConfigurer<S, E>
|
||||
extends AnnotationConfigurerAdapter<StateMachineStates<S, E>, StateMachineStateConfigurer<S, E>, StateMachineStateBuilder<S, E>>
|
||||
implements StateConfigurer<S, E> {
|
||||
|
||||
private Object parent;
|
||||
|
||||
private final Object region = UUID.randomUUID().toString();
|
||||
|
||||
private final Collection<StateData<S, E>> incomplete = new ArrayList<StateData<S, E>>();
|
||||
|
||||
private S initialState;
|
||||
@@ -131,7 +142,7 @@ public class DefaultStateConfigurer<S, E>
|
||||
|
||||
private void addIncomplete(Object parent, S state, Collection<E> deferred,
|
||||
Collection<? extends Action<S, E>> entryActions, Collection<? extends Action<S, E>> exitActions) {
|
||||
incomplete.add(new StateData<S, E>(parent, state, deferred, entryActions, exitActions));
|
||||
incomplete.add(new StateData<S, E>(parent, region, state, deferred, entryActions, exitActions));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.statemachine.region;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.statemachine.listener.StateMachineListener;
|
||||
import org.springframework.statemachine.state.State;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
|
||||
@@ -87,4 +88,12 @@ public interface Region<S, E> {
|
||||
* @return true, if complete
|
||||
*/
|
||||
boolean isComplete();
|
||||
|
||||
/**
|
||||
* Adds the state listener.
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
void addStateListener(StateMachineListener<S, E> listener);
|
||||
|
||||
}
|
||||
|
||||
@@ -134,7 +134,9 @@ public class RegionState<S, E> extends AbstractState<S, E> {
|
||||
}
|
||||
} else {
|
||||
for (Region<S, E> region : getRegions()) {
|
||||
region.getState().entry(event, context);
|
||||
if (region.getState() != null) {
|
||||
region.getState().entry(event, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ import org.springframework.statemachine.listener.StateMachineListener;
|
||||
import org.springframework.statemachine.processor.StateMachineHandler;
|
||||
import org.springframework.statemachine.processor.StateMachineOnTransitionHandler;
|
||||
import org.springframework.statemachine.processor.StateMachineRuntime;
|
||||
import org.springframework.statemachine.region.Region;
|
||||
import org.springframework.statemachine.state.AbstractState;
|
||||
import org.springframework.statemachine.state.PseudoStateKind;
|
||||
import org.springframework.statemachine.state.State;
|
||||
@@ -198,6 +199,11 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
if (state.isSubmachineState()) {
|
||||
StateMachine<S, E> submachine = ((AbstractState<S, E>)state).getSubmachine();
|
||||
submachine.addStateListener(new StateMachineListenerRelay());
|
||||
} else if (state.isOrthogonal()) {
|
||||
Collection<Region<S, E>> regions = ((AbstractState<S, E>)state).getRegions();
|
||||
for (Region<S, E> region : regions) {
|
||||
region.addStateListener(new StateMachineListenerRelay());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,7 +211,6 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
@Override
|
||||
protected void doStart() {
|
||||
super.doStart();
|
||||
notifyStateMachineStarted(this);
|
||||
registerTriggerListener();
|
||||
switchToState(initialState, initialEvent, null, this);
|
||||
// TODO: for now execute outside of switchToState
|
||||
@@ -214,6 +219,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
initialEvent != null ? initialEvent.getHeaders() : null, extendedState, initialTransition, this);
|
||||
initialTransition.transit(stateContext);
|
||||
}
|
||||
notifyStateMachineStarted(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,19 +17,28 @@ package org.springframework.statemachine;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
import org.springframework.statemachine.AbstractStateMachineTests.TestEntryAction;
|
||||
import org.springframework.statemachine.AbstractStateMachineTests.TestEvents;
|
||||
import org.springframework.statemachine.AbstractStateMachineTests.TestExitAction;
|
||||
import org.springframework.statemachine.AbstractStateMachineTests.TestStates;
|
||||
import org.springframework.statemachine.action.Action;
|
||||
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.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.region.Region;
|
||||
import org.springframework.statemachine.state.DefaultPseudoState;
|
||||
import org.springframework.statemachine.state.EnumState;
|
||||
@@ -47,10 +56,15 @@ import org.springframework.statemachine.trigger.EventTrigger;
|
||||
* @author Janne Valkealahti
|
||||
*
|
||||
*/
|
||||
public class RegionMachineTests {
|
||||
public class RegionMachineTests extends AbstractStateMachineTests {
|
||||
|
||||
@Override
|
||||
protected AnnotationConfigApplicationContext buildContext() {
|
||||
return new AnnotationConfigApplicationContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleRegion() throws Exception {
|
||||
public void testSimpleRegionBuildRaw() throws Exception {
|
||||
PseudoState pseudoState = new DefaultPseudoState(PseudoStateKind.INITIAL);
|
||||
TestEntryAction entryActionS1 = new TestEntryAction("S1");
|
||||
TestExitAction exitActionS1 = new TestExitAction("S1");
|
||||
@@ -113,7 +127,7 @@ public class RegionMachineTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiRegion() throws Exception {
|
||||
public void testMultiRegionBuildRaw() throws Exception {
|
||||
SyncTaskExecutor taskExecutor = new SyncTaskExecutor();
|
||||
PseudoState pseudoState = new DefaultPseudoState(PseudoStateKind.INITIAL);
|
||||
State<TestStates,TestEvents> stateSI = new EnumState<TestStates,TestEvents>(TestStates.SI);
|
||||
@@ -205,4 +219,106 @@ public class RegionMachineTests {
|
||||
assertThat(exitActionS112.stateContexts.size(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiRegion() throws Exception {
|
||||
context.register(BaseConfig.class, StateMachineEventPublisherConfiguration.class, Config1.class);
|
||||
context.refresh();
|
||||
assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
|
||||
@SuppressWarnings("unchecked")
|
||||
EnumStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
|
||||
assertThat(machine, notNullValue());
|
||||
TestStateMachineListener listener = context.getBean(TestStateMachineListener.class);
|
||||
machine.addStateListener(listener);
|
||||
machine.start();
|
||||
assertThat(listener.stateMachineStartedLatch.await(5, TimeUnit.SECONDS), is(true));
|
||||
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S10, TestStates.S20));
|
||||
|
||||
listener.reset(2, 0);
|
||||
machine.sendEvent(TestEvents.E1);
|
||||
assertThat(listener.stateChangedLatch.await(5, TimeUnit.SECONDS), is(true));
|
||||
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S11, TestStates.S21));
|
||||
|
||||
listener.reset(1, 0);
|
||||
machine.sendEvent(TestEvents.E2);
|
||||
assertThat(listener.stateChangedLatch.await(5, TimeUnit.SECONDS), is(true));
|
||||
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S10, TestStates.S21));
|
||||
|
||||
listener.reset(1, 0);
|
||||
machine.sendEvent(TestEvents.E3);
|
||||
assertThat(listener.stateChangedLatch.await(5, TimeUnit.SECONDS), is(true));
|
||||
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S10, TestStates.S20));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(TestStates.S10)
|
||||
.state(TestStates.S10)
|
||||
.state(TestStates.S11)
|
||||
.and()
|
||||
.withStates()
|
||||
.initial(TestStates.S20)
|
||||
.state(TestStates.S20)
|
||||
.state(TestStates.S21);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(TestStates.S10)
|
||||
.target(TestStates.S11)
|
||||
.event(TestEvents.E1)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S11)
|
||||
.target(TestStates.S10)
|
||||
.event(TestEvents.E2)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S20)
|
||||
.target(TestStates.S21)
|
||||
.event(TestEvents.E1)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S21)
|
||||
.target(TestStates.S20)
|
||||
.event(TestEvents.E3);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TestStateMachineListener testStateMachineListener() {
|
||||
return new TestStateMachineListener();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class TestStateMachineListener extends StateMachineListenerAdapter<TestStates, TestEvents> {
|
||||
|
||||
volatile CountDownLatch stateChangedLatch = new CountDownLatch(0);
|
||||
volatile CountDownLatch stateMachineStartedLatch = new CountDownLatch(3);
|
||||
|
||||
@Override
|
||||
public void stateChanged(State<TestStates, TestEvents> from, State<TestStates, TestEvents> to) {
|
||||
stateChangedLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateMachineStarted(StateMachine<TestStates, TestEvents> stateMachine) {
|
||||
stateMachineStartedLatch.countDown();
|
||||
}
|
||||
|
||||
void reset(int c1, int c2) {
|
||||
stateChangedLatch = new CountDownLatch(c1);
|
||||
stateMachineStartedLatch = new CountDownLatch(c2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user