public class DeterministicStateMachine extends ProcessorAdapter implements StateMachine
SynchronizedStateMachine
Modifier and Type | Field and Description |
---|---|
private State |
currentState
The current state.
|
private java.util.List<Preprocessor> |
preprocessors
The preprocessors of events.
|
private java.util.List<StateListener> |
stateListeners
The listeners.
|
private java.util.Map<State,TransitionMap> |
stateTransitions
The transitions.
|
private java.util.List<TransitionListener> |
transitionListeners
The listeners.
|
logger
INITIAL_STATE_NAME
Constructor and Description |
---|
DeterministicStateMachine(java.lang.String name)
Create the object.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(StateListener listener)
Add a new listener.
|
void |
addListener(TransitionListener listener)
Add a new listener.
|
void |
addPreprocessor(Preprocessor preprocessor)
Add a new preprocessor.
|
void |
addState(State state)
Add a new state.
|
private void |
addStateInternal(State state)
Add a new state to the state machine.
|
void |
addTransition(Transition transition)
Add a new transition.
|
void |
close()
Finish processing of the events and free all allocated resources.
|
boolean |
easterEgg()
Each software product should contain at least one easter egg.
|
State |
getActiveState()
Get the currently active state.
|
java.util.Set<State> |
getActiveStates()
Get the currently active states.
|
java.util.Set<State> |
getStates()
Get all defined states.
|
protected Transition |
getTransition(State state,
Event event)
Get transition of a specified event type defined for a state.
|
java.util.Set<Transition> |
getTransitions()
Get all defined transitions.
|
boolean |
isInFinalState()
Is the active state a final state?
|
(package private) void |
notifyEnter(State previous,
Event event,
State current)
Process all state enter callbacks.
|
(package private) void |
notifyExit(State current,
Event event,
State next)
Process all state exit callbacks.
|
(package private) void |
notifyTransition(Transition transition,
State source,
Event event,
State destination)
Process all transition callbacks.
|
private Event |
preprocessEvent(Event event)
Preprocess event using all registered preprocessors (recursive).
|
Event |
process(Event event)
Process the event.
|
private void |
processCheck(Event event)
Check the input event and the internal state before processing the event.
|
private Event |
processInternal(Transition transition,
Event eventToProcess,
Event matchedEvent)
Real processing of the event.
|
void |
setStartState(State state)
Set the start state.
|
void |
start()
Building of the processor is finished, prepare it to the events
processing.
|
java.lang.String |
toString() |
getName
private final java.util.Map<State,TransitionMap> stateTransitions
private State currentState
private final java.util.List<StateListener> stateListeners
private final java.util.List<TransitionListener> transitionListeners
private final java.util.List<Preprocessor> preprocessors
public DeterministicStateMachine(java.lang.String name)
name
- the name of the state machinepublic void setStartState(State state) throws FsmException
StateMachine
setStartState
in interface StateMachine
state
- the stateFsmException
StateMachine.start()
,
Processor.process(Event)
public void start() throws FsmException
Processor
start
in interface Processor
start
in interface StateMachine
start
in class ProcessorAdapter
FsmException
- if something failsProcessor.close()
public void close()
Processor
Processor.close()
should be
avoided. The behavior is not defined and may cause unpredictable
behavior, e.g. NullPointerException
.close
in interface java.lang.AutoCloseable
close
in interface Processor
close
in interface StateMachine
close
in class ProcessorAdapter
Processor.start()
public void addState(State state) throws FsmException
StateMachine
addState
in interface StateMachine
state
- the stateFsmException
- if something failsprivate void addStateInternal(State state)
state
- the statepublic void addTransition(Transition transition) throws FsmException
StateMachine
addTransition
in interface StateMachine
transition
- the transitionFsmException
- if something failspublic void addPreprocessor(Preprocessor preprocessor)
StateMachine
addPreprocessor
in interface StateMachine
preprocessor
- the preprocessorProcessor.process(Event)
,
StateMachine.start()
,
StateMachine.close()
public void addListener(StateListener listener)
StateMachine
addListener
in interface StateMachine
listener
- the listenerpublic void addListener(TransitionListener listener)
StateMachine
addListener
in interface StateMachine
listener
- the listenervoid notifyEnter(State previous, Event event, State current) throws FsmException
previous
- the previous stateevent
- the eventcurrent
- the current stateFsmException
- if something failsvoid notifyExit(State current, Event event, State next) throws FsmException
current
- the current stateevent
- the eventnext
- the next stateFsmException
- if something failsvoid notifyTransition(Transition transition, State source, Event event, State destination) throws FsmException
transition
- the transitionsource
- the source stateevent
- the eventdestination
- the destination stateFsmException
- if something failspublic State getActiveState()
StateMachine
getActiveState
in interface StateMachine
StateMachine.getActiveStates()
public java.util.Set<State> getActiveStates()
StateMachine
getActiveStates
in interface StateMachine
StateMachine.getActiveState()
public Event process(Event event) throws FsmException
Processor
process
in interface Processor
event
- the input eventFsmException
- if something failsprivate Event preprocessEvent(Event event) throws FsmException
event
- the eventFsmException
- if something failsprivate void processCheck(Event event) throws FsmException
event
- the input eventFsmException
- if the event can't be processed for any reasonprivate Event processInternal(Transition transition, Event eventToProcess, Event matchedEvent) throws FsmException
transition
- the transition that should be processedeventToProcess
- the event that should be processedmatchedEvent
- the event that matched, for logging purposesFsmException
- if something failspublic java.util.Set<State> getStates()
StateMachine
getStates
in interface StateMachine
public java.util.Set<Transition> getTransitions()
StateMachine
getTransitions
in interface StateMachine
protected Transition getTransition(State state, Event event)
state
- the state, it must be defined in the state machineevent
- the eventpublic boolean isInFinalState()
StateMachine
isInFinalState
in interface StateMachine
public java.lang.String toString()
toString
in class java.lang.Object
public boolean easterEgg()
Copyright 2013 Michal Turek, AnotherFSM.