Class AbstractStateMachine<S,E>

Type Parameters:
S - the type of state
E - the type of event
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle, ReactiveStateMachineAccess<S,E>, StateMachineAccess<S,E>, Region<S,E>, StateMachine<S,E>, StateMachineReactiveLifecycle
Direct Known Subclasses:
ObjectStateMachine

public abstract class AbstractStateMachine<S,E> extends StateMachineObjectSupport<S,E> implements StateMachine<S,E>, StateMachineAccess<S,E>
Base implementation of a StateMachine loosely modelled from UML state machine.
  • Constructor Details

    • AbstractStateMachine

      public AbstractStateMachine(Collection<State<S,E>> states, Collection<Transition<S,E>> transitions, State<S,E> initialState)
      Instantiates a new abstract state machine.
      Parameters:
      states - the states of this machine
      transitions - the transitions of this machine
      initialState - the initial state of this machine
    • AbstractStateMachine

      public AbstractStateMachine(Collection<State<S,E>> states, Collection<Transition<S,E>> transitions, State<S,E> initialState, ExtendedState extendedState)
      Instantiates a new abstract state machine.
      Parameters:
      states - the states of this machine
      transitions - the transitions of this machine
      initialState - the initial state of this machine
      extendedState - the extended state of this machine
    • AbstractStateMachine

      public AbstractStateMachine(Collection<State<S,E>> states, Collection<Transition<S,E>> transitions, State<S,E> initialState, Transition<S,E> initialTransition, org.springframework.messaging.Message<E> initialEvent, ExtendedState extendedState, UUID uuid)
      Instantiates a new abstract state machine.
      Parameters:
      states - the states of this machine
      transitions - the transitions of this machine
      initialState - the initial state of this machine
      initialTransition - the initial transition
      initialEvent - the initial event of this machine
      extendedState - the extended state of this machine
      uuid - the given uuid for this machine
  • Method Details

    • getState

      public State<S,E> getState()
      Description copied from interface: Region
      Gets the current State.
      Specified by:
      getState in interface Region<S,E>
      Returns:
      current state
    • getInitialState

      public State<S,E> getInitialState()
      Description copied from interface: StateMachine
      Gets the initial state S.
      Specified by:
      getInitialState in interface StateMachine<S,E>
      Returns:
      initial state
    • getExtendedState

      public ExtendedState getExtendedState()
      Description copied from interface: StateMachine
      Gets the state machine extended state.
      Specified by:
      getExtendedState in interface StateMachine<S,E>
      Returns:
      extended state
    • setHistoryState

      public void setHistoryState(PseudoState<S,E> history)
      Parameters:
      history - to set internal history state.
    • getHistoryState

      public PseudoState<S,E> getHistoryState()
      Returns:
      history state attribute.
    • sendEvent

      public boolean sendEvent(org.springframework.messaging.Message<E> event)
      Description copied from interface: Region
      Send an event E wrapped with a Message to the region.

      NOTE: this method is now deprecated in favour of a reactive methods.

      Specified by:
      sendEvent in interface Region<S,E>
      Parameters:
      event - the wrapped event to send
      Returns:
      true if event was accepted
    • notifyEventNotAccepted

      protected void notifyEventNotAccepted(StateContext<S,E> stateContext)
      Overrides:
      notifyEventNotAccepted in class StateMachineObjectSupport<S,E>
    • sendEvent

      public boolean sendEvent(E event)
      Description copied from interface: Region
      Send an event E to the region.

      NOTE: this method is now deprecated in favour of a reactive methods.

      Specified by:
      sendEvent in interface Region<S,E>
      Parameters:
      event - the event to send
      Returns:
      true if event was accepted
    • sendEvents

      public reactor.core.publisher.Flux<StateMachineEventResult<S,E>> sendEvents(reactor.core.publisher.Flux<org.springframework.messaging.Message<E>> events)
      Description copied from interface: Region
      Send a Flux of events and return a Flux of StateMachineEventResults. Events are consumed after returned results are consumed.
      Specified by:
      sendEvents in interface Region<S,E>
      Parameters:
      events - the events
      Returns:
      the event results
    • sendEvent

      public reactor.core.publisher.Flux<StateMachineEventResult<S,E>> sendEvent(reactor.core.publisher.Mono<org.springframework.messaging.Message<E>> event)
      Description copied from interface: Region
      Send a Mono of event and return a Flux of StateMachineEventResults. Events are consumed after returned results are consumed.
      Specified by:
      sendEvent in interface Region<S,E>
      Parameters:
      event - the event
      Returns:
      the event results
    • sendEventCollect

      public reactor.core.publisher.Mono<List<StateMachineEventResult<S,E>>> sendEventCollect(reactor.core.publisher.Mono<org.springframework.messaging.Message<E>> event)
      Description copied from interface: Region
      Send a Mono of event and return a Mono of collected StateMachineEventResults as a list. Events are consumed after returned results are consumed.
      Specified by:
      sendEventCollect in interface Region<S,E>
      Parameters:
      event - the event
      Returns:
      the event results
    • onInit

      protected void onInit() throws Exception
      Description copied from class: LifecycleObjectSupport
      Subclasses may implement this for initialization logic. Called during the InitializingBean phase.
      Overrides:
      onInit in class LifecycleObjectSupport
      Throws:
      Exception - exception
    • setBeanFactory

      public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
      Overrides:
      setBeanFactory in class LifecycleObjectSupport
      Throws:
      org.springframework.beans.BeansException
    • doPreStartReactively

      protected reactor.core.publisher.Mono<Void> doPreStartReactively()
      Description copied from class: LifecycleObjectSupport
      Subclasses may implement this for pre start logic.
      Overrides:
      doPreStartReactively in class LifecycleObjectSupport
      Returns:
      the mono for completion
    • doPostStartReactively

      protected reactor.core.publisher.Mono<Void> doPostStartReactively()
      Description copied from class: LifecycleObjectSupport
      Subclasses may implement this for post start logic.
      Overrides:
      doPostStartReactively in class LifecycleObjectSupport
      Returns:
      the mono for completion
    • doPreStopReactively

      protected reactor.core.publisher.Mono<Void> doPreStopReactively()
      Description copied from class: LifecycleObjectSupport
      Subclasses may implement this for pre stop logic.
      Overrides:
      doPreStopReactively in class LifecycleObjectSupport
      Returns:
      the mono for completion
    • doDestroy

      protected void doDestroy()
      Description copied from class: LifecycleObjectSupport
      Subclasses may implement this for destroy logic.
      Overrides:
      doDestroy in class LifecycleObjectSupport
    • setStateMachineError

      public void setStateMachineError(Exception exception)
      Description copied from interface: StateMachine
      Sets the state machine error.
      Specified by:
      setStateMachineError in interface StateMachine<S,E>
      Parameters:
      exception - the new state machine error
    • hasStateMachineError

      public boolean hasStateMachineError()
      Description copied from interface: StateMachine
      Checks for state machine error.
      Specified by:
      hasStateMachineError in interface StateMachine<S,E>
      Returns:
      true, if error has been set
    • addStateListener

      public void addStateListener(StateMachineListener<S,E> listener)
      Description copied from interface: Region
      Adds the state listener.
      Specified by:
      addStateListener in interface Region<S,E>
      Parameters:
      listener - the listener
    • removeStateListener

      public void removeStateListener(StateMachineListener<S,E> listener)
      Description copied from interface: Region
      Removes the state listener.
      Specified by:
      removeStateListener in interface Region<S,E>
      Parameters:
      listener - the listener
    • isComplete

      public boolean isComplete()
      Description copied from interface: Region
      Checks if region complete. Region is considered to be completed if it has reached its end state and no further event processing is happening.
      Specified by:
      isComplete in interface Region<S,E>
      Returns:
      true, if complete
    • getStates

      public Collection<State<S,E>> getStates()
      Gets the States defined in this machine. Returned collection is an unmodifiable copy because states in a state machine are immutable.
      Specified by:
      getStates in interface Region<S,E>
      Returns:
      immutable copy of existing states
    • getTransitions

      public Collection<Transition<S,E>> getTransitions()
      Description copied from interface: Region
      Gets a Transitions for this region.
      Specified by:
      getTransitions in interface Region<S,E>
      Returns:
      immutable copy of transitions
    • setInitialEnabled

      public void setInitialEnabled(boolean enabled)
      Description copied from interface: StateMachineAccess
      Sets if initial state is enabled when a state machine is using sub states.
      Specified by:
      setInitialEnabled in interface StateMachineAccess<S,E>
      Parameters:
      enabled - the new initial enabled
    • getStateMachineAccessor

      public StateMachineAccessor<S,E> getStateMachineAccessor()
      Description copied from interface: StateMachine
      Gets the state machine accessor.
      Specified by:
      getStateMachineAccessor in interface StateMachine<S,E>
      Returns:
      the state machine accessor
    • setRelay

      public void setRelay(StateMachine<S,E> stateMachine)
      Description copied from interface: StateMachineAccess
      Sets the relay state machine.
      Specified by:
      setRelay in interface StateMachineAccess<S,E>
      Parameters:
      stateMachine - the state machine
    • setParentMachine

      public void setParentMachine(StateMachine<S,E> parentMachine)
      Description copied from interface: StateMachineAccess
      Sets the parent machine.
      Specified by:
      setParentMachine in interface StateMachineAccess<S,E>
      Parameters:
      parentMachine - the state machine
    • setForwardedInitialEvent

      public void setForwardedInitialEvent(org.springframework.messaging.Message<E> message)
      Description copied from interface: StateMachineAccess
      Set initial forwarded event which is used for passing in event and its headers for actions executed when sub state is entered via initial transition.
      Specified by:
      setForwardedInitialEvent in interface StateMachineAccess<S,E>
      Parameters:
      message - the forwarded message
    • setTransitionConflightPolicy

      public void setTransitionConflightPolicy(TransitionConflictPolicy transitionConflictPolicy)
      Sets the transition conflict policy.
      Parameters:
      transitionConflictPolicy - the new transition conflict policy
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • resetStateMachine

      public void resetStateMachine(StateMachineContext<S,E> stateMachineContext)
      Description copied from interface: StateMachineAccess
      Reset state machine.
      Specified by:
      resetStateMachine in interface StateMachineAccess<S,E>
      Parameters:
      stateMachineContext - the state machine context
      See Also:
    • resetStateMachineReactively

      public reactor.core.publisher.Mono<Void> resetStateMachineReactively(StateMachineContext<S,E> stateMachineContext)
      Description copied from interface: ReactiveStateMachineAccess
      Reset state machine reactively.
      Specified by:
      resetStateMachineReactively in interface ReactiveStateMachineAccess<S,E>
      Parameters:
      stateMachineContext - the state machine context
      Returns:
      mono for completion
    • addStateMachineInterceptor

      public void addStateMachineInterceptor(StateMachineInterceptor<S,E> interceptor)
      Description copied from interface: StateMachineAccess
      Adds the state machine interceptor.
      Specified by:
      addStateMachineInterceptor in interface StateMachineAccess<S,E>
      Parameters:
      interceptor - the interceptor
    • addStateMachineMonitor

      public void addStateMachineMonitor(StateMachineMonitor<S,E> monitor)
      Description copied from interface: StateMachineAccess
      Adds the state machine monitor.
      Specified by:
      addStateMachineMonitor in interface StateMachineAccess<S,E>
      Parameters:
      monitor - the monitor
    • getUuid

      public UUID getUuid()
      Description copied from interface: Region
      Gets the region and state machine unique id.
      Specified by:
      getUuid in interface Region<S,E>
      Returns:
      the region and state machine unique id
    • getId

      public String getId()
      Description copied from interface: Region
      Gets the region and state machine id. This identifier is provided for users disposal and can be set from a various ways to build a machines.
      Specified by:
      getId in interface Region<S,E>
      Returns:
      the region and state machine id
    • setId

      public void setId(String id)
      Sets the machine id.
      Parameters:
      id - the new machine id
    • executeTriggerlessTransitions

      protected reactor.core.publisher.Mono<Void> executeTriggerlessTransitions(StateMachine<S,E> stateMachine, StateContext<S,E> stateContext, State<S,E> state)
    • getStateMachineExecutor

      protected StateMachineExecutor<S,E> getStateMachineExecutor()