Class TrcStateMachine<T>

java.lang.Object
trclib.robotcore.TrcStateMachine<T>
Type Parameters:
T - specifies the State enum type that list all possible states.

public class TrcStateMachine<T> extends Object
This class implements an event driven state machine. The caller can add multiple events for the state machine to monitor. If one or more events are signaled, the state machine will automatically advance to the specified next state.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private T
     
    private boolean
     
    private final ArrayList<TrcEvent>
     
    private boolean
     
    private double
     
    private final String
     
    private T
     
    private boolean
     
    private boolean
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    TrcStateMachine(String instanceName)
    Constructor: Creates an instance of the state machine with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This method adds an event to the event list to be monitored.
    This method checks whether the state machine is ready.
    private void
    This method clears the signaled state of all the events in the list.
    This method returns the next state of the state machine.
    This method returns the current state of the state machine.
    boolean
    This method checks if the state machine is enabled.
    boolean
    This method checks if the state machine is in ready mode.
    boolean
    This method checks if timeout has happened on waiting for event(s).
    void
    setState(T state)
    This method sets the current state of the state machine.
    void
    start(T state)
    This method starts the state machine with the given starting state and puts it in ready mode.
    void
    This method stops the state machine by disabling it.
    This method returns the instance name.
    void
    waitForEvents(T nextState)
    This method puts the state machine into not ready mode and starts monitoring the events in the list.
    void
    waitForEvents(T nextState, boolean waitForAllEvents)
    This method puts the state machine into not ready mode and starts monitoring the events in the list.
    void
    waitForEvents(T nextState, boolean clearEvents, boolean waitForAllEvents)
    This method puts the state machine into not ready mode and starts monitoring the events in the list.
    void
    waitForEvents(T nextState, boolean clearEvents, boolean waitForAllEvents, double timeout)
    This method puts the state machine into not ready mode and starts monitoring the events in the list.
    void
    waitForEvents(T nextState, boolean waitForAllEvents, double timeout)
    This method puts the state machine into not ready mode and starts monitoring the events in the list.
    void
    waitForEvents(T nextState, double timeout)
    This method puts the state machine into not ready mode and starts monitoring the events in the list.
    void
    waitForSingleEvent(TrcEvent event, T nextState)
    This method puts the state machine into not ready mode and starts monitoring a single event.
    void
    waitForSingleEvent(TrcEvent event, T nextState, boolean clearEvent, double timeout)
    This method puts the state machine into not ready mode and starts monitoring a single event.
    void
    waitForSingleEvent(TrcEvent event, T nextState, double timeout)
    This method puts the state machine into not ready mode and starts monitoring a single event.

    Methods inherited from class java.lang.Object

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

    • instanceName

      private final String instanceName
    • eventList

      private final ArrayList<TrcEvent> eventList
    • currState

      private T currState
    • nextState

      private T nextState
    • enabled

      private boolean enabled
    • ready

      private boolean ready
    • expired

      private boolean expired
    • expiredTime

      private double expiredTime
    • waitForAllEvents

      private boolean waitForAllEvents
  • Constructor Details

    • TrcStateMachine

      public TrcStateMachine(String instanceName)
      Constructor: Creates an instance of the state machine with the given name.
      Parameters:
      instanceName - specifies the instance name of the state machine.
  • Method Details

    • toString

      public String toString()
      This method returns the instance name.
      Overrides:
      toString in class Object
      Returns:
      instance name.
    • start

      public void start(T state)
      This method starts the state machine with the given starting state and puts it in ready mode.
      Parameters:
      state - specifies the starting state.
    • stop

      public void stop()
      This method stops the state machine by disabling it.
    • getState

      public T getState()
      This method returns the current state of the state machine.
      Returns:
      current state of the state machine.
    • getNextState

      public T getNextState()
      This method returns the next state of the state machine.
      Returns:
      next state of the state machine.
    • checkReadyAndGetState

      public T checkReadyAndGetState()
      This method checks whether the state machine is ready. If so, it returns the current state. If the state machine is not ready, it returns null.
      Returns:
      current state of the state machine, null if state machine is not ready.
    • setState

      public void setState(T state)
      This method sets the current state of the state machine.
      Parameters:
      state - specifies the state to set the state machine to.
    • isEnabled

      public boolean isEnabled()
      This method checks if the state machine is enabled.
      Returns:
      true if state machine is enabled, false otherwise.
    • isReady

      public boolean isReady()
      This method checks if the state machine is in ready mode. If not, it will enumerate all the events it is monitoring and make sure if any or all of them are signaled as the condition for putting the state machine back in ready mode.
      Returns:
      true if the state machine is in ready mode, false otherwise.
    • isTimedout

      public boolean isTimedout()
      This method checks if timeout has happened on waiting for event(s).
      Returns:
      true if a timeout was set and expired, false otherwise.
    • addEvent

      public void addEvent(TrcEvent event)
      This method adds an event to the event list to be monitored.
      Parameters:
      event - specifies the vent to be added to the list.
    • waitForEvents

      public void waitForEvents(T nextState, boolean clearEvents, boolean waitForAllEvents, double timeout)
      This method puts the state machine into not ready mode and starts monitoring the events in the list. If waitForAllEvents is false and any event on the list is signaled or waitForAllEvents is true and all events are signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.
      Parameters:
      nextState - specifies the next state when the state machine becomes ready.
      clearEvents - specifies true to clear all events.
      waitForAllEvents - specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.
      timeout - specifies a timeout value. A zero value means there is no timeout.
    • waitForEvents

      public void waitForEvents(T nextState, boolean clearEvents, boolean waitForAllEvents)
      This method puts the state machine into not ready mode and starts monitoring the events in the list. If waitForAllEvents is false and any event on the list is signaled or waitForAllEvents is true and all events are signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state.
      Parameters:
      nextState - specifies the next state when the state machine becomes ready.
      clearEvents - specifies true to clear all events.
      waitForAllEvents - specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.
    • waitForEvents

      public void waitForEvents(T nextState, boolean waitForAllEvents, double timeout)
      This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.
      Parameters:
      nextState - specifies the next state when the state machine becomes ready.
      waitForAllEvents - specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.
      timeout - specifies a timeout value. A zero value means there is no timeout.
    • waitForEvents

      public void waitForEvents(T nextState, boolean waitForAllEvents)
      This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.
      Parameters:
      nextState - specifies the next state when the state machine becomes ready.
      waitForAllEvents - specifies true if all events must be signaled for the state machine to go ready. If false, any signaled event will cause the state machine to go ready.
    • waitForEvents

      public void waitForEvents(T nextState, double timeout)
      This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event(s) have not been signaled.
      Parameters:
      nextState - specifies the next state when the state machine becomes ready.
      timeout - specifies a timeout value. A zero value means there is no timeout.
    • waitForEvents

      public void waitForEvents(T nextState)
      This method puts the state machine into not ready mode and starts monitoring the events in the list. If any event on the list is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state.
      Parameters:
      nextState - specifies the next state when the state machine becomes ready.
    • waitForSingleEvent

      public void waitForSingleEvent(TrcEvent event, T nextState, boolean clearEvent, double timeout)
      This method puts the state machine into not ready mode and starts monitoring a single event. If the event is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event have not been signaled.
      Parameters:
      event - specifies the event to wait for.
      nextState - specifies the next state when the state machine becomes ready.
      clearEvent - specifies true to clear the event.
      timeout - specifies a timeout value. A zero value means there is no timeout.
    • waitForSingleEvent

      public void waitForSingleEvent(TrcEvent event, T nextState, double timeout)
      This method puts the state machine into not ready mode and starts monitoring a single event. If the event is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event have not been signaled.
      Parameters:
      event - specifies the event to wait for.
      nextState - specifies the next state when the state machine becomes ready.
      timeout - specifies a timeout value. A zero value means there is no timeout.
    • waitForSingleEvent

      public void waitForSingleEvent(TrcEvent event, T nextState)
      This method puts the state machine into not ready mode and starts monitoring a single event. If the event is signaled, the state machine will be put back to ready mode and it will automatically advance to the given next state. If timeout is non-zero, the state machine will be put back to ready mode after timeout has expired even though the required event have not been signaled.
      Parameters:
      event - specifies the event to wait for.
      nextState - specifies the next state when the state machine becomes ready.
    • clearAllEvents

      private void clearAllEvents()
      This method clears the signaled state of all the events in the list.