Example usage for org.springframework.statemachine.config.builders StateMachineTransitionConfigurer withJoin

List of usage examples for org.springframework.statemachine.config.builders StateMachineTransitionConfigurer withJoin

Introduction

In this page you can find the example usage for org.springframework.statemachine.config.builders StateMachineTransitionConfigurer withJoin.

Prototype

JoinTransitionConfigurer<S, E> withJoin() throws Exception;

Source Link

Document

Gets a configurer for transition from a join pseudostate.

Usage

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

private StateMachine<String, String> buildStateMachine(List<TaskWrapper> tasks, TaskExecutor taskExecutor)
        throws Exception {
    StateMachineBuilder.Builder<String, String> builder = StateMachineBuilder.builder();

    int taskCount = topLevelTaskCount(tasks);

    builder.configureConfiguration().withConfiguration()
            .taskExecutor(taskExecutor != null ? taskExecutor : taskExecutor(taskCount));

    StateMachineStateConfigurer<String, String> stateMachineStateConfigurer = builder.configureStates();
    StateMachineTransitionConfigurer<String, String> stateMachineTransitionConfigurer = builder
            .configureTransitions();/*from  www.  j ava2 s.c  o  m*/

    stateMachineStateConfigurer.withStates().initial(STATE_READY).fork(STATE_FORK)
            .state(STATE_TASKS, tasksEntryAction(), null).join(STATE_JOIN).choice(STATE_CHOICE)
            .state(STATE_ERROR);

    stateMachineTransitionConfigurer.withExternal().source(STATE_READY).target(STATE_FORK).event(EVENT_RUN)
            .and().withFork().source(STATE_FORK).target(STATE_TASKS);

    Iterator<Node<TaskWrapper>> iterator = buildTasksIterator(tasks);
    String parent = null;
    Collection<String> joinStates = new ArrayList<String>();
    while (iterator.hasNext()) {
        Node<TaskWrapper> node = iterator.next();
        if (node.getData() == null) {
            break;
        }
        String initial = STATE_TASKS_PREFIX + node.getData().id.toString() + STATE_TASKS_INITIAL_POSTFIX;
        String task = STATE_TASKS_PREFIX + node.getData().id.toString();
        parent = node.getData().parent != null ? STATE_TASKS_PREFIX + node.getData().parent.toString()
                : STATE_TASKS;

        stateMachineStateConfigurer.withStates().parent(parent).initial(initial).state(task,
                runnableAction(node.getData().runnable, node.getData().id.toString()), null);

        if (node.getChildren().isEmpty()) {
            joinStates.add(task);
        }

        stateMachineTransitionConfigurer.withExternal().state(parent).source(initial).target(task);
    }

    stateMachineStateConfigurer.withStates().parent(STATE_ERROR).initial(STATE_AUTOMATIC)
            .state(STATE_AUTOMATIC, automaticAction(), null).state(STATE_MANUAL);

    stateMachineTransitionConfigurer.withJoin().sources(joinStates).target(STATE_JOIN).and().withExternal()
            .source(STATE_JOIN).target(STATE_CHOICE).and().withChoice().source(STATE_CHOICE)
            .first(STATE_ERROR, tasksChoiceGuard()).last(STATE_READY).and().withExternal().source(STATE_ERROR)
            .target(STATE_READY).event(EVENT_CONTINUE).action(continueAction()).and().withExternal()
            .source(STATE_AUTOMATIC).target(STATE_MANUAL).event(EVENT_FALLBACK).and().withInternal()
            .source(STATE_MANUAL).action(fixAction()).event(EVENT_FIX);

    return builder.build();
}