private AsynchronousSearchStateMachine initStateMachine()

in src/main/java/org/opensearch/search/asynchronous/service/AsynchronousSearchService.java [569:615]


    private AsynchronousSearchStateMachine initStateMachine() {
        AsynchronousSearchStateMachine stateMachine = new AsynchronousSearchStateMachine(
                EnumSet.allOf(AsynchronousSearchState.class), INIT, contextEventListener);

        stateMachine.markTerminalStates(EnumSet.of(CLOSED));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(INIT, RUNNING,
                (s, e) -> ((AsynchronousSearchActiveContext) e.asynchronousSearchContext()).setTask(e.getSearchTask()),
                (contextId, listener) -> listener.onContextRunning(contextId), SearchStartedEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(RUNNING, SUCCEEDED,
                (s, e) -> ((AsynchronousSearchActiveContext) e.asynchronousSearchContext()).processSearchResponse(e.getSearchResponse()),
                (contextId, listener) -> listener.onContextCompleted(contextId), SearchSuccessfulEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(RUNNING, FAILED,
                (s, e) -> ((AsynchronousSearchActiveContext) e.asynchronousSearchContext()).processSearchFailure(e.getException()),
                (contextId, listener) -> listener.onContextFailed(contextId), SearchFailureEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(SUCCEEDED, PERSISTING,
                (s, e) -> asynchronousSearchPostProcessor.persistResponse((AsynchronousSearchActiveContext) e.asynchronousSearchContext(),
                        e.getAsynchronousSearchPersistenceModel()),
                (contextId, listener) -> {}, BeginPersistEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(FAILED, PERSISTING,
                (s, e) -> asynchronousSearchPostProcessor.persistResponse((AsynchronousSearchActiveContext) e.asynchronousSearchContext(),
                        e.getAsynchronousSearchPersistenceModel()),
                (contextId, listener) -> {}, BeginPersistEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(PERSISTING, PERSIST_SUCCEEDED,
                (s, e) -> {},
                (contextId, listener) -> listener.onContextPersisted(contextId), SearchResponsePersistedEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(PERSISTING, PERSIST_FAILED,
                (s, e) -> {},
                (contextId, listener) -> listener.onContextPersistFailed(contextId), SearchResponsePersistFailedEvent.class));

        stateMachine.registerTransition(new AsynchronousSearchTransition<>(RUNNING, CLOSED,
                (s, e) -> asynchronousSearchActiveStore.freeContext(e.asynchronousSearchContext().getContextId()),
                (contextId, listener) -> listener.onRunningContextDeleted(contextId), SearchDeletedEvent.class));

        for (AsynchronousSearchState state : EnumSet.of(PERSISTING, PERSIST_SUCCEEDED, PERSIST_FAILED, SUCCEEDED, FAILED, INIT)) {
            stateMachine.registerTransition(new AsynchronousSearchTransition<>(state, CLOSED,
                    (s, e) -> asynchronousSearchActiveStore.freeContext(e.asynchronousSearchContext().getContextId()),
                    (contextId, listener) -> listener.onContextDeleted(contextId), SearchDeletedEvent.class));
        }
        return stateMachine;
    }