Class StateMachine

java.lang.Object
ai.statemachine.StateMachine

public class StateMachine extends Object
A flexible implementation of a statemachine. This class can represent a deterministic or non-deterministic statemachine, however the actual transitions will never be random, therefore all statemachines are predictable in their transitions. It involves an unlimited number of states and transitions between them, which can be automated by adding conditions to them.
Since:
13.07.2021
Author:
Juyas
  • Constructor Details

    • StateMachine

      public StateMachine()
      Create a statemachine with no states or transitions.
  • Method Details

    • getCurrentStateName

      public String getCurrentStateName()
      See Also:
      • currentState
    • getCurrentState

      public State getCurrentState()
      Returns:
      the current State of the machine
    • hasTransition

      public boolean hasTransition(String fromState, String toState)
      Check if there is transition from a given state to a given state by their names.
      Parameters:
      fromState - the name of the state to start
      toState - the name of the state to potentially transition to
      Returns:
      true if and only if there is at least one know transition
    • canTransitionTo

      public boolean canTransitionTo(String state)
      Check if there is any known state that can transition to the given state.
      Parameters:
      state - the name of the state to potentially transition to
      Returns:
      true if and only if there is any state that has a transition to the given one
    • hasTransition

      public boolean hasTransition(String state)
      Check if there is any known transition outgoing from the given state.
      Parameters:
      state - the name of the state to transition from
      Returns:
      true if and only if there is any transition starting at the given state
    • hasState

      public boolean hasState(String name)
      Check if a state is known by its name.
      Parameters:
      name - the name of the state
      Returns:
      true if and only if there is a state by this name
    • addState

      public boolean addState(String name, State state)
      Adds a new state to the machine.
      Parameters:
      name - the name of the new state
      state - the new state
      Returns:
      false if the state could not be added or a state with identical name already existed
    • addTransition

      public boolean addTransition(String fromState, String toState, Function<State,Boolean> condition)
      Add a new transition.
      Returns:
      false, if the states are unknown
      See Also:
      • Transition(String, Function)
    • startMachine

      public boolean startMachine(String startState)
      Attempts to start the state machine. This is required to set the starting/current state of the machine and enable the update loop. The method doTransition(String) will fail as well, before this method is called.
      Parameters:
      startState - the state to start from
      Returns:
      false, if the the statemachine could not be started, because - startState is null - startState is not known to the machine - currentState is already not null, so machine already started
    • doTransition

      public boolean doTransition(String toState)
      Attempts to transition to the given state.
      Parameters:
      toState - the state to transition to
      Returns:
      false, if and only if the given stte is unknown or null
      Throws:
      NullPointerException - if the machine has not beeing started yet
    • validate

      public boolean validate()
      Validate whether the state machine does not contain unreachable or deadlock states.
      Returns:
      false if and only if there are states, that do not transition or are orphaned
    • update

      public void update(float dt)
      Update loop method. Will do nothing, if the machine is not started.
      Parameters:
      dt - the delta time
      See Also: