Refactor join handling
- Refactor how join is handled - Add new StateListener for listening entry/exit per State. - Transition is now passed to next guy from a join state. - A lot of changes in core machine/factory/executor to support this new join handling. - Fixes #235 - Fixes #237
This commit is contained in:
@@ -61,6 +61,7 @@ import org.springframework.statemachine.state.ExitPseudoState;
|
||||
import org.springframework.statemachine.state.ForkPseudoState;
|
||||
import org.springframework.statemachine.state.HistoryPseudoState;
|
||||
import org.springframework.statemachine.state.JoinPseudoState;
|
||||
import org.springframework.statemachine.state.JoinPseudoState.JoinStateData;
|
||||
import org.springframework.statemachine.state.JunctionPseudoState;
|
||||
import org.springframework.statemachine.state.JunctionPseudoState.JunctionStateData;
|
||||
import org.springframework.statemachine.state.PseudoState;
|
||||
@@ -631,34 +632,24 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
|
||||
joins.add(stateMap.get(fs));
|
||||
}
|
||||
}
|
||||
S ss = null;
|
||||
|
||||
List<JoinStateData<S, E>> joinTargets = new ArrayList<JoinStateData<S, E>>();
|
||||
Collection<TransitionData<S, E>> transitions = stateMachineTransitions.getTransitions();
|
||||
for (TransitionData<S, E> tt : transitions) {
|
||||
if (tt.getSource() == s) {
|
||||
ss = tt.getTarget();
|
||||
break;
|
||||
StateHolder<S, E> holder = new StateHolder<S, E>(stateMap.get(tt.getTarget()));
|
||||
if (holder.getState() == null) {
|
||||
holderMap.put(tt.getTarget(), holder);
|
||||
}
|
||||
joinTargets.add(new JoinStateData<S, E>(holder, tt.getGuard()));
|
||||
}
|
||||
}
|
||||
StateHolder<S, E> holder = new StateHolder<S, E>(stateMap.get(ss));
|
||||
if (holder.getState() == null) {
|
||||
holderMap.put(ss, holder);
|
||||
}
|
||||
JoinPseudoState<S, E> pseudoState = new JoinPseudoState<S, E>(joins, holder);
|
||||
JoinPseudoState<S, E> pseudoState = new JoinPseudoState<S, E>(joins, joinTargets);
|
||||
|
||||
state = buildStateInternal(stateData.getState(), stateData.getDeferred(), stateData.getEntryActions(),
|
||||
stateData.getExitActions(), pseudoState);
|
||||
states.add(state);
|
||||
stateMap.put(stateData.getState(), state);
|
||||
|
||||
// find joins sources and associate
|
||||
for (Entry<S, State<S, E>> e : stateMap.entrySet()) {
|
||||
State<S, E> value = e.getValue();
|
||||
if (value.isOrthogonal()) {
|
||||
Collection<State<S, E>> states2 = value.getStates();
|
||||
if (states2.containsAll(joins)) {
|
||||
((RegionState<S, E>)value).setJoin(pseudoState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -706,6 +697,23 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
|
||||
}
|
||||
}
|
||||
|
||||
if (stateMachineTransitions.getJoins() != null) {
|
||||
for (Entry<S, List<S>> entry : stateMachineTransitions.getJoins().entrySet()) {
|
||||
if (stateMap.get(entry.getKey()) != null) {
|
||||
List<S> entryList = entry.getValue();
|
||||
for (S entryState : entryList) {
|
||||
State<S, E> source = stateMap.get(entryState);
|
||||
if (source != null && !source.isOrthogonal()) {
|
||||
State<S, E> target = stateMap.get(entry.getKey());
|
||||
DefaultExternalTransition<S, E> transition = new DefaultExternalTransition<S, E>(
|
||||
source, target, null, null, null, null, null);
|
||||
transitions.add(transition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Transition<S, E> initialTransition = new InitialTransition<S, E>(initialState, initialAction);
|
||||
StateMachine<S, E> machine = buildStateMachineInternal(states, transitions, initialState, initialTransition,
|
||||
null, defaultExtendedState, historyState, contextEvents, beanFactory, taskExecutor, taskScheduler,
|
||||
|
||||
@@ -44,6 +44,7 @@ public abstract class AbstractState<S, E> implements State<S, E> {
|
||||
private final Collection<Region<S, E>> regions = new ArrayList<Region<S, E>>();
|
||||
private final StateMachine<S, E> submachine;
|
||||
private List<Trigger<S, E>> triggers = new ArrayList<Trigger<S, E>>();
|
||||
private final CompositeStateListener<S, E> stateListener = new CompositeStateListener<S, E>();
|
||||
|
||||
/**
|
||||
* Instantiates a new abstract state.
|
||||
@@ -162,6 +163,7 @@ public abstract class AbstractState<S, E> implements State<S, E> {
|
||||
|
||||
@Override
|
||||
public void exit(StateContext<S, E> context) {
|
||||
stateListener.onExit(context);
|
||||
for (Trigger<S, E> trigger : triggers) {
|
||||
trigger.disarm();
|
||||
}
|
||||
@@ -169,6 +171,7 @@ public abstract class AbstractState<S, E> implements State<S, E> {
|
||||
|
||||
@Override
|
||||
public void entry(StateContext<S, E> context) {
|
||||
stateListener.onEntry(context);
|
||||
for (Trigger<S, E> trigger : triggers) {
|
||||
trigger.arm();
|
||||
}
|
||||
@@ -225,6 +228,16 @@ public abstract class AbstractState<S, E> implements State<S, E> {
|
||||
return submachine != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addStateListener(StateListener<S, E> listener) {
|
||||
stateListener.register(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeStateListener(StateListener<S, E> listener) {
|
||||
stateListener.unregister(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the submachine.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.statemachine.state;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.springframework.statemachine.StateContext;
|
||||
import org.springframework.statemachine.listener.AbstractCompositeListener;
|
||||
|
||||
/**
|
||||
* Composite state listener.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*
|
||||
* @param <S> the type of state
|
||||
* @param <E> the type of event
|
||||
*/
|
||||
public class CompositeStateListener<S, E> extends AbstractCompositeListener<StateListener<S, E>>
|
||||
implements StateListener<S, E> {
|
||||
|
||||
@Override
|
||||
public void onEntry(StateContext<S, E> context) {
|
||||
for (Iterator<StateListener<S, E>> iterator = getListeners().reverse(); iterator.hasNext();) {
|
||||
iterator.next().onEntry(context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExit(StateContext<S, E> context) {
|
||||
for (Iterator<StateListener<S, E>> iterator = getListeners().reverse(); iterator.hasNext();) {
|
||||
iterator.next().onExit(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,12 @@ package org.springframework.statemachine.state;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.statemachine.StateContext;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.guard.Guard;
|
||||
import org.springframework.statemachine.state.PseudoStateContext.PseudoAction;
|
||||
import org.springframework.statemachine.support.StateMachineUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -33,36 +36,42 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class JoinPseudoState<S, E> extends AbstractPseudoState<S, E> {
|
||||
|
||||
private final static Log log = LogFactory.getLog(JoinPseudoState.class);
|
||||
private final List<State<S, E>> joins;
|
||||
private volatile JoinTracker tracker;
|
||||
private final StateHolder<S, E> state;
|
||||
private final JoinTracker tracker;
|
||||
private final List<JoinStateData<S, E>> joinTargets;
|
||||
|
||||
/**
|
||||
* Instantiates a new join pseudo state.
|
||||
*
|
||||
* @param joins the joins
|
||||
* @param state the holder for target state
|
||||
* @param joinTargets the target states
|
||||
*/
|
||||
public JoinPseudoState(List<State<S, E>> joins, StateHolder<S, E> state) {
|
||||
public JoinPseudoState(List<State<S, E>> joins, List<JoinStateData<S, E>> joinTargets) {
|
||||
super(PseudoStateKind.JOIN);
|
||||
Assert.notNull(state, "Holder must be set");
|
||||
this.joins = joins;
|
||||
this.state = state;
|
||||
this.joinTargets = joinTargets;
|
||||
this.tracker = new JoinTracker();
|
||||
}
|
||||
|
||||
@Override
|
||||
public State<S, E> entry(StateContext<S, E> context) {
|
||||
tracker = new JoinTracker(this, new ArrayList<State<S,E>>(joins));
|
||||
context.getStateMachine().addStateListener(tracker);
|
||||
return state.getState();
|
||||
if (!tracker.isNotified()) {
|
||||
return null;
|
||||
}
|
||||
State<S, E> s = null;
|
||||
for (JoinStateData<S, E> c : joinTargets) {
|
||||
s = c.getState();
|
||||
if (c.guard != null && evaluateInternal(c.guard, context)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit(StateContext<S, E> context) {
|
||||
if (context != null) {
|
||||
context.getStateMachine().removeStateListener(tracker);
|
||||
}
|
||||
tracker = null;
|
||||
tracker.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,28 +83,112 @@ public class JoinPseudoState<S, E> extends AbstractPseudoState<S, E> {
|
||||
return joins;
|
||||
}
|
||||
|
||||
private class JoinTracker extends StateMachineListenerAdapter<S, E> {
|
||||
private boolean evaluateInternal(Guard<S, E> guard, StateContext<S, E> context) {
|
||||
try {
|
||||
return guard.evaluate(context);
|
||||
} catch (Throwable t) {
|
||||
log.warn("Deny guard due to throw as GUARD should not error", t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private class JoinTracker {
|
||||
|
||||
private final PseudoState<S, E> pseudoState;
|
||||
private final List<State<S, E>> track;
|
||||
private volatile boolean notified = false;
|
||||
|
||||
public JoinTracker(PseudoState<S, E> pseudoState, List<State<S, E>> track) {
|
||||
this.pseudoState = pseudoState;
|
||||
this.track = track;
|
||||
}
|
||||
public JoinTracker() {
|
||||
this.track = new ArrayList<State<S,E>>(joins);
|
||||
for (State<S, E> tt : joins) {
|
||||
final State<S, E> t = tt;
|
||||
t.addStateListener(new StateListener<S, E>() {
|
||||
|
||||
@Override
|
||||
public synchronized void stateChanged(State<S, E> from, State<S, E> to) {
|
||||
if (!notified && track.size() > 0) {
|
||||
track.remove(to);
|
||||
if (track.size() == 0) {
|
||||
notified = true;
|
||||
notifyContext(new DefaultPseudoStateContext<S, E>(pseudoState, PseudoAction.JOIN_COMPLETED));
|
||||
}
|
||||
@Override
|
||||
public void onEntry(StateContext<S, E> context) {
|
||||
if (StateMachineUtils.isPseudoState(context.getTransition().getTarget(), PseudoStateKind.END)) {
|
||||
if (!notified && track.size() > 0) {
|
||||
track.remove(t);
|
||||
if (track.size() == 0) {
|
||||
notified = true;
|
||||
notifyContext(new DefaultPseudoStateContext<S, E>(JoinPseudoState.this, PseudoAction.JOIN_COMPLETED));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExit(StateContext<S, E> context) {
|
||||
if (!notified && track.size() > 0) {
|
||||
track.remove(t);
|
||||
if (track.size() == 0) {
|
||||
notified = true;
|
||||
notifyContext(new DefaultPseudoStateContext<S, E>(JoinPseudoState.this, PseudoAction.JOIN_COMPLETED));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
track.clear();
|
||||
track.addAll(joins);
|
||||
notified = false;
|
||||
}
|
||||
|
||||
public boolean isNotified() {
|
||||
return notified;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data class wrapping join {@link State} and {@link Guard}
|
||||
* together.
|
||||
*
|
||||
* @param <S> the type of state
|
||||
* @param <E> the type of event
|
||||
*/
|
||||
public static class JoinStateData<S, E> {
|
||||
private final StateHolder<S, E> state;
|
||||
private final Guard<S, E> guard;
|
||||
|
||||
/**
|
||||
* Instantiates a new join state data.
|
||||
*
|
||||
* @param state the state holder
|
||||
* @param guard the guard
|
||||
*/
|
||||
public JoinStateData(StateHolder<S, E> state, Guard<S, E> guard) {
|
||||
Assert.notNull(state, "Holder must be set");
|
||||
this.state = state;
|
||||
this.guard = guard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state holder.
|
||||
*
|
||||
* @return the state holder
|
||||
*/
|
||||
public StateHolder<S, E> getStateHolder() {
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the state.
|
||||
*
|
||||
* @return the state
|
||||
*/
|
||||
public State<S, E> getState() {
|
||||
return state.getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the guard.
|
||||
*
|
||||
* @return the guard
|
||||
*/
|
||||
public Guard<S, E> getGuard() {
|
||||
return guard;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,6 @@ import org.springframework.statemachine.support.StateMachineUtils;
|
||||
*/
|
||||
public class RegionState<S, E> extends AbstractState<S, E> {
|
||||
|
||||
private JoinPseudoState<S, E> join;
|
||||
|
||||
/**
|
||||
* Instantiates a new region state.
|
||||
*
|
||||
@@ -129,6 +127,7 @@ public class RegionState<S, E> extends AbstractState<S, E> {
|
||||
|
||||
@Override
|
||||
public void exit(StateContext<S, E> context) {
|
||||
super.exit(context);
|
||||
for (Region<S, E> region : getRegions()) {
|
||||
if (region.getState() != null) {
|
||||
region.getState().exit(context);
|
||||
@@ -145,9 +144,7 @@ public class RegionState<S, E> extends AbstractState<S, E> {
|
||||
|
||||
@Override
|
||||
public void entry(StateContext<S, E> context) {
|
||||
if (join != null) {
|
||||
join.entry(context);
|
||||
}
|
||||
super.entry(context);
|
||||
Collection<? extends Action<S, E>> actions = getEntryActions();
|
||||
if (actions != null) {
|
||||
for (Action<S, E> action : actions) {
|
||||
@@ -201,10 +198,6 @@ public class RegionState<S, E> extends AbstractState<S, E> {
|
||||
return states;
|
||||
}
|
||||
|
||||
public void setJoin(JoinPseudoState<S, E> join) {
|
||||
this.join = join;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RegionState [getIds()=" + getIds() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
|
||||
|
||||
@@ -147,4 +147,17 @@ public interface State<S, E> {
|
||||
*/
|
||||
boolean isSubmachineState();
|
||||
|
||||
/**
|
||||
* Adds the state listener.
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
void addStateListener(StateListener<S, E> listener);
|
||||
|
||||
/**
|
||||
* Removes the state listener.
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
void removeStateListener(StateListener<S, E> listener);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.statemachine.state;
|
||||
|
||||
import org.springframework.statemachine.StateContext;
|
||||
|
||||
/**
|
||||
* {@code StateListener} for various state events.
|
||||
*
|
||||
* @author Janne Valkealahti
|
||||
*
|
||||
* @param <S> the type of state
|
||||
* @param <E> the type of event
|
||||
*/
|
||||
public interface StateListener<S, E> {
|
||||
|
||||
/**
|
||||
* Called when {@link State} want to notify of its entry.
|
||||
*
|
||||
* @param context the state context
|
||||
*/
|
||||
void onEntry(StateContext<S, E> context);
|
||||
|
||||
/**
|
||||
* Called when {@link State} want to notify of its exit.
|
||||
*
|
||||
* @param context the state context
|
||||
*/
|
||||
void onExit(StateContext<S, E> context);
|
||||
}
|
||||
@@ -136,6 +136,7 @@ public class StateMachineState<S, E> extends AbstractState<S, E> {
|
||||
|
||||
@Override
|
||||
public void exit(StateContext<S, E> context) {
|
||||
super.exit(context);
|
||||
// don't stop if it looks like we're coming back
|
||||
// stop would cause start with entry which would
|
||||
// enable default transition and state
|
||||
@@ -156,6 +157,7 @@ public class StateMachineState<S, E> extends AbstractState<S, E> {
|
||||
|
||||
@Override
|
||||
public void entry(final StateContext<S, E> context) {
|
||||
super.entry(context);
|
||||
Collection<? extends Action<S, E>> actions = getEntryActions();
|
||||
if (actions != null && !isLocal(context)) {
|
||||
for (Action<S, E> action : actions) {
|
||||
|
||||
@@ -45,7 +45,6 @@ import org.springframework.statemachine.region.Region;
|
||||
import org.springframework.statemachine.state.AbstractState;
|
||||
import org.springframework.statemachine.state.ForkPseudoState;
|
||||
import org.springframework.statemachine.state.HistoryPseudoState;
|
||||
import org.springframework.statemachine.state.JoinPseudoState;
|
||||
import org.springframework.statemachine.state.PseudoState;
|
||||
import org.springframework.statemachine.state.PseudoStateContext;
|
||||
import org.springframework.statemachine.state.PseudoStateKind;
|
||||
@@ -286,11 +285,15 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
// TODO: fix above stateContext as it's not used
|
||||
notifyTransitionStart(buildStateContext(Stage.TRANSITION_START, message, t, getRelayStateMachine()));
|
||||
notifyTransition(buildStateContext(Stage.TRANSITION, message, t, getRelayStateMachine()));
|
||||
if (t.getKind() == TransitionKind.INITIAL) {
|
||||
switchToState(t.getTarget(), message, t, getRelayStateMachine());
|
||||
notifyStateMachineStarted(buildStateContext(Stage.STATEMACHINE_START, message, t, getRelayStateMachine()));
|
||||
} else if (t.getKind() != TransitionKind.INTERNAL) {
|
||||
switchToState(t.getTarget(), message, t, getRelayStateMachine());
|
||||
if (t.getTarget().getPseudoState() != null && t.getTarget().getPseudoState().getKind() == PseudoStateKind.JOIN) {
|
||||
exitFromState(t.getSource(), message, t, getRelayStateMachine());
|
||||
} else {
|
||||
if (t.getKind() == TransitionKind.INITIAL) {
|
||||
switchToState(t.getTarget(), message, t, getRelayStateMachine());
|
||||
notifyStateMachineStarted(buildStateContext(Stage.STATEMACHINE_START, message, t, getRelayStateMachine()));
|
||||
} else if (t.getKind() != TransitionKind.INTERNAL) {
|
||||
switchToState(t.getTarget(), message, t, getRelayStateMachine());
|
||||
}
|
||||
}
|
||||
// TODO: looks like events should be called here and anno processing earlier
|
||||
notifyTransitionEnd(buildStateContext(Stage.TRANSITION_END, message, t, getRelayStateMachine()));
|
||||
@@ -784,25 +787,29 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
@Override
|
||||
public void onContext(PseudoStateContext<S, E> context) {
|
||||
PseudoState<S, E> pseudoState = context.getPseudoState();
|
||||
if (pseudoState.getKind() == PseudoStateKind.JOIN) {
|
||||
List<State<S, E>> joins = ((JoinPseudoState<S, E>)context.getPseudoState()).getJoins();
|
||||
for (State<S, E> join : joins) {
|
||||
exitFromState(join, null, null, getRelayStateMachine());
|
||||
}
|
||||
}
|
||||
State<S, E> toState = findStateWithPseudoState(pseudoState);
|
||||
State<S, E> toStateOrig = findStateWithPseudoState(pseudoState);
|
||||
StateContext<S, E> stateContext = buildStateContext(Stage.STATE_EXIT, null, null, getRelayStateMachine());
|
||||
State<S, E> toState = followLinkedPseudoStates(toStateOrig, stateContext);
|
||||
// TODO: try to find matching transition based on direct link.
|
||||
// should make this built-in in pseudostates
|
||||
Transition<S, E> transition = findTransition(toStateOrig, toState);
|
||||
switchToState(toState, null, transition, getRelayStateMachine());
|
||||
pseudoState.exit(stateContext);
|
||||
toState = followLinkedPseudoStates(toState, stateContext);
|
||||
// should figure out what transition to use as we pass null for now
|
||||
// which is then expected in exitCurrentState
|
||||
switchToState(toState, null, null, getRelayStateMachine());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Transition<S, E> findTransition(State<S, E> from, State<S, E> to) {
|
||||
for (Transition<S, E> transition : transitions) {
|
||||
if (transition.getSource() == from && transition.getTarget() == to) {
|
||||
return transition;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private State<S, E> findStateWithPseudoState(PseudoState<S, E> pseudoState) {
|
||||
for (State<S, E> s : states) {
|
||||
if (s.getPseudoState() == pseudoState) {
|
||||
@@ -984,9 +991,7 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
exitFromState(r.getState(), message, transition, stateMachine, sources, targets);
|
||||
}
|
||||
}
|
||||
if (transition == null) {
|
||||
exitFromState(currentState, message, transition, stateMachine, sources, targets);
|
||||
}
|
||||
exitFromState(currentState, message, transition, stateMachine, sources, targets);
|
||||
} else {
|
||||
exitFromState(currentState, message, transition, stateMachine, sources, targets);
|
||||
}
|
||||
|
||||
@@ -19,12 +19,14 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -36,9 +38,11 @@ import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.statemachine.StateContext;
|
||||
import org.springframework.statemachine.StateContext.Stage;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.StateMachineSystemConstants;
|
||||
import org.springframework.statemachine.StateContext.Stage;
|
||||
import org.springframework.statemachine.state.JoinPseudoState;
|
||||
import org.springframework.statemachine.state.PseudoStateKind;
|
||||
import org.springframework.statemachine.state.State;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
import org.springframework.statemachine.trigger.DefaultTriggerContext;
|
||||
@@ -172,6 +176,9 @@ public class DefaultStateMachineExecutor<S, E> extends LifecycleObjectSupport im
|
||||
interceptors.add(interceptor);
|
||||
}
|
||||
|
||||
private final Set<Transition<S, E>> joinSyncTransitions = new HashSet<>();
|
||||
private final Set<State<S, E>> joinSyncStates = new HashSet<>();
|
||||
|
||||
private boolean handleTriggerTrans(List<Transition<S, E>> trans, Message<E> queuedMessage) {
|
||||
boolean transit = false;
|
||||
for (Transition<S, E> t : trans) {
|
||||
@@ -190,6 +197,28 @@ public class DefaultStateMachineExecutor<S, E> extends LifecycleObjectSupport im
|
||||
continue;
|
||||
}
|
||||
|
||||
// special handling of join
|
||||
if (StateMachineUtils.isPseudoState(t.getTarget(), PseudoStateKind.JOIN)) {
|
||||
if (joinSyncStates.isEmpty()) {
|
||||
List<State<S, E>> joins = ((JoinPseudoState<S, E>)t.getTarget().getPseudoState()).getJoins();
|
||||
joinSyncStates.addAll(joins);
|
||||
}
|
||||
joinSyncTransitions.add(t);
|
||||
boolean removed = joinSyncStates.remove(t.getSource());
|
||||
boolean joincomplete = removed & joinSyncStates.isEmpty();
|
||||
if (joincomplete) {
|
||||
for (Transition<S, E> tt : joinSyncTransitions) {
|
||||
StateContext<S, E> stateContext = buildStateContext(queuedMessage, tt, relayStateMachine);
|
||||
tt.transit(stateContext);
|
||||
stateMachineExecutorTransit.transit(tt, stateContext, queuedMessage);
|
||||
}
|
||||
joinSyncTransitions.clear();
|
||||
break;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
StateContext<S, E> stateContext = buildStateContext(queuedMessage, t, relayStateMachine);
|
||||
try {
|
||||
stateContext = interceptors.preTransition(stateContext);
|
||||
|
||||
@@ -95,6 +95,14 @@ public abstract class StateMachineUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static <S, E> boolean isPseudoState(State<S, E> state, PseudoStateKind kind) {
|
||||
if (state != null) {
|
||||
PseudoState<S, E> pseudoState = state.getPseudoState();
|
||||
return pseudoState != null && pseudoState.getKind() == kind;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static <S> Collection<String> toStringCollection(Collection<S> collection) {
|
||||
Collection<String> c = new ArrayList<String>();
|
||||
for (S item : collection) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.statemachine.state;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -24,18 +25,22 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.statemachine.AbstractStateMachineTests;
|
||||
import org.springframework.statemachine.ObjectStateMachine;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.StateMachineSystemConstants;
|
||||
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.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.support.StateMachineInterceptorAdapter;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
|
||||
public class JoinStateTests extends AbstractStateMachineTests {
|
||||
@@ -105,16 +110,17 @@ public class JoinStateTests extends AbstractStateMachineTests {
|
||||
machine.sendEvent(TestEvents.E1);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(3));
|
||||
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S2, TestStates.S20, TestStates.S30));
|
||||
|
||||
listener.reset(1);
|
||||
machine.sendEvent(TestEvents.E2);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(3);
|
||||
listener.reset(2);
|
||||
machine.sendEvent(TestEvents.E3);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(3));
|
||||
assertThat(listener.stateChangedCount, is(2));
|
||||
|
||||
assertThat(machine.getState().getIds(), contains(TestStates.S4));
|
||||
}
|
||||
@@ -185,14 +191,128 @@ public class JoinStateTests extends AbstractStateMachineTests {
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(3);
|
||||
listener.reset(2);
|
||||
machine.sendEvent(TestEvents.E3);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(2));
|
||||
|
||||
assertThat(machine.getState().getIds(), contains(TestStates.S4));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMultiJoin1() throws Exception {
|
||||
context.register(BaseConfig.class, Config3.class);
|
||||
context.refresh();
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
TestListener listener = new TestListener();
|
||||
machine.addStateListener(listener);
|
||||
listener.reset(1);
|
||||
assertThat(machine, notNullValue());
|
||||
machine.start();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(3);
|
||||
machine.sendEvent(TestEvents.E1);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(3));
|
||||
|
||||
listener.reset(1);
|
||||
machine.sendEvent(TestEvents.E2);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(2);
|
||||
machine.sendEvent(TestEvents.E3);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(2));
|
||||
|
||||
assertThat(machine.getState().getIds(), contains(TestStates.S4));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testMultiJoin2() throws Exception {
|
||||
context.register(BaseConfig.class, Config3.class);
|
||||
context.refresh();
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
TestListener listener = new TestListener();
|
||||
machine.addStateListener(listener);
|
||||
listener.reset(1);
|
||||
assertThat(machine, notNullValue());
|
||||
machine.start();
|
||||
machine.getExtendedState().getVariables().put("foo", "bar");
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(3);
|
||||
machine.sendEvent(TestEvents.E1);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(3));
|
||||
|
||||
listener.reset(1);
|
||||
machine.sendEvent(TestEvents.E2);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(2);
|
||||
machine.sendEvent(TestEvents.E3);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(2));
|
||||
|
||||
assertThat(machine.getState().getIds(), contains(TestStates.SF));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testInterceptorPostStateChangeTransitionNotNull() throws Exception {
|
||||
context.register(BaseConfig.class, Config1.class);
|
||||
context.refresh();
|
||||
ObjectStateMachine<TestStates,TestEvents> machine =
|
||||
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
|
||||
TestListener listener = new TestListener();
|
||||
machine.addStateListener(listener);
|
||||
|
||||
final AtomicBoolean nullCheck = new AtomicBoolean(false);
|
||||
machine.addStateMachineInterceptor(new StateMachineInterceptorAdapter<TestStates,TestEvents>() {
|
||||
@Override
|
||||
public void postStateChange(State<TestStates, TestEvents> state, Message<TestEvents> message,
|
||||
Transition<TestStates, TestEvents> transition, StateMachine<TestStates, TestEvents> stateMachine) {
|
||||
if (state.getId() == TestStates.S4) {
|
||||
nullCheck.set(transition == null);
|
||||
}
|
||||
super.postStateChange(state, message, transition, stateMachine);
|
||||
}
|
||||
});
|
||||
|
||||
listener.reset(1);
|
||||
assertThat(machine, notNullValue());
|
||||
machine.start();
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(3);
|
||||
machine.sendEvent(TestEvents.E1);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(3));
|
||||
|
||||
listener.reset(1);
|
||||
machine.sendEvent(TestEvents.E2);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(1));
|
||||
|
||||
listener.reset(2);
|
||||
machine.sendEvent(TestEvents.E3);
|
||||
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateChangedCount, is(2));
|
||||
|
||||
assertThat(machine.getState().getIds(), contains(TestStates.S4));
|
||||
assertThat("Interceptor postStateChange has null transition", nullCheck.get(), is(false));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
@@ -312,6 +432,71 @@ public class JoinStateTests extends AbstractStateMachineTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
static class Config3 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(TestStates.SI)
|
||||
.state(TestStates.S2)
|
||||
.join(TestStates.S3)
|
||||
.state(TestStates.SF)
|
||||
.state(TestStates.S4)
|
||||
.and()
|
||||
.withStates()
|
||||
.parent(TestStates.S2)
|
||||
.initial(TestStates.S20)
|
||||
.end(TestStates.S21)
|
||||
.and()
|
||||
.withStates()
|
||||
.parent(TestStates.S2)
|
||||
.initial(TestStates.S30)
|
||||
.end(TestStates.S31);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(TestStates.SI)
|
||||
.target(TestStates.S2)
|
||||
.event(TestEvents.E1)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S20)
|
||||
.target(TestStates.S21)
|
||||
.event(TestEvents.E2)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S30)
|
||||
.target(TestStates.S31)
|
||||
.event(TestEvents.E3)
|
||||
.and()
|
||||
.withJoin()
|
||||
.source(TestStates.S2)
|
||||
.target(TestStates.S3)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S3)
|
||||
.target(TestStates.SF)
|
||||
.guardExpression("!extendedState.variables.isEmpty()")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S3)
|
||||
.target(TestStates.S4)
|
||||
.guardExpression("extendedState.variables.isEmpty()")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(TestStates.S4)
|
||||
.target(TestStates.SI)
|
||||
.event(TestEvents.E4);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class TestListener extends StateMachineListenerAdapter<TestStates, TestEvents> {
|
||||
|
||||
volatile CountDownLatch stateChangedLatch = new CountDownLatch(1);
|
||||
|
||||
Reference in New Issue
Block a user