org.squirrelframework.foundation.fsm.impl
Class AbstractStateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

java.lang.Object
  extended by org.squirrelframework.foundation.component.impl.AbstractSubject
      extended by org.squirrelframework.foundation.fsm.impl.AbstractStateMachine<T,S,E,C>
Type Parameters:
T - state machine type
S - state type
E - event type
C - context type
All Implemented Interfaces:
Observable, StateMachine<T,S,E,C>, Visitable
Direct Known Subclasses:
AbstractUntypedStateMachine

public abstract class AbstractStateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
extends AbstractSubject
implements StateMachine<T,S,E,C>

The Abstract state machine provide several extension ability to cover different extension granularity.

  1. Method beforeStateExit/afterStateEntry is used to add custom logic on all kinds of state exit/entry.
  2. Method exit[stateName]/entry[stateName] is extension method which is used to add custom logic on specific state.
  3. Method beforeTransitionBegin/afterTransitionComplete is used to add custom logic on all kinds of transition accepted all conditions.
  4. Method transitFrom[fromStateName]To[toStateName]On[eventName] is used to add custom logic on specific transition accepted all conditions.
  5. Method transitFromAnyTo[toStateName]On[eventName] is used to add custom logic on any state transfer to specific target state on specific event happens, so as the transitFrom[fromStateName]ToAnyOn[eventName], transitFrom[fromState]To[ToStateName], and on[EventName].

Author:
Henry.He

Nested Class Summary
static class AbstractStateMachine.AbstractStateMachineEvent<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.AbstractTransitionEvent<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.StartEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.StateMachineExceptionEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.TerminateEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.TransitionBeginEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.TransitionCompleteEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.TransitionDeclinedEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.TransitionEndEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
static class AbstractStateMachine.TransitionExceptionEventImpl<T extends StateMachine<T,S,E,C>,S,E,C>
           
 
Nested classes/interfaces inherited from interface org.squirrelframework.foundation.fsm.StateMachine
StateMachine.StartEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.StartListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.StateMachineEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.StateMachineExceptionEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.StateMachineExceptionListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.StateMachineListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TerminateEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TerminateListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionBeginEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionBeginListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionCompleteEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionCompleteListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionDeclinedEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionDeclinedListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionEndEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionEndListener<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionExceptionEvent<T extends StateMachine<T,S,E,C>,S,E,C>, StateMachine.TransitionExceptionListener<T extends StateMachine<T,S,E,C>,S,E,C>
 
Field Summary
protected  Lock readLock
           
protected  ReentrantReadWriteLock rwLock
           
protected  Lock writeLock
           
 
Constructor Summary
AbstractStateMachine()
           
 
Method Summary
 void accept(Visitor visitor)
          Accepts a #Visitor.
 void addDeclarativeListener(Object listenerMethodProvider)
           
 void addExecActionListener(ActionExecutionService.BeforeExecActionListener<T,S,E,C> listener)
           
 void addStartListener(StateMachine.StartListener<T,S,E,C> listener)
           
 void addStateMachineExceptionListener(StateMachine.StateMachineExceptionListener<T,S,E,C> listener)
           
 void addStateMachineListener(StateMachine.StateMachineListener<T,S,E,C> listener)
           
 void addTerminateListener(StateMachine.TerminateListener<T,S,E,C> listener)
           
 void addTransitionBeginListener(StateMachine.TransitionBeginListener<T,S,E,C> listener)
           
 void addTransitionCompleteListener(StateMachine.TransitionCompleteListener<T,S,E,C> listener)
           
 void addTransitionDeclinedListener(StateMachine.TransitionDeclinedListener<T,S,E,C> listener)
           
 void addTransitionEndListener(StateMachine.TransitionEndListener<T,S,E,C> listener)
           
 void addTransitionExceptionListener(StateMachine.TransitionExceptionListener<T,S,E,C> listener)
           
protected  void afterActionInvoked(S fromState, S toState, E event, C context)
           
protected  void afterTransitionCausedException(S fromState, S toState, E event, C context)
           
protected  void afterTransitionCompleted(S fromState, S toState, E event, C context)
           
protected  void afterTransitionDeclined(S fromState, E event, C context)
           
protected  void afterTransitionEnd(S fromState, S toState, E event, C context)
           
protected  void beforeActionInvoked(S fromState, S toState, E event, C context)
           
protected  void beforeTransitionBegin(S fromState, E event, C context)
           
 boolean canAccept(E event)
           
protected  void cleanQueuedEvents()
          Clean all queued events
 StateMachineData.Reader<T,S,E,C> dumpSavedData()
          Dump current state machine data.
 String exportXMLDefinition(boolean beautifyXml)
           
 void fire(E event)
          Fire event
 void fire(E event, C context)
          Fires the specified event
 void fireImmediate(E event)
          Fire event immediately
 void fireImmediate(E event, C context)
          Fires event with context immediately, if current state machine is busy, the next processing event is this event.
 Collection<ImmutableState<T,S,E,C>> getAllRawStates()
           
 Collection<S> getAllStates()
           
 ImmutableState<T,S,E,C> getCurrentRawState()
           
 S getCurrentState()
           
 String getDescription()
           
 int getExecutorListenerSize()
          Internal use only
 String getIdentifier()
           
 ImmutableState<T,S,E,C> getInitialRawState()
           
 S getInitialState()
           
 S getLastActiveChildStateOf(S parentStateId)
           
 TransitionException getLastException()
           
 ImmutableState<T,S,E,C> getLastRawState()
           
 S getLastState()
           
 ImmutableState<T,S,E,C> getRawStateFrom(S stateId)
           
 StateMachineStatus getStatus()
           
 List<S> getSubStatesOn(S parentStateId)
           
 T getThis()
           
 boolean isContextSensitive()
           
protected  boolean isIdle()
           
 boolean loadSavedData(StateMachineData.Reader<T,S,E,C> savedData)
          Load saved data for current state machine.
protected  void prepare()
           
 void removeDeclarativeListener(Object listenerMethodProvider)
           
 void removeExecActionListener(ActionExecutionService.BeforeExecActionListener<T,S,E,C> listener)
           
 void removeStartListener(StateMachine.StartListener<T,S,E,C> listener)
           
 void removeStateMachineExceptionListener(StateMachine.StateMachineExceptionListener<T,S,E,C> listener)
           
 void removeStateMachineListener(StateMachine.StateMachineListener<T,S,E,C> listener)
           
 void removeTerminateListener(StateMachine.TerminateListener<T,S,E,C> listener)
           
 void removeTransitionBeginListener(StateMachine.TransitionBeginListener<T,S,E,C> listener)
           
 void removeTransitionCompleteListener(StateMachine.TransitionCompleteListener<T,S,E,C> listener)
           
 void removeTransitionDecleindListener(StateMachine.TransitionDeclinedListener<T,S,E,C> listener)
           
 void removeTransitionEndListener(StateMachine.TransitionEndListener<T,S,E,C> listener)
           
 void removeTransitionExceptionListener(StateMachine.TransitionExceptionListener<T,S,E,C> listener)
           
protected  void setLastException(TransitionException lastException)
           
protected  void setStatus(StateMachineStatus status)
           
 void start()
          Start state machine without context
 void start(C context)
          Start state machine under external context
 void terminate()
          Terminate state machine without context
 void terminate(C context)
          Terminate state machine under external context
 S test(E event)
          Test event
 S test(E event, C context)
          Test transition result under circumstance
 Class<C> typeOfContext()
           
 Class<E> typeOfEvent()
           
 Class<S> typeOfState()
           
 
Methods inherited from class org.squirrelframework.foundation.component.impl.AbstractSubject
addListener, addListener, fireEvent, getListenerSize, isNotifiable, removeAllListeners, removeListener, removeListener, removeListener, removeListener, setNotifiable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.squirrelframework.foundation.component.Observable
addListener, addListener, fireEvent, getListenerSize, isNotifiable, removeAllListeners, removeListener, removeListener, removeListener, removeListener, setNotifiable
 

Field Detail

rwLock

protected final ReentrantReadWriteLock rwLock

writeLock

protected final Lock writeLock

readLock

protected final Lock readLock
Constructor Detail

AbstractStateMachine

public AbstractStateMachine()
Method Detail

prepare

protected void prepare()

fire

public void fire(E event,
                 C context)
Description copied from interface: StateMachine
Fires the specified event

Specified by:
fire in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
event - the event
context - external context

fireImmediate

public void fireImmediate(E event,
                          C context)
Description copied from interface: StateMachine
Fires event with context immediately, if current state machine is busy, the next processing event is this event.

Specified by:
fireImmediate in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
event - the event
context - external context

fire

public void fire(E event)
Description copied from interface: StateMachine
Fire event

Specified by:
fire in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

fireImmediate

public void fireImmediate(E event)
Description copied from interface: StateMachine
Fire event immediately

Specified by:
fireImmediate in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

cleanQueuedEvents

protected void cleanQueuedEvents()
Clean all queued events


test

public S test(E event,
              C context)
Description copied from interface: StateMachine
Test transition result under circumstance

Specified by:
test in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
event - test event
context - text context
Returns:
test transition result

test

public S test(E event)
Description copied from interface: StateMachine
Test event

Specified by:
test in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

canAccept

public boolean canAccept(E event)
Specified by:
canAccept in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
event - test event
Returns:
true is passed in event is acceptable otherwise return false

isIdle

protected boolean isIdle()

afterTransitionCausedException

protected void afterTransitionCausedException(S fromState,
                                              S toState,
                                              E event,
                                              C context)

beforeTransitionBegin

protected void beforeTransitionBegin(S fromState,
                                     E event,
                                     C context)

afterTransitionCompleted

protected void afterTransitionCompleted(S fromState,
                                        S toState,
                                        E event,
                                        C context)

afterTransitionEnd

protected void afterTransitionEnd(S fromState,
                                  S toState,
                                  E event,
                                  C context)

afterTransitionDeclined

protected void afterTransitionDeclined(S fromState,
                                       E event,
                                       C context)

beforeActionInvoked

protected void beforeActionInvoked(S fromState,
                                   S toState,
                                   E event,
                                   C context)

afterActionInvoked

protected void afterActionInvoked(S fromState,
                                  S toState,
                                  E event,
                                  C context)

getCurrentRawState

public ImmutableState<T,S,E,C> getCurrentRawState()
Specified by:
getCurrentRawState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
current raw state of state machine

getLastRawState

public ImmutableState<T,S,E,C> getLastRawState()
Specified by:
getLastRawState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
last active raw state of state machine

getInitialRawState

public ImmutableState<T,S,E,C> getInitialRawState()
Specified by:
getInitialRawState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
initial raw state of state machine

getRawStateFrom

public ImmutableState<T,S,E,C> getRawStateFrom(S stateId)
Specified by:
getRawStateFrom in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getAllRawStates

public Collection<ImmutableState<T,S,E,C>> getAllRawStates()
Specified by:
getAllRawStates in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getAllStates

public Collection<S> getAllStates()
Specified by:
getAllStates in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getCurrentState

public S getCurrentState()
Specified by:
getCurrentState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
current state id of state machine

getLastState

public S getLastState()
Specified by:
getLastState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
last active state id of state machine

getInitialState

public S getInitialState()
Specified by:
getInitialState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
id of state machine initial state

start

public void start(C context)
Description copied from interface: StateMachine
Start state machine under external context

Specified by:
start in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
context - external context

start

public void start()
Description copied from interface: StateMachine
Start state machine without context

Specified by:
start in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getStatus

public StateMachineStatus getStatus()
Specified by:
getStatus in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
current status of state machine

setStatus

protected void setStatus(StateMachineStatus status)

getLastActiveChildStateOf

public S getLastActiveChildStateOf(S parentStateId)
Specified by:
getLastActiveChildStateOf in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
parentStateId - id of parent state
Returns:
last active child state of the parent state

getSubStatesOn

public List<S> getSubStatesOn(S parentStateId)
Specified by:
getSubStatesOn in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
sub state of parallel state

terminate

public void terminate(C context)
Description copied from interface: StateMachine
Terminate state machine under external context

Specified by:
terminate in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
context - external context

terminate

public void terminate()
Description copied from interface: StateMachine
Terminate state machine without context

Specified by:
terminate in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getThis

public T getThis()
Specified by:
getThis in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
type-safe state machine instance

accept

public void accept(Visitor visitor)
Description copied from interface: Visitable
Accepts a #Visitor.

Specified by:
accept in interface Visitable
Parameters:
visitor - the visitor.

dumpSavedData

public StateMachineData.Reader<T,S,E,C> dumpSavedData()
Description copied from interface: StateMachine
Dump current state machine data. This operation can only be done when state machine status is StateMachineStatus.IDLE, otherwise null will be returned.

Specified by:
dumpSavedData in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
dumped state machine data reader

loadSavedData

public boolean loadSavedData(StateMachineData.Reader<T,S,E,C> savedData)
Description copied from interface: StateMachine
Load saved data for current state machine. The operation can only be done when state machine status is StateMachineStatus.INITIALIZED or StateMachineStatus.TERMINATED.

Specified by:
loadSavedData in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Parameters:
savedData - provided saved data
Returns:
true if load saved data success otherwise false

isContextSensitive

public boolean isContextSensitive()
Specified by:
isContextSensitive in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>
Returns:
whether state machine is context sensitive

typeOfContext

public Class<C> typeOfContext()
Specified by:
typeOfContext in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

typeOfEvent

public Class<E> typeOfEvent()
Specified by:
typeOfEvent in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

typeOfState

public Class<S> typeOfState()
Specified by:
typeOfState in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getLastException

public TransitionException getLastException()
Specified by:
getLastException in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

setLastException

protected void setLastException(TransitionException lastException)

addDeclarativeListener

public void addDeclarativeListener(Object listenerMethodProvider)
Specified by:
addDeclarativeListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeDeclarativeListener

public void removeDeclarativeListener(Object listenerMethodProvider)
Specified by:
removeDeclarativeListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getExecutorListenerSize

public int getExecutorListenerSize()
Internal use only

Returns:
ActionExecutionService

getIdentifier

public String getIdentifier()
Specified by:
getIdentifier in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

getDescription

public String getDescription()
Specified by:
getDescription in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

exportXMLDefinition

public String exportXMLDefinition(boolean beautifyXml)
Specified by:
exportXMLDefinition in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addStateMachineListener

public void addStateMachineListener(StateMachine.StateMachineListener<T,S,E,C> listener)
Specified by:
addStateMachineListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeStateMachineListener

public void removeStateMachineListener(StateMachine.StateMachineListener<T,S,E,C> listener)
Specified by:
removeStateMachineListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addStartListener

public void addStartListener(StateMachine.StartListener<T,S,E,C> listener)
Specified by:
addStartListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeStartListener

public void removeStartListener(StateMachine.StartListener<T,S,E,C> listener)
Specified by:
removeStartListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addTerminateListener

public void addTerminateListener(StateMachine.TerminateListener<T,S,E,C> listener)
Specified by:
addTerminateListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeTerminateListener

public void removeTerminateListener(StateMachine.TerminateListener<T,S,E,C> listener)
Specified by:
removeTerminateListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addStateMachineExceptionListener

public void addStateMachineExceptionListener(StateMachine.StateMachineExceptionListener<T,S,E,C> listener)
Specified by:
addStateMachineExceptionListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeStateMachineExceptionListener

public void removeStateMachineExceptionListener(StateMachine.StateMachineExceptionListener<T,S,E,C> listener)
Specified by:
removeStateMachineExceptionListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addTransitionBeginListener

public void addTransitionBeginListener(StateMachine.TransitionBeginListener<T,S,E,C> listener)
Specified by:
addTransitionBeginListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeTransitionBeginListener

public void removeTransitionBeginListener(StateMachine.TransitionBeginListener<T,S,E,C> listener)
Specified by:
removeTransitionBeginListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addTransitionCompleteListener

public void addTransitionCompleteListener(StateMachine.TransitionCompleteListener<T,S,E,C> listener)
Specified by:
addTransitionCompleteListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeTransitionCompleteListener

public void removeTransitionCompleteListener(StateMachine.TransitionCompleteListener<T,S,E,C> listener)
Specified by:
removeTransitionCompleteListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addTransitionExceptionListener

public void addTransitionExceptionListener(StateMachine.TransitionExceptionListener<T,S,E,C> listener)
Specified by:
addTransitionExceptionListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeTransitionExceptionListener

public void removeTransitionExceptionListener(StateMachine.TransitionExceptionListener<T,S,E,C> listener)
Specified by:
removeTransitionExceptionListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addTransitionDeclinedListener

public void addTransitionDeclinedListener(StateMachine.TransitionDeclinedListener<T,S,E,C> listener)
Specified by:
addTransitionDeclinedListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeTransitionDecleindListener

public void removeTransitionDecleindListener(StateMachine.TransitionDeclinedListener<T,S,E,C> listener)
Specified by:
removeTransitionDecleindListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addTransitionEndListener

public void addTransitionEndListener(StateMachine.TransitionEndListener<T,S,E,C> listener)
Specified by:
addTransitionEndListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeTransitionEndListener

public void removeTransitionEndListener(StateMachine.TransitionEndListener<T,S,E,C> listener)
Specified by:
removeTransitionEndListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

addExecActionListener

public void addExecActionListener(ActionExecutionService.BeforeExecActionListener<T,S,E,C> listener)
Specified by:
addExecActionListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>

removeExecActionListener

public void removeExecActionListener(ActionExecutionService.BeforeExecActionListener<T,S,E,C> listener)
Specified by:
removeExecActionListener in interface StateMachine<T extends StateMachine<T,S,E,C>,S,E,C>


Copyright © 2013-2014. All Rights Reserved.