Package ai.statemachine
Class StateMachine
java.lang.Object
ai.statemachine.StateMachine
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds a new state to the machine.boolean
Add a new transition.boolean
canTransitionTo
(String state) Check if there is any known state that can transition to the given state.boolean
doTransition
(String toState) Attempts to transition to the given state.boolean
Check if a state is known by its name.boolean
hasTransition
(String state) Check if there is any known transition outgoing from the given state.boolean
hasTransition
(String fromState, String toState) Check if there is transition from a given state to a given state by their names.boolean
startMachine
(String startState) Attempts to start the state machine.void
update
(float dt) Update loop method.boolean
validate()
Validate whether the state machine does not contain unreachable or deadlock states.
-
Constructor Details
-
StateMachine
public StateMachine()Create a statemachine with no states or transitions.
-
-
Method Details
-
getCurrentStateName
- See Also:
-
currentState
-
getCurrentState
- Returns:
- the current
State
of the machine
-
hasTransition
Check if there is transition from a given state to a given state by their names.- Parameters:
fromState
- the name of the state to starttoState
- the name of the state to potentially transition to- Returns:
- true if and only if there is at least one know transition
-
canTransitionTo
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
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
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
Adds a new state to the machine.- Parameters:
name
- the name of the new statestate
- the new state- Returns:
- false if the state could not be added or a state with identical name already existed
-
addTransition
Add a new transition.- Returns:
- false, if the states are unknown
- See Also:
-
Transition(String, Function)
-
startMachine
Attempts to start the state machine. This is required to set the starting/current state of the machine and enable the update loop. The methoddoTransition(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
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:
-