List of usage examples for org.springframework.statemachine.access StateMachineAccess resetStateMachine
void resetStateMachine(StateMachineContext<S, E> stateMachineContext);
From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java
/** * Resets state machine states from a backing persistent repository. If * {@link StateMachinePersist} is not set this method doesn't do anything. * {@link StateMachine} is stopped before states are reseted from a persistent * store and started afterwards.//from ww w . j a va2 s .c o m */ public void resetFromPersistStore() { if (persist == null) { // TODO: should we throw or silently return? return; } final StateMachineContext<String, String> context; try { context = persist.read(null); } catch (Exception e) { throw new StateMachineException("Error reading state from persistent store", e); } stateMachine.stop(); stateMachine.getStateMachineAccessor() .doWithAllRegions(new StateMachineFunction<StateMachineAccess<String, String>>() { @Override public void apply(StateMachineAccess<String, String> function) { function.resetStateMachine(context); } }); stateMachine.start(); }
From source file:org.springframework.statemachine.support.AbstractStateMachine.java
@Override public void resetStateMachine(StateMachineContext<S, E> stateMachineContext) { // TODO: this function needs a serious rewrite if (stateMachineContext == null) { log.info("Got null context, resetting to initial state and clearing extended state"); currentState = initialState;/*from w w w .ja v a 2 s . c om*/ extendedState.getVariables().clear(); return; } if (log.isDebugEnabled()) { log.debug("Request to reset state machine: stateMachine=[" + this + "] stateMachineContext=[" + stateMachineContext + "]"); } setId(stateMachineContext.getId()); S state = stateMachineContext.getState(); boolean stateSet = false; // handle state reset for (State<S, E> s : getStates()) { for (State<S, E> ss : s.getStates()) { if (state != null && ss.getIds().contains(state)) { currentState = s; // setting lastState here is needed for restore lastState = currentState; // TODO: not sure about starting submachine/regions here, though // needed if we only transit to super state or reset regions if (s.isSubmachineState()) { StateMachine<S, E> submachine = ((AbstractState<S, E>) s).getSubmachine(); for (final StateMachineContext<S, E> child : stateMachineContext.getChilds()) { submachine.getStateMachineAccessor() .doWithRegion(new StateMachineFunction<StateMachineAccess<S, E>>() { @Override public void apply(StateMachineAccess<S, E> function) { function.resetStateMachine(child); } }); } submachine.start(); } else if (s.isOrthogonal() && stateMachineContext.getChilds() != null) { Collection<Region<S, E>> regions = ((AbstractState<S, E>) s).getRegions(); for (Region<S, E> region : regions) { for (final StateMachineContext<S, E> child : stateMachineContext.getChilds()) { ((StateMachine<S, E>) region).getStateMachineAccessor() .doWithRegion(new StateMachineFunction<StateMachineAccess<S, E>>() { @Override public void apply(StateMachineAccess<S, E> function) { function.resetStateMachine(child); } }); } } for (Region<S, E> region : regions) { region.start(); } } if (log.isDebugEnabled()) { log.debug("State reseted: stateMachine=[" + this + "] stateMachineContext=[" + stateMachineContext + "]"); } stateSet = true; break; } else if (!stateMachineContext.getChilds().isEmpty()) { // we're here because root machine only have regions if (s.isOrthogonal()) { Collection<Region<S, E>> regions = ((AbstractState<S, E>) s).getRegions(); for (Region<S, E> region : regions) { for (final StateMachineContext<S, E> child : stateMachineContext.getChilds()) { ((StateMachine<S, E>) region).getStateMachineAccessor() .doWithRegion(new StateMachineFunction<StateMachineAccess<S, E>>() { @Override public void apply(StateMachineAccess<S, E> function) { function.resetStateMachine(child); } }); } } for (Region<S, E> region : regions) { region.start(); } } } } if (stateSet) { break; } } // handle history reset here as above state reset loop breaks out if (history != null && stateMachineContext.getHistoryStates() != null) { // setting history for 'this' machine State<S, E> h = null; for (State<S, E> hh : getStates()) { if (hh.getId().equals(stateMachineContext.getHistoryStates().get(null))) { h = hh; break; } } if (h != null) { ((HistoryPseudoState<S, E>) history).setState(h); } } for (State<S, E> s : getStates()) { // setting history for 'submachines' if (s.isSubmachineState()) { StateMachine<S, E> submachine = ((AbstractState<S, E>) s).getSubmachine(); PseudoState<S, E> submachineHistory = ((AbstractStateMachine<S, E>) submachine).getHistoryState(); if (submachineHistory != null) { State<S, E> h = null; for (State<S, E> hh : submachine.getStates()) { if (hh.getId().equals(stateMachineContext.getHistoryStates().get(s.getId()))) { h = hh; break; } } if (h != null) { ((HistoryPseudoState<S, E>) submachineHistory).setState(h); } } } } if (stateSet && stateMachineContext.getExtendedState() != null) { this.extendedState = stateMachineContext.getExtendedState(); } }