Example usage for org.springframework.statemachine StateMachineContext getState

List of usage examples for org.springframework.statemachine StateMachineContext getState

Introduction

In this page you can find the example usage for org.springframework.statemachine StateMachineContext getState.

Prototype

S getState();

Source Link

Document

Gets the state.

Usage

From source file:org.springframework.statemachine.recipes.TasksHandlerTests.java

@Test
public void testPersist1() throws InterruptedException {
    TestStateMachinePersist persist = new TestStateMachinePersist();
    TasksHandler handler = TasksHandler.builder().task("1", sleepRunnable()).task("2", sleepRunnable())
            .task("3", sleepRunnable()).persist(persist).build();

    TestListener listener = new TestListener();
    listener.reset(9, 0, 0);//from  w w w  . j  ava 2 s  .  c om
    StateMachine<String, String> machine = handler.getStateMachine();
    machine.addStateListener(listener);
    machine.start();
    assertThat(listener.stateMachineStartedLatch.await(1, TimeUnit.SECONDS), is(true));

    persist.reset(5);

    handler.runTasks();

    assertThat(listener.stateChangedLatch.await(8, TimeUnit.SECONDS), is(true));
    assertThat(listener.stateChangedCount, is(9));
    assertThat(machine.getState().getIds(), contains(TasksHandler.STATE_READY));
    Map<Object, Object> variables = machine.getExtendedState().getVariables();
    assertThat(variables.size(), is(3));

    assertThat(persist.writeLatch.await(4, TimeUnit.SECONDS), is(true));
    assertThat(persist.contexts.size(), is(5));

    for (StateMachineContext<String, String> context : persist.getContexts()) {
        if (context.getState() == "TASKS") {
            assertThat(context.getChilds().size(), is(3));
        } else {
            assertThat(context.getChilds().size(), is(0));
        }
    }
}

From source file:org.springframework.statemachine.recipes.TasksHandlerTests.java

@Test
public void testPersist2() throws InterruptedException {
    TestStateMachinePersist persist = new TestStateMachinePersist();
    TasksHandler handler = TasksHandler.builder().task("1", sleepRunnable()).task("2", sleepRunnable())
            .task("3", failRunnable()).persist(persist).build();

    TestListener listener = new TestListener();
    listener.reset(11, 0, 0);//from  ww w. j a v a 2s  .  c o m
    StateMachine<String, String> machine = handler.getStateMachine();
    machine.addStateListener(listener);
    machine.start();
    assertThat(listener.stateMachineStartedLatch.await(1, TimeUnit.SECONDS), is(true));

    persist.reset(6);

    handler.runTasks();

    assertThat(listener.stateChangedLatch.await(8, TimeUnit.SECONDS), is(true));
    assertThat(listener.stateChangedCount, is(11));
    assertThat(machine.getState().getIds(), contains(TasksHandler.STATE_ERROR, TasksHandler.STATE_MANUAL));
    Map<Object, Object> variables = machine.getExtendedState().getVariables();
    assertThat(variables.size(), is(3));

    assertThat(persist.writeLatch.await(4, TimeUnit.SECONDS), is(true));
    assertThat(persist.contexts.size(), is(6));

    for (StateMachineContext<String, String> context : persist.getContexts()) {
        if (context.getState() == "TASKS") {
            assertThat(context.getChilds().size(), is(3));
        } else if (context.getState() == "ERROR") {
            assertThat(context.getChilds().size(), is(1));
        } else {
            assertThat(context.getChilds().size(), is(0));
        }
    }
}

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   ww  w . j a v  a2s . c o  m
        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();
    }
}