Example usage for org.springframework.statemachine StateMachine getStates

List of usage examples for org.springframework.statemachine StateMachine getStates

Introduction

In this page you can find the example usage for org.springframework.statemachine StateMachine getStates.

Prototype

Collection<State<S, E>> getStates();

Source Link

Document

Gets the State s defined in this region.

Usage

From source file:com.netflix.genie.agent.execution.statemachine.StateMachineAutoConfiguration.java

private void registerListeners(final Collection<JobExecutionListener> listeners,
        final StateMachine<States, Events> stateMachine) {
    // Add state machine listeners
    listeners.forEach(stateMachine::addStateListener);

    // Add state action listeners to each state
    for (JobExecutionListener listener : listeners) {
        stateMachine.addStateListener(listener);
        for (State<States, Events> state : stateMachine.getStates()) {
            state.addActionListener(listener);
        }/*  w ww.j a  v  a2  s .c  om*/
    }
}

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;//w  ww.jav a2 s  . com
        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();
    }
}

From source file:org.springframework.statemachine.support.AbstractStateMachine.java

private static <S, E> boolean isDirectSubstate(State<S, E> left, State<S, E> right) {
    // Checks if right hand side is a direct substate of a left hand side.
    if (left != null && left.isSubmachineState()) {
        StateMachine<S, E> submachine = ((AbstractState<S, E>) left).getSubmachine();
        return submachine.getStates().contains(right);
    } else {// w ww.  j a  v  a 2  s . c  om
        return false;
    }
}