public class StateTransitionMap extends Object implements TransitionIndex<State,Message,StateTransition>
For example, the transition [ A -- m --> B ] would be stored in a map as represented in the table below :
Key | Transitions -------------------- A | [ m -> B ] B | []It is important to note that target states will be also stored as key in the transitions map. In case a new transition from A is added , [ A --- n ---> C ] , the transitions would be updated as :
Key | Transitions -------------------- A | [ m -> B, n -> C ] B | [] C | []In case a transition is added from this state using the same message [ A --- m ---> C, the transition map would be updated as follows :
Key | Transitions -------------------- A | [ m -> C, n -> C ] B | [] C | []After this operation State B is not reachable
| Constructor and Description |
|---|
StateTransitionMap()
Creates the state machine.
|
StateTransitionMap(@NonNull Map<State,Map<Message,State>> transitionMap)
Creates the state machine with a given transition map.
|
StateTransitionMap(@NonNull Map<State,Map<Message,State>> transitionMap,
@NonNull TransitionValidator<State,Message,StateTransition> transitionValidator)
Creates the state machine with a custom transition map
and validator.
|
StateTransitionMap(@NonNull TransitionValidator<State,Message,StateTransition> transitionValidator)
Creates the state machine with a custom transition validator.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(StateTransition transition)
Adds a new Transition to the statemachine.
|
void |
addAll(@NonNull Collection<StateTransition> transitions)
Adds all supplied transitions to the index.
|
void |
clear()
Clear all states and transitions from the map
|
boolean |
contains(@NonNull State state)
Checks the presence of the given state in
the index.
|
boolean |
contains(@NonNull StateTransition transition)
Checks the presence of the given transition in
the index.
|
Optional<State> |
find(String stateName)
Finds the given state by its name.
|
Collection<StateTransition> |
getAllTransitions()
Retrieve all the transitions stored in the transition map.
|
Optional<State> |
getFirst()
Retrieves the first state in the transition map
|
Optional<State> |
getNext(@NonNull State source,
@NonNull Message m)
Retrieves the next state in the transition from
source state upon message m reception
|
Optional<State> |
getPrevious(State source,
Message m)
Retrieves the previous node upon reception of the
message on the given source node.
|
Collection<StateTransition> |
getTransitions(State state)
Retrieves the transitions from the given state
or throws a
NullStateException
if not found. |
Collection<StateTransition> |
getTransitions(String stateName)
Retrieves the transitions from the given state
or throws a
NullStateException
if not found. |
Map<State,Map<Message,State>> |
getTransitionsAsMap()
Retrieves the transitions as a map
|
List<State> |
prune()
Remove orphan states from the transition map
i.e.
|
void |
remove(@NonNull State state)
Removes the current state from the machine
including defined transitions.
|
void |
remove(StateTransition transition)
Removes an existing Transition from the state machine.
|
void |
remove(@NonNull String stateName)
Removes the current state from the machine
including defined transitions.
|
void |
removeAllTransitions()
Clear all transitions from the map
|
int |
size()
Retrieves the number of states in the transition map
|
public StateTransitionMap()
public StateTransitionMap(@NonNull
@NonNull Map<State,Map<Message,State>> transitionMap)
transitionMap - the transition mappublic StateTransitionMap(@NonNull
@NonNull TransitionValidator<State,Message,StateTransition> transitionValidator)
transitionValidator - transition validatorpublic StateTransitionMap(@NonNull
@NonNull Map<State,Map<Message,State>> transitionMap,
@NonNull
@NonNull TransitionValidator<State,Message,StateTransition> transitionValidator)
transitionMap - the transition maptransitionValidator - transition validatorpublic void add(StateTransition transition)
add in interface TransitionIndex<State,Message,StateTransition>transition - the transition to addpublic void remove(StateTransition transition)
remove in interface TransitionIndex<State,Message,StateTransition>transition - the transition to removepublic void remove(@NonNull
@NonNull State state)
remove in interface TransitionIndex<State,Message,StateTransition>state - the state to removepublic void remove(@NonNull
@NonNull String stateName)
remove in interface TransitionIndex<State,Message,StateTransition>stateName - the state to removepublic void removeAllTransitions()
removeAllTransitions in interface TransitionIndex<State,Message,StateTransition>public void clear()
clear in interface TransitionIndex<State,Message,StateTransition>public Optional<State> getNext(@NonNull @NonNull State source, @NonNull @NonNull Message m)
getNext in interface TransitionIndex<State,Message,StateTransition>source - the origin statem - the received messagepublic Optional<State> getPrevious(State source, Message m)
getPrevious in interface TransitionIndex<State,Message,StateTransition>source - the origin nodem - the messagepublic Optional<State> find(String stateName)
find in interface TransitionIndex<State,Message,StateTransition>stateName - the name of the statepublic boolean contains(@NonNull
@NonNull State state)
contains in interface TransitionIndex<State,Message,StateTransition>state - the state to findpublic boolean contains(@NonNull
@NonNull StateTransition transition)
contains in interface TransitionIndex<State,Message,StateTransition>transition - the transition to findpublic Optional<State> getFirst()
getFirst in interface TransitionIndex<State,Message,StateTransition>public int size()
size in interface TransitionIndex<State,Message,StateTransition>public List<State> prune()
prune in interface TransitionIndex<State,Message,StateTransition>public Collection<StateTransition> getTransitions(String stateName)
NullStateException
if not found.getTransitions in interface TransitionIndex<State,Message,StateTransition>stateName - the state's namepublic Collection<StateTransition> getTransitions(State state)
NullStateException
if not found.getTransitions in interface TransitionIndex<State,Message,StateTransition>state - the statepublic Map<State,Map<Message,State>> getTransitionsAsMap()
getTransitionsAsMap in interface TransitionIndex<State,Message,StateTransition>public Collection<StateTransition> getAllTransitions()
getAllTransitions in interface TransitionIndex<State,Message,StateTransition>public void addAll(@NonNull
@NonNull Collection<StateTransition> transitions)
addAll in interface TransitionIndex<State,Message,StateTransition>transitions - the transitions to addCopyright © 2019 state-machine. All rights reserved.