Package pygar.state
Class StateMachine<T>
java.lang.Object
java.util.Observable
pygar.state.StateMachine<T>
- Type Parameters:
T
- the type of the context object that accompanies each event
- Direct Known Subclasses:
NegotiationGroupStateMachine
,OrderedStateMachine
,PmessageStateMachine
@ThreadSafe public class StateMachine<T> extends java.util.Observable
This class implements simple state machines that respond to events.
The events are named and contain context information of type T.
State machines have predefined transitions. Each transition is
characterized by a start state, an end state, the action that provokes
the transition and an event handle that performs any associated action.
Several sub-types of transitions are supported. If an event has no particular starting state, then the transition may be specified with a null starting state. Such transitions may have a defined list of starting states that should not respond to the event.
A transition may have a null end state. This transition is interpreted as a loop-back - a state that performs the eventHandler and returns to the same state.
Each statemachine has two optional slots that support a simple persistence scheme that can be used to checkpoint and rollback the system. The first slot contains a persistence id. The second contains function object for logging transitions. (optional slots not currently implemented)
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
StateMachine.Context
Class Context defines the data for observers of this state machine.class
StateMachine.TransitionPt1
This inner class contains a lookup table for eventsclass
StateMachine.TransitionPt2
transition descriptionclass
StateMachine.TransitionPt3
alternate transition description for transitions that have many starting states. -
Constructor Summary
Constructors Constructor Description StateMachine(java.lang.String start)
-
Method Summary
Modifier and Type Method Description protected StateMachine.Context
_getStateContext()
Internal routine called from a super class to create a copy of context as it is known to this class.void
acceptContextFreeEvent(java.lang.String event)
Receive an event from another object with no context specified.void
acceptEvent(java.lang.String event, T context)
Receive an event from another object in the context of a third context object of type T.void
addTransition(java.lang.String startState, java.lang.String eventName, java.lang.String endState, EventListener<T> action)
Add a transition to the state machine where the transition has a definite start state.void
addTransition(java.lang.String eventName, java.lang.String endState, EventListener<T> action, java.lang.String[] insensitive)
Add a transition to the state machine where the transition has multiple start states.java.lang.String
getState()
Return the current state of the state machine.static void
main(java.lang.String[] args)
void
notifyObservers()
Function to perform the duties of an observable n.b.void
notifyObservers(java.lang.Object o)
void
setState(java.lang.String state)
Assign a state to the state machine.
-
Constructor Details
-
StateMachine
public StateMachine(java.lang.String start)
-
-
Method Details
-
notifyObservers
public void notifyObservers()Function to perform the duties of an observable n.b. all the callers of this procedure should synchronize on "this"- Overrides:
notifyObservers
in classjava.util.Observable
-
notifyObservers
public void notifyObservers(java.lang.Object o)- Overrides:
notifyObservers
in classjava.util.Observable
-
_getStateContext
Internal routine called from a super class to create a copy of context as it is known to this class. The message field is left blank for completion later.- Returns:
- Context
-
setState
public void setState(java.lang.String state)Assign a state to the state machine. Normally, however, you should just let that events induce state transitions.- Parameters:
state
-
-
getState
public java.lang.String getState()Return the current state of the state machine.- Returns:
- state
-
addTransition
public void addTransition(java.lang.String eventName, java.lang.String endState, EventListener<T> action, java.lang.String[] insensitive)Add a transition to the state machine where the transition has multiple start states.- Parameters:
eventName
- name of the eventendState
- state after the event or null if this is a loop-backaction
- EventListener that provides actions for the transitioninsensitive
- a list of start state names that do not respond to this event
-
addTransition
public void addTransition(java.lang.String startState, java.lang.String eventName, java.lang.String endState, EventListener<T> action)Add a transition to the state machine where the transition has a definite start state.- Parameters:
startState
- initial state of transition or null for an event operating on all states.eventName
- name of the eventendState
- state after the event or null if this is a loop-backaction
- EventListener that provides actions for the transition
-
acceptContextFreeEvent
public void acceptContextFreeEvent(java.lang.String event) throws MessageSystemException, java.lang.InterruptedException, java.lang.reflect.InvocationTargetExceptionReceive an event from another object with no context specified. N.b. the success of the operation depends on the handlers for the transition invoked by the event. If the handler, requires a context, then the operation fails. Context free events are usually associated with user input events, not enterprise messages.- Parameters:
event
-- Throws:
MessageSystemException
java.lang.InterruptedException
java.lang.reflect.InvocationTargetException
-
acceptEvent
Receive an event from another object in the context of a third context object of type T.- Parameters:
event
-context
-- Throws:
java.lang.Exception
-
main
public static void main(java.lang.String[] args)- Parameters:
args
-
-