Class FSM<E>

  • Type Parameters:
    E - The type of events the FSM processes.
    Direct Known Subclasses:
    ConcurrentFSM

    public class FSM<E>
    extends Object
    A finite state machine that defines the behavior of a user interaction.
    • Field Detail

      • logger

        protected Logger logger
      • inner

        protected boolean inner
      • startingState

        protected State<E> startingState
        By default an FSM triggers its 'start' event when it leaves its initial state. In some cases, this is not the case. For example, a double-click interaction is an FSM that must trigger its start event when the FSM reaches... its terminal state. Similarly, a DnD must trigger its start event on the first move, not on the first press. The goal of this attribute is to identify the state of the FSM that must trigger the start event. By default, this attribute is set with the initial state of the FSM.
      • started

        protected boolean started
        Goes with 'startingState'. It permits to know whether the FSM has started, ie whether the 'starting state' has been reached.
      • initState

        protected final InitState<E> initState
      • states

        protected final Set<State<E>> states
        The states that compose the finite state machine.
      • handlers

        protected final Set<FSMHandler> handlers
        The handler that want to be notified when the state machine of the interaction changed.
      • eventsToProcess

        protected final List<E> eventsToProcess
        The events still in process. For example when the user press key ctrl and scroll one time using the wheel of the mouse, the interaction scrolling is finished but the event keyPressed 'ctrl' is still in process. At the end of the interaction, these events are re-introduced into the state machine of the interaction for processing.
      • currentTimeout

        protected TimeoutTransition<E> currentTimeout
        The current timeout in progress.
      • currentSubFSM

        protected FSM<E> currentSubFSM
    • Constructor Detail

      • FSM

        public FSM()
        Creates the FSM.
    • Method Detail

      • getCurrentState

        public OutputState<E> getCurrentState()
        Returns:
        The current state of FSM during its execution.
      • currentState

        public io.reactivex.Observable<Map.Entry<OutputState<E>,​OutputState<E>>> currentState()
        Returns:
        An observable value for observing the current state of FSM during its execution.
      • setInner

        public void setInner​(boolean inner)
        States whether the FSM is an inner FSM (ie, whether it is included into another FSM as a sub-FSM transition).
        Parameters:
        inner - True: this FSM will be considered as an inner FSM.
      • isInner

        public boolean isInner()
        Returns:
        True: this FSM is an inner FSM.
      • process

        public boolean process​(E event)
        Processes the provided event to run the FSM.
        Parameters:
        event - The event to process.
        Returns:
        True: the FSM correctly processed the event.
      • isStarted

        public boolean isStarted()
        Returns:
        True: The FSM started.
      • setCurrentState

        protected void setCurrentState​(OutputState<E> state)
      • processRemainingEvents

        protected void processRemainingEvents()
        At the end of the FSM execution, the events still (eg keyPress) in process must be recycled to be reused in the FSM.
      • addRemaningEventsToProcess

        protected void addRemaningEventsToProcess​(E event)
      • onTerminating

        protected void onTerminating()
                              throws CancelFSMException
        Terminates the state machine.
        Throws:
        CancelFSMException - If the interaction is cancelled by a handler during the stopping step.
      • onCancelling

        protected void onCancelling()
        Cancels the state machine.
      • onStarting

        public void onStarting()
                        throws CancelFSMException
        Starts the state machine.
        Throws:
        CancelFSMException - If the interaction is cancelled by a handler during the starting step.
      • onUpdating

        public void onUpdating()
                        throws CancelFSMException
        Updates the state machine.
        Throws:
        CancelFSMException - If the interaction is cancelled by a handler during the updating step.
      • addState

        protected void addState​(InputState<E> state)
        Adds a state to the state machine.
        Parameters:
        state - The state to add. Must not be null.
      • log

        public void log​(boolean log)
        Logs (or not) information about the execution of the FSM.
        Parameters:
        log - True: logging activated.
      • reinit

        public void reinit()
        Reinitialises the FSM. Remaining events to process are however not clear. See fullReinit() for that.
      • fullReinit

        public void fullReinit()
        Reinitialises the FSM. Compared to reinit() this method flushes the remaining events to process.
      • onTimeout

        protected void onTimeout()
        Jobs to do when a timeout transition is executed. Because the timeout transition is based on a separated thread, the job done by this method must be executed in the UI thread. UI Platforms must override this method to do that.
      • stopCurrentTimeout

        protected void stopCurrentTimeout()
        Stops the current timeout transition.
      • checkTimeoutTransition

        protected void checkTimeoutTransition()
        Checks whether the current state has a timeout transition. If it is the case, the timeout transition is launched.
      • addHandler

        public void addHandler​(FSMHandler handler)
        Adds an FSM handler.
        Parameters:
        handler - The handler to add.
      • removeHandler

        public void removeHandler​(FSMHandler handler)
        Removes the given FSM handler from this FSM.
        Parameters:
        handler - The handler to remove.
      • notifyHandlerOnStart

        protected void notifyHandlerOnStart()
                                     throws CancelFSMException
        Notifies handler that the interaction starts.
        Throws:
        CancelFSMException - If the interaction is cancelled by a handler during the starting step.
      • notifyHandlerOnUpdate

        protected void notifyHandlerOnUpdate()
                                      throws CancelFSMException
        Notifies handler that the interaction updates.
        Throws:
        CancelFSMException - If the interaction is cancelled by a handler during the updating step.
      • notifyHandlerOnStop

        protected void notifyHandlerOnStop()
                                    throws CancelFSMException
        Notifies handler that the interaction stops.
        Throws:
        CancelFSMException - If the interaction is cancelled by a handler during the stopping step.
      • notifyHandlerOnCancel

        protected void notifyHandlerOnCancel()
        Notifies handler that the interaction is cancelled.
      • getStates

        public Set<State<E>> getStates()
        Returns:
        The set of the states that compose the FSM. This returns a unmodifiable set.
      • uninstall

        public void uninstall()
        Uninstall the FSM. Useful for flushing memory. The FSM must not be used after that.