- java.lang.Object
-
- io.github.interacto.fsm.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 Summary
Fields Modifier and Type Field Description protected OutputState<E>currentStateprotected io.reactivex.subjects.PublishSubject<Map.Entry<OutputState<E>,OutputState<E>>>currentStatePublisherprotected FSM<E>currentSubFSMprotected TimeoutTransition<E>currentTimeoutThe current timeout in progress.protected List<E>eventsToProcessThe events still in process.protected Set<FSMHandler>handlersThe handler that want to be notified when the state machine of the interaction changed.protected InitState<E>initStateprotected booleaninnerprotected Loggerloggerprotected booleanstartedGoes with 'startingState'.protected State<E>startingStateBy default an FSM triggers its 'start' event when it leaves its initial state.protected Set<State<E>>statesThe states that compose the finite state machine.
-
Constructor Summary
Constructors Constructor Description FSM()Creates the FSM.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddHandler(FSMHandler handler)Adds an FSM handler.protected voidaddRemaningEventsToProcess(E event)protected voidaddState(InputState<E> state)Adds a state to the state machine.protected voidcheckTimeoutTransition()Checks whether the current state has a timeout transition.io.reactivex.Observable<Map.Entry<OutputState<E>,OutputState<E>>>currentState()protected voidenterStdState(StdState<E> state)voidfullReinit()Reinitialises the FSM.OutputState<E>getCurrentState()Set<State<E>>getStates()booleanisInner()booleanisStarted()voidlog(boolean log)Logs (or not) information about the execution of the FSM.protected voidnotifyHandlerOnCancel()Notifies handler that the interaction is cancelled.protected voidnotifyHandlerOnStart()Notifies handler that the interaction starts.protected voidnotifyHandlerOnStop()Notifies handler that the interaction stops.protected voidnotifyHandlerOnUpdate()Notifies handler that the interaction updates.protected voidonCancelling()Cancels the state machine.voidonStarting()Starts the state machine.protected voidonTerminating()Terminates the state machine.protected voidonTimeout()Jobs to do when a timeout transition is executed.voidonUpdating()Updates the state machine.booleanprocess(E event)Processes the provided event to run the FSM.protected voidprocessRemainingEvents()At the end of the FSM execution, the events still (eg keyPress) in process must be recycled to be reused in the FSM.voidreinit()Reinitialises the FSM.voidremoveHandler(FSMHandler handler)Removes the given FSM handler from this FSM.protected voidsetCurrentState(OutputState<E> state)voidsetInner(boolean inner)States whether the FSM is an inner FSM (ie, whether it is included into another FSM as a sub-FSM transition).protected voidstopCurrentTimeout()Stops the current timeout transition.voiduninstall()Uninstall the FSM.
-
-
-
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.
-
currentState
protected OutputState<E> currentState
-
currentStatePublisher
protected final io.reactivex.subjects.PublishSubject<Map.Entry<OutputState<E>,OutputState<E>>> currentStatePublisher
-
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.
-
-
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.
-
enterStdState
protected void enterStdState(StdState<E> state) throws CancelFSMException
- Throws:
CancelFSMException
-
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 CancelFSMExceptionTerminates 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 CancelFSMExceptionStarts the state machine.- Throws:
CancelFSMException- If the interaction is cancelled by a handler during the starting step.
-
onUpdating
public void onUpdating() throws CancelFSMExceptionUpdates 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. SeefullReinit()for that.
-
fullReinit
public void fullReinit()
Reinitialises the FSM. Compared toreinit()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 CancelFSMExceptionNotifies handler that the interaction starts.- Throws:
CancelFSMException- If the interaction is cancelled by a handler during the starting step.
-
notifyHandlerOnUpdate
protected void notifyHandlerOnUpdate() throws CancelFSMExceptionNotifies handler that the interaction updates.- Throws:
CancelFSMException- If the interaction is cancelled by a handler during the updating step.
-
notifyHandlerOnStop
protected void notifyHandlerOnStop() throws CancelFSMExceptionNotifies 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.
-
-