Packagecom.yworks.support
Classpublic class StateMachine
InheritanceStateMachine Inheritance flash.events.EventDispatcher

A state machine implementation that can be used to manage complex state and transitions.

The machine is built of States and Transitions. The machine potentially changes its state in response to events.



Public Properties
 PropertyDefined By
  currentState : State
Gets or sets the current state the machine is in.
StateMachine
  startState : State
[read-only] Gets the start state.
StateMachine
  states : Array
[read-only] Returns all states in this machine.
StateMachine
  transitions : Array
[read-only] Returns all transitions in this machine.
StateMachine
Public Methods
 MethodDefined By
  
StateMachine(transitionDoneHandler:Function = null, stateEnteredHandler:Function = null, startState:State = null)
Creates an empty machine using the provided event listeners and a given start state.
StateMachine
  
createState(stateEnteredHandler:Function = null):State
Creates a new state given a listener for the stateEntered event.
StateMachine
  
createTransition(source:State, target:State, eventRecognizer:Function = null, transitionDoneListener:Function = null):Transition
Creates a conditional transition between two given states using a given transitionDone listener.
StateMachine
  
findTransition(srcState:State, targetState:State):Transition
Finds a transistion that connects two given states.
StateMachine
  
performTransition(transition:Transition):void
Executes a given transition programmatically, regardless whether this is currently allowed.
StateMachine
  
removeState(state:State):void
Removes a state from this machine.
StateMachine
  
removeTransition(transition:Transition):void
Removes a transition from this state machine.
StateMachine
  
reset():void
Resets this machine so that the current state is the startState.
StateMachine
  
run(evt:Event):void
Runs this machine using the given event source and event argument to determine which transition to take from the currentState.
StateMachine
Protected Methods
 MethodDefined By
  
onStateChange(newState:State, oldState:State):void
Called when the current state changes.
StateMachine
  
onTransition(transition:Transition):void
Called when a transition is being traversed.
StateMachine
Events
 Event Summary Defined By
  An event that will be triggerd if a state has been created.StateMachine
  Dispatched when a state has been entered during a run call.StateMachine
  Dispatched when a state has been exited during a run call.StateMachine
  An event that will be triggerd if a state has been removed.StateMachine
  Dispatched when a transition has been created.StateMachine
  Dispatched when the statemachine has traversed a Transition during a run call.StateMachine
  Dispatched when a transition has been removed.StateMachine
Property Detail
currentStateproperty
currentState:State

Gets or sets the current state the machine is in.


Implementation
    public function get currentState():State
    public function set currentState(value:State):void
startStateproperty 
startState:State  [read-only]

Gets the start state.


Implementation
    public function get startState():State
statesproperty 
states:Array  [read-only]

Returns all states in this machine.


Implementation
    public function get states():Array
transitionsproperty 
transitions:Array  [read-only]

Returns all transitions in this machine.


Implementation
    public function get transitions():Array
Constructor Detail
StateMachine()Constructor
public function StateMachine(transitionDoneHandler:Function = null, stateEnteredHandler:Function = null, startState:State = null)

Creates an empty machine using the provided event listeners and a given start state.

Parameters
transitionDoneHandler:Function (default = null) — an event listener for the transitionDone event.
 
stateEnteredHandler:Function (default = null) — an event listener for the stateEntered event.
 
startState:State (default = null) — The state to use as the start state.
Method Detail
createState()method
public function createState(stateEnteredHandler:Function = null):State

Creates a new state given a listener for the stateEntered event.

Parameters

stateEnteredHandler:Function (default = null)

Returns
State — The new State instance.
createTransition()method 
public function createTransition(source:State, target:State, eventRecognizer:Function = null, transitionDoneListener:Function = null):Transition

Creates a conditional transition between two given states using a given transitionDone listener.

Parameters

source:State — The source state.
 
target:State — The target state.
 
eventRecognizer:Function (default = null) — An event recognizer that determines whether a given event triggers this transition.
 
transitionDoneListener:Function (default = null) — Event listener for the transitionDone event. The listener has to take a TransitionEvent as an argument.

Returns
Transition — The newly created transition
findTransition()method 
public function findTransition(srcState:State, targetState:State):Transition

Finds a transistion that connects two given states.

Parameters

srcState:State — The source state to connect.
 
targetState:State — The target state to connect.

Returns
Transition — A transition which connects the two states, null if no transition is found.
onStateChange()method 
protected function onStateChange(newState:State, oldState:State):void

Called when the current state changes.

Parameters

newState:State
 
oldState:State

onTransition()method 
protected function onTransition(transition:Transition):void

Called when a transition is being traversed.

Parameters

transition:Transition

performTransition()method 
public function performTransition(transition:Transition):void

Executes a given transition programmatically, regardless whether this is currently allowed.

This will set the current state to the transition's target state.

Parameters

transition:Transition — The transition to traverse.

removeState()method 
public function removeState(state:State):void

Removes a state from this machine.

Parameters

state:State — A state to remove. This may not be the startState

removeTransition()method 
public function removeTransition(transition:Transition):void

Removes a transition from this state machine.

Parameters

transition:Transition — The transition to remove.

reset()method 
public function reset():void

Resets this machine so that the current state is the startState.

run()method 
public function run(evt:Event):void

Runs this machine using the given event source and event argument to determine which transition to take from the currentState.

The first enabled Transition that will be triggered by the given event will be traversed. If this method is called reentrantly, the request will be queued and performed after the current request has been finished.

Parameters

evt:Event — The event that caused this machine to run.

Event Detail
stateCreated Event
Event Object Type: com.yworks.support.StateEvent
StateEvent.type property = com.yworks.support.StateEvent.STATE_CREATED

An event that will be triggerd if a state has been created.

stateEntered Event  
Event Object Type: com.yworks.support.StateChangeEvent
StateChangeEvent.type property = com.yworks.support.StateChangeEvent.STATE_ENTERED

Dispatched when a state has been entered during a run call.

stateExited Event  
Event Object Type: com.yworks.support.StateChangeEvent
StateChangeEvent.type property = com.yworks.support.StateChangeEvent.STATE_EXITED

Dispatched when a state has been exited during a run call.

stateRemoved Event  
Event Object Type: com.yworks.support.StateEvent
StateEvent.type property = com.yworks.support.StateEvent.STATE_REMOVED

An event that will be triggerd if a state has been removed.

transitionCreated Event  
Event Object Type: com.yworks.support.TransitionEvent
TransitionEvent.type property = com.yworks.support.TransitionEvent.TRANSITION_CREATED

Dispatched when a transition has been created.

transitionDone Event  
Event Object Type: com.yworks.support.TransitionEvent
TransitionEvent.type property = com.yworks.support.TransitionEvent.TRANSITION_DONE

Dispatched when the statemachine has traversed a Transition during a run call.

transitionRemoved Event  
Event Object Type: com.yworks.support.TransitionEvent
TransitionEvent.type property = com.yworks.support.TransitionEvent.TRANSITION_REMOVED

Dispatched when a transition has been removed.