Tune action handling and concept
- Statemachine now available in StateContext - Added single actions to StateConfigurer instead of forcing to use Collection.
This commit is contained in:
@@ -60,4 +60,11 @@ public interface StateContext<S, E> {
|
||||
*/
|
||||
Transition<S, E> getTransition();
|
||||
|
||||
/**
|
||||
* Gets the state machine.
|
||||
*
|
||||
* @return the state machine
|
||||
*/
|
||||
StateMachine<S, E> getStateMachine();
|
||||
|
||||
}
|
||||
|
||||
@@ -87,6 +87,21 @@ public class DefaultStateConfigurer<S, E>
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StateConfigurer<S, E> state(S state, Action<S, E> entryAction, Action<S, E> exitAction) {
|
||||
Collection<Action<S, E>> entryActions = null;
|
||||
if (entryAction != null) {
|
||||
entryActions = new ArrayList<Action<S, E>>(1);
|
||||
entryActions.add(entryAction);
|
||||
}
|
||||
Collection<Action<S, E>> exitActions = null;
|
||||
if (exitAction != null) {
|
||||
exitActions = new ArrayList<Action<S, E>>(1);
|
||||
exitActions.add(exitAction);
|
||||
}
|
||||
return state(state, entryActions, exitActions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StateConfigurer<S, E> state(S state, E... deferred) {
|
||||
Collection<E> d = null;
|
||||
|
||||
@@ -34,6 +34,8 @@ public interface StateConfigurer<S, E> extends
|
||||
StateConfigurer<S, E> state(S state, Collection<? extends Action<S, E>> entryActions,
|
||||
Collection<? extends Action<S, E>> exitActions);
|
||||
|
||||
StateConfigurer<S, E> state(S state, Action<S, E> entryAction, Action<S, E> exitAction);
|
||||
|
||||
StateConfigurer<S, E> state(S state, E... deferred);
|
||||
|
||||
StateConfigurer<S, E> states(Set<S> states);
|
||||
|
||||
@@ -312,7 +312,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
log.trace("Exit state=[" + state + "]");
|
||||
MessageHeaders messageHeaders = event != null ? event.getHeaders() : new MessageHeaders(
|
||||
new HashMap<String, Object>());
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, transition);
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, transition, this);
|
||||
state.exit(event != null ? event.getPayload() : null, stateContext);
|
||||
}
|
||||
}
|
||||
@@ -322,7 +322,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
log.trace("Enter state=[" + state + "]");
|
||||
MessageHeaders messageHeaders = event != null ? event.getHeaders() : new MessageHeaders(
|
||||
new HashMap<String, Object>());
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, transition);
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, transition, this);
|
||||
state.entry(event != null ? event.getPayload() : null, stateContext);
|
||||
}
|
||||
}
|
||||
@@ -395,7 +395,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
while ((queueItem = triggerQueue.poll()) != null) {
|
||||
Message<E> queuedEvent = queueItem.message;
|
||||
Transition<S, E> transition = triggerToTransitionMap.get(queueItem.trigger);
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(queuedEvent != null ? queuedEvent.getHeaders() : null, extendedState, transition);
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(queuedEvent != null ? queuedEvent.getHeaders() : null, extendedState, transition, this);
|
||||
if (transition == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -426,7 +426,7 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
|
||||
if (sourceState != null && targetState != null) {
|
||||
MessageHeaders messageHeaders = event != null ? event.getHeaders() : new MessageHeaders(
|
||||
new HashMap<String, Object>());
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, null);
|
||||
StateContext<S, E> stateContext = new DefaultStateContext<S, E>(messageHeaders, extendedState, null, this);
|
||||
getStateMachineHandlerResults(getStateMachineHandlers(sourceState, targetState), stateContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.statemachine.support;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.statemachine.ExtendedState;
|
||||
import org.springframework.statemachine.StateContext;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
|
||||
public class DefaultStateContext<S, E> implements StateContext<S, E> {
|
||||
@@ -28,10 +29,13 @@ public class DefaultStateContext<S, E> implements StateContext<S, E> {
|
||||
|
||||
private final Transition<S,E> transition;
|
||||
|
||||
public DefaultStateContext(MessageHeaders messageHeaders, ExtendedState extendedState, Transition<S,E> transition) {
|
||||
private final StateMachine<S, E> stateMachine;
|
||||
|
||||
public DefaultStateContext(MessageHeaders messageHeaders, ExtendedState extendedState, Transition<S,E> transition, StateMachine<S, E> stateMachine) {
|
||||
this.messageHeaders = messageHeaders;
|
||||
this.extendedState = extendedState;
|
||||
this.transition = transition;
|
||||
this.stateMachine = stateMachine;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,4 +63,9 @@ public class DefaultStateContext<S, E> implements StateContext<S, E> {
|
||||
return transition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StateMachine<S, E> getStateMachine() {
|
||||
return stateMachine;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -252,6 +252,7 @@ public class ConfigurationTests extends AbstractStateMachineTests {
|
||||
.withStates()
|
||||
.initial(TestStates.S11)
|
||||
.state(TestStates.S11, actions1, Arrays.asList(action2()))
|
||||
.state(TestStates.S12, action1(), action2())
|
||||
.and()
|
||||
.withStates()
|
||||
.parent(TestStates.S11)
|
||||
|
||||
@@ -59,7 +59,7 @@ public class SpelExpressionGuardTests extends AbstractStateMachineTests {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("foo", "bar");
|
||||
MessageHeaders headers = new MessageHeaders(map);
|
||||
DefaultStateContext<TestStates, TestEvents> stateContext = new DefaultStateContext<TestStates, TestEvents>(headers, null, null);
|
||||
DefaultStateContext<TestStates, TestEvents> stateContext = new DefaultStateContext<TestStates, TestEvents>(headers, null, null, null);
|
||||
|
||||
assertThat(guard.evaluate(stateContext), is(true));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user