Package org.zkoss.fsm

Class StateMachine<E,C,IN>

java.lang.Object
org.zkoss.fsm.StateMachine<E,C,IN>

public abstract class StateMachine<E,C,IN> extends Object
A Finite State Machine implementation. This state machine is callback based, which differs from the standard FSM from textbook. Easier to use and faster to develop and debug.
Since:
6.0.0
Author:
simonpai
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected E
     
    protected boolean
     
    protected boolean
     
    protected final Map<E,StateCtx<E,C,IN>>
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a state machine
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    afterStep(IN input, C inputClass, E origin, E destination)
    This method is called after executing a step
    protected void
    beforeStep(IN input, C inputClass, E origin)
    This method is called before executing a step
    protected final void
    doDebug(String message)
    Manually send a debug message
    protected final void
    doReject(IN input)
    Reject a character
    protected abstract C
    getClass(IN input)
    Determines the class for an input character.
    Return the current state
    protected abstract E
    getLandingState(IN input, C inputClass)
    Determines the initial state upon meeting the input character and class
    getState(E token)
    Get the state by token.
    getState(E token, boolean autoCreate)
    Get the state by token.
    protected void
    Called at the constructor of state machine
    boolean
    Return true if the machine is suspended
    boolean
    Return true if the machine is stopped
    protected void
    onDebug(String message)
    This method is call at certain situations when debug mode is on.
    protected void
    onReject(IN input)
    This method is called when the machine rejects an input character
    protected void
    This method is called at constructor and when reseting the machine.
    protected void
    onStart(IN input, C inputClass, E landing)
    This method is called when the machine takes the first character.
    protected void
    onStop(boolean endOfInput)
    This method is called when the machine stops
    removeState(E token)
    Remove the state by token.
    final void
    run(IN input)
    Feed the machine a single character
    final void
    run(Iterator<IN> inputs)
    Feed the machine a stream of characters
    setDebugMode(boolean mode)
    Set debug mode, where onDebug(String) is called at certain timing to assist user develop the state machine
    setState(E token, StateCtx<E,C,IN> state)
    Set the state by token
    final void
    start(IN input)
    Starts the machine with a single input character.
    final void
    start(Iterator<IN> inputs)
    Starts the machine with a stream of input characters.
    protected final void
    Suspend the machine
    final void
    Terminates the machine.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _states

      protected final Map<E,StateCtx<E,C,IN>> _states
    • _current

      protected E _current
    • _run

      protected boolean _run
    • _step

      protected int _step
    • _debug

      protected boolean _debug
  • Constructor Details

    • StateMachine

      public StateMachine()
      Construct a state machine
  • Method Details

    • setDebugMode

      public StateMachine<E,C,IN> setDebugMode(boolean mode)
      Set debug mode, where onDebug(String) is called at certain timing to assist user develop the state machine
    • setState

      public StateCtx<E,C,IN> setState(E token, StateCtx<E,C,IN> state)
      Set the state by token
      Returns:
      the state
    • removeState

      public StateCtx<E,C,IN> removeState(E token)
      Remove the state by token.
      Returns:
      the removed state
    • getState

      public StateCtx<E,C,IN> getState(E token)
      Get the state by token. Automatically creates one if it does not exist.
    • getState

      public StateCtx<E,C,IN> getState(E token, boolean autoCreate)
      Get the state by token.
      Parameters:
      autoCreate - if true, automatically creates one if it does not exist.
    • init

      protected void init()
      Called at the constructor of state machine
    • getLandingState

      protected abstract E getLandingState(IN input, C inputClass)
      Determines the initial state upon meeting the input character and class
    • getClass

      protected abstract C getClass(IN input)
      Determines the class for an input character.
    • onReset

      protected void onReset()
      This method is called at constructor and when reseting the machine.
    • onStart

      protected void onStart(IN input, C inputClass, E landing)
      This method is called when the machine takes the first character.
    • beforeStep

      protected void beforeStep(IN input, C inputClass, E origin)
      This method is called before executing a step
    • afterStep

      protected void afterStep(IN input, C inputClass, E origin, E destination)
      This method is called after executing a step
    • onStop

      protected void onStop(boolean endOfInput)
      This method is called when the machine stops
      Parameters:
      endOfInput - true if the machine stops due to end of input
    • onReject

      protected void onReject(IN input)
      This method is called when the machine rejects an input character
    • onDebug

      protected void onDebug(String message)
      This method is call at certain situations when debug mode is on.
      See Also:
    • run

      public final void run(Iterator<IN> inputs)
      Feed the machine a stream of characters
    • run

      public final void run(IN input)
      Feed the machine a single character
    • start

      public final void start(Iterator<IN> inputs)
      Starts the machine with a stream of input characters.
    • start

      public final void start(IN input)
      Starts the machine with a single input character.
    • terminate

      public final void terminate()
      Terminates the machine.
    • getCurrentState

      public E getCurrentState()
      Return the current state
    • isTerminated

      public boolean isTerminated()
      Return true if the machine is stopped
    • isSuspended

      public boolean isSuspended()
      Return true if the machine is suspended
    • suspend

      protected final void suspend()
      Suspend the machine
    • doReject

      protected final void doReject(IN input)
      Reject a character
    • doDebug

      protected final void doDebug(String message)
      Manually send a debug message
      See Also: