Example usage for org.springframework.statemachine.config StateMachineBuilder builder

List of usage examples for org.springframework.statemachine.config StateMachineBuilder builder

Introduction

In this page you can find the example usage for org.springframework.statemachine.config StateMachineBuilder builder.

Prototype

public static <S, E> Builder<S, E> builder() 

Source Link

Document

Gets a builder for a StateMachine .

Usage

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

@Bean
@Lazy//from   w ww .j  av a2 s. c  o m
StateMachine<States, Events> stateMachine(final Collection<Pair<States, StateAction>> statesWithActions,
        final Collection<Triple<States, Events, States>> eventDrivenTransitions,
        final Collection<States> statesWithErrorTransition, final Collection<JobExecutionListener> listeners)
        throws Exception {

    final StateMachineBuilder.Builder<States, Events> builder = new StateMachineBuilder.Builder<>();

    configureConfiguration(builder);

    configureStates(builder, statesWithActions);

    configureTransitions(builder, eventDrivenTransitions, statesWithErrorTransition);

    // Build state machine
    final StateMachine<States, Events> stateMachine = builder.build();

    registerListeners(listeners, stateMachine);

    return stateMachine;
}

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

/**
 * Provide a lazy {@link StateMachine} instance configured with the current model expected for job execution.
 *
 * @param statesWithActions         The states to use that have actions associated with them available in the
 *                                  Spring context
 * @param eventDrivenTransitions    The event driven transitions available in the Spring context
 * @param statesWithErrorTransition The states that have error transitions associated with them available in the
 *                                  Spring context
 * @param listeners                 Any {@link JobExecutionListener} implementations available in the Spring context
 * @return A {@link StateMachine} instance configured with the available options from the application context
 * @throws Exception On any error configuring the state machine
 *///  w  w  w.  j  a v  a 2  s.c o m
@Bean
@Lazy
public StateMachine<States, Events> stateMachine(final Collection<Pair<States, StateAction>> statesWithActions,
        final Collection<Triple<States, Events, States>> eventDrivenTransitions,
        final Collection<States> statesWithErrorTransition, final Collection<JobExecutionListener> listeners)
        throws Exception {
    final StateMachineBuilder.Builder<States, Events> builder = new StateMachineBuilder.Builder<>();

    configureConfiguration(builder);

    configureStates(builder, statesWithActions);

    configureTransitions(builder, eventDrivenTransitions, statesWithErrorTransition);

    // Build state machine
    final StateMachine<States, Events> stateMachine = builder.build();

    registerListeners(listeners, stateMachine);

    return stateMachine;
}

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

private void configureConfiguration(final StateMachineBuilder.Builder<States, Events> builder)
        throws Exception {
    builder.configureConfiguration().withConfiguration().machineId(STATE_MACHINE_ID);
}

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

private void configureStates(final StateMachineBuilder.Builder<States, Events> builder,
        final Collection<Pair<States, StateAction>> statesWithActions) throws Exception {
    // Set up initial and terminal states (action-free)
    final StateConfigurer<States, Events> stateConfigurer = builder.configureStates().withStates()
            .initial(States.READY).end(States.END);

    // Set up the rest of the states with their corresponding action
    for (Pair<States, StateAction> stateWithAction : statesWithActions) {
        final States state = stateWithAction.getLeft();
        final StateAction action = stateWithAction.getRight();
        stateConfigurer//from w  ww.  j  a  v a2s  .  c  o  m
                // Use entryAction because it is not interruptible.
                // StateAction is susceptible to cancellation in case of event-triggered transition out of the state.
                .state(state, action, null);
        log.info("Configured state {} with action {}", state, action.getClass().getSimpleName());
    }
}

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

private void configureTransitions(final StateMachineBuilder.Builder<States, Events> builder,
        final Collection<Triple<States, Events, States>> eventDrivenTransitions,
        final Collection<States> statesWithErrorTransition) throws Exception {
    final StateMachineTransitionConfigurer<States, Events> transitionConfigurer = builder
            .configureTransitions();/* ww w  .java  2 s.  c om*/

    // Set up event-driven transitions
    for (Triple<States, Events, States> transition : eventDrivenTransitions) {
        final States sourceState = transition.getLeft();
        final States targetState = transition.getRight();
        final Events event = transition.getMiddle();
        transitionConfigurer.withExternal().source(sourceState).target(targetState).event(event).and();
        log.info("Configured event-driven transition: ({}) -> [{}] -> ({})", sourceState, event, targetState);
    }

    // Set up transitions to HANDLE_ERROR state.
    for (States state : statesWithErrorTransition) {
        transitionConfigurer.withExternal().source(state).target(States.HANDLE_ERROR).event(Events.ERROR).and();
        log.info("Configured error transition: ({}) -> ({})", state, States.HANDLE_ERROR);
    }

    // Add transition from HANDLE_ERROR to END
    transitionConfigurer.withExternal().source(States.HANDLE_ERROR).target(States.END)
            .event(Events.HANDLE_ERROR_COMPLETE);
}

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  w  w w .j a v  a2  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();
}