Fixing initial state transitions
- Reported and fixex #81 - Change how internal state is entered and handled when transition doesn't terminate into initial state. - Needed to change stuff in various places because some of the concepts were literally broken which were overlooked due to missing tests.
This commit is contained in:
@@ -348,9 +348,12 @@ public abstract class AbstractStateMachineFactory<S, E> extends LifecycleObjectS
|
||||
continue;
|
||||
}
|
||||
if (stateMachine != null) {
|
||||
PseudoState<S, E> pseudoState = null;
|
||||
if (stateData.isInitial()) {
|
||||
pseudoState = new DefaultPseudoState<S, E>(PseudoStateKind.INITIAL);
|
||||
}
|
||||
state = new StateMachineState<S, E>(stateData.getState(), stateMachine, stateData.getDeferred(),
|
||||
stateData.getEntryActions(), stateData.getExitActions(), new DefaultPseudoState<S, E>(
|
||||
PseudoStateKind.INITIAL));
|
||||
stateData.getEntryActions(), stateData.getExitActions(), pseudoState);
|
||||
// TODO: below if/else doesn't feel right
|
||||
if (stateDatas.size() > 1 && stateData.isInitial()) {
|
||||
initialState = state;
|
||||
|
||||
@@ -162,44 +162,48 @@ public class StateMachineState<S, E> extends AbstractState<S, E> {
|
||||
}
|
||||
}
|
||||
|
||||
if (getPseudoState() != null && getPseudoState().getKind() == PseudoStateKind.INITIAL) {
|
||||
// disable initial state if it looks like we're about
|
||||
// to transit directory into a non initial state
|
||||
// we do transit via initial state if we're returning
|
||||
// via history state
|
||||
boolean initialEnabled = true;
|
||||
if (context.getTransition() != null) {
|
||||
State<S, E> target = context.getTransition().getTarget();
|
||||
PseudoStateKind kind = target.getPseudoState() != null ? target.getPseudoState().getKind() : null;
|
||||
State<S, E> findDeepParent = findDeepParent(getSubmachine().getStates(), target);
|
||||
if (findDeepParent != null && findDeepParent.isSubmachineState()) {
|
||||
((StateMachineState<S, E>) findDeepParent).getSubmachine().getStateMachineAccessor()
|
||||
.doWithRegion(new StateMachineFunction<StateMachineAccess<S, E>>() {
|
||||
if (context.getTransition() != null) {
|
||||
State<S, E> target = context.getTransition().getTarget();
|
||||
State<S, E> immediateDeepParent = findDeepParent(getSubmachine().getStates(), target);
|
||||
|
||||
@Override
|
||||
public void apply(StateMachineAccess<S, E> function) {
|
||||
function.setInitialEnabled(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (getSubmachine().getStates().contains(target) && kind != PseudoStateKind.HISTORY_SHALLOW
|
||||
&& kind != PseudoStateKind.HISTORY_DEEP) {
|
||||
initialEnabled = false;
|
||||
}
|
||||
// disable initial state where needed
|
||||
if (immediateDeepParent != null && immediateDeepParent.isSubmachineState() && ( !isInitial(target) ) ) {
|
||||
|
||||
((StateMachineState<S, E>) immediateDeepParent).getSubmachine().getStateMachineAccessor()
|
||||
.doWithRegion(new StateMachineFunction<StateMachineAccess<S, E>>() {
|
||||
|
||||
@Override
|
||||
public void apply(StateMachineAccess<S, E> function) {
|
||||
function.setInitialEnabled(false);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
// need final for state machine access
|
||||
final boolean enabled = initialEnabled;
|
||||
getSubmachine().getStateMachineAccessor().doWithRegion(
|
||||
new StateMachineFunction<StateMachineAccess<S, E>>() {
|
||||
if (immediateDeepParent != null && !isInitial(immediateDeepParent)) {
|
||||
getSubmachine().getStateMachineAccessor().doWithRegion(
|
||||
new StateMachineFunction<StateMachineAccess<S, E>>() {
|
||||
|
||||
@Override
|
||||
public void apply(StateMachineAccess<S, E> function) {
|
||||
function.setInitialEnabled(enabled);
|
||||
}
|
||||
});
|
||||
getSubmachine().start();
|
||||
@Override
|
||||
public void apply(StateMachineAccess<S, E> function) {
|
||||
function.setInitialEnabled(false);
|
||||
}
|
||||
});
|
||||
} else if (immediateDeepParent != null && isInitial(immediateDeepParent) && isInitial(target)) {
|
||||
((StateMachineState<S, E>) immediateDeepParent).getSubmachine().getStateMachineAccessor().doWithRegion(
|
||||
new StateMachineFunction<StateMachineAccess<S, E>>() {
|
||||
|
||||
@Override
|
||||
public void apply(StateMachineAccess<S, E> function) {
|
||||
function.setInitialEnabled(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
getSubmachine().start();
|
||||
}
|
||||
|
||||
private boolean isInitial(State<S, E> state) {
|
||||
return state.getPseudoState() != null && state.getPseudoState().getKind() == PseudoStateKind.INITIAL;
|
||||
}
|
||||
|
||||
private State<S, E> findDeepParent(Collection<State<S, E>> states, State<S, E> state) {
|
||||
|
||||
@@ -265,6 +265,9 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
protected void doStart() {
|
||||
// if state is set assume nothing to do
|
||||
if (currentState != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("State already set, disabling initial");
|
||||
}
|
||||
stateMachineExecutor.setInitialEnabled(false);
|
||||
stateMachineExecutor.start();
|
||||
return;
|
||||
@@ -272,6 +275,9 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
registerPseudoStateListener();
|
||||
|
||||
if (initialEnabled != null && !initialEnabled) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Initial disable asked, disabling initial");
|
||||
}
|
||||
stateMachineExecutor.setInitialEnabled(false);
|
||||
}
|
||||
|
||||
@@ -326,9 +332,7 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
|
||||
@Override
|
||||
public void setInitialEnabled(boolean enabled) {
|
||||
if (initialEnabled == null) {
|
||||
initialEnabled = enabled;
|
||||
}
|
||||
initialEnabled = enabled;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -554,8 +558,13 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
if (isTargetSubOf && currentState == transition.getTarget()) {
|
||||
state = transition.getSource();
|
||||
}
|
||||
// else if (currentState == null && StateMachineUtils.isSubstate(findDeep, state)) {
|
||||
// state = findDeep;
|
||||
// }
|
||||
}
|
||||
|
||||
boolean nonDeepStatePresent = false;
|
||||
|
||||
if (states.contains(state)) {
|
||||
if (exit) {
|
||||
exitCurrentState(state, message, transition, stateMachine);
|
||||
@@ -567,7 +576,21 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
}
|
||||
entryToState(state, message, transition, stateMachine);
|
||||
notifyStateChanged(notifyFrom, state);
|
||||
} else if (currentState != null) {
|
||||
nonDeepStatePresent = true;
|
||||
} else if (currentState == null && StateMachineUtils.isSubstate(findDeep, state)) {
|
||||
if (exit) {
|
||||
exitCurrentState(findDeep, message, transition, stateMachine);
|
||||
}
|
||||
State<S, E> notifyFrom = currentState;
|
||||
currentState = findDeep;
|
||||
if (!isRunning()) {
|
||||
start();
|
||||
}
|
||||
entryToState(findDeep, message, transition, stateMachine);
|
||||
notifyStateChanged(notifyFrom, findDeep);
|
||||
}
|
||||
|
||||
if (currentState != null && !nonDeepStatePresent) {
|
||||
if (findDeep != null) {
|
||||
if (exit) {
|
||||
exitCurrentState(state, message, transition, stateMachine);
|
||||
@@ -603,9 +626,12 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean shouldEntry = findDeep != currentState;
|
||||
boolean shouldTryEntry = findDeep != currentState;
|
||||
if (!shouldTryEntry && (transition.getSource() == currentState && StateMachineUtils.isSubstate(currentState, transition.getTarget()))) {
|
||||
shouldTryEntry = true;
|
||||
}
|
||||
currentState = findDeep;
|
||||
if (shouldEntry) {
|
||||
if (shouldTryEntry) {
|
||||
entryToState(currentState, message, transition, stateMachine);
|
||||
}
|
||||
|
||||
@@ -654,7 +680,6 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
|
||||
State<S, E> findDeep = findDeepParent(transition.getTarget());
|
||||
boolean isTargetSubOfOtherState = findDeep != null && findDeep != currentState;
|
||||
boolean isTargetSubOfSource = StateMachineUtils.isSubstate(transition.getSource(), transition.getTarget());
|
||||
boolean isSubOfSource = StateMachineUtils.isSubstate(transition.getSource(), currentState);
|
||||
boolean isSubOfTarget = StateMachineUtils.isSubstate(transition.getTarget(), currentState);
|
||||
|
||||
@@ -666,14 +691,11 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
} else if (!isSubOfSource && !isSubOfTarget && currentState == transition.getTarget()) {
|
||||
} else if (isTargetSubOfOtherState) {
|
||||
} else if (!isSubOfSource && !isSubOfTarget && findDeep == null) {
|
||||
} else if (!isSubOfSource && !isSubOfTarget && (transition.getSource() == currentState && StateMachineUtils.isSubstate(currentState, transition.getTarget()))) {
|
||||
} else if (!isSubOfSource && !isSubOfTarget) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (transition.getSource() == currentState && isTargetSubOfSource) {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
log.debug("Exit state=[" + state + "]");
|
||||
@@ -700,7 +722,7 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
|
||||
} else if (isComingFromOtherSubmachine) {
|
||||
} else if (!isSubOfSource && !isSubOfTarget && findDeep2 == null) {
|
||||
} else if (isSubOfSource && !isSubOfTarget && currentState == transition.getTarget()) {
|
||||
return;
|
||||
} else if (!isSubOfSource && !isSubOfTarget && (transition.getSource() == currentState && StateMachineUtils.isSubstate(currentState, transition.getTarget()))) {
|
||||
} else if (!isSubOfSource && !isSubOfTarget) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -146,9 +146,9 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
|
||||
assertThat(entryActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true));
|
||||
assertThat(exitActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true));
|
||||
|
||||
assertThat(entryActionS11.stateContexts.size(), is(1));
|
||||
assertThat(exitActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS111.stateContexts.size(), is(2));
|
||||
assertThat(exitActionS111.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS11.stateContexts.size(), is(2));
|
||||
assertThat(exitActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS1.stateContexts.size(), is(1));
|
||||
assertThat(exitActionS1.stateContexts.size(), is(1));
|
||||
@@ -336,9 +336,9 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
|
||||
assertThat(entryActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true));
|
||||
assertThat(exitActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(false));
|
||||
|
||||
assertThat(entryActionS11.stateContexts.size(), is(1));
|
||||
assertThat(exitActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS111.stateContexts.size(), is(2));
|
||||
assertThat(exitActionS111.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS11.stateContexts.size(), is(2));
|
||||
assertThat(exitActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS1.stateContexts.size(), is(1));
|
||||
assertThat(exitActionS1.stateContexts.size(), is(0));
|
||||
@@ -371,9 +371,9 @@ public class SubStateMachineTests extends AbstractStateMachineTests {
|
||||
assertThat(entryActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true));
|
||||
assertThat(exitActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true));
|
||||
|
||||
assertThat(entryActionS11.stateContexts.size(), is(1));
|
||||
assertThat(exitActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS111.stateContexts.size(), is(2));
|
||||
assertThat(exitActionS111.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS11.stateContexts.size(), is(2));
|
||||
assertThat(exitActionS11.stateContexts.size(), is(1));
|
||||
assertThat(entryActionS1.stateContexts.size(), is(1));
|
||||
assertThat(exitActionS1.stateContexts.size(), is(1));
|
||||
|
||||
@@ -200,7 +200,7 @@ public class TransitionTests extends AbstractStateMachineTests {
|
||||
listener.reset(0, 2);
|
||||
machine.sendEvent(TestEvents2.PAUSE);
|
||||
assertThat(listener.stateEnteredLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateEnteredCount, is(2));
|
||||
assertThat(listener.stateEnteredCount, is(3));
|
||||
assertThat(machine.getState().getIds(), contains(TestStates2.BUSY, TestStates2.PAUSED));
|
||||
}
|
||||
|
||||
@@ -221,10 +221,10 @@ public class TransitionTests extends AbstractStateMachineTests {
|
||||
assertThat(listener.stateChangedCount, is(2));
|
||||
assertThat(machine.getState().getIds(), contains(TestStates2.IDLE, TestStates2.CLOSED));
|
||||
|
||||
listener.reset(0, 4);
|
||||
listener.reset(0, 3);
|
||||
machine.sendEvent(TestEvents2.PAUSE);
|
||||
assertThat(listener.stateEnteredLatch.await(2, TimeUnit.SECONDS), is(true));
|
||||
assertThat(listener.stateEnteredCount, is(4));
|
||||
assertThat(listener.stateEnteredCount, is(3));
|
||||
assertThat(machine.getState().getIds(), contains(TestStates2.BUSY, TestStates2.PAUSED, TestStates2.PAUSED2));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user