home *** CD-ROM | disk | FTP | other *** search
- package classes.basic.StateMachine
- {
- public class TFStateMachine
- {
-
- public static var SM_STATECHANGE:Number = 1;
-
- public static var SM_EVENTARRIVE:Number = 5;
-
- public static var SM_PROCESS:Number = 4;
-
- public static var SM_EVENT:Number = 2;
-
- public static var SM_ENDSTATE:Number = 3;
-
- public static var SM_INITIALIZE:Number = 0;
-
-
- private var listState:Array;
-
- private var eventHandler:Function;
-
- private var currentState:TFStateMachineState;
-
- private var listTransition:Array;
-
- private var listEvent:Array;
-
- private var bReady:Boolean;
-
- public function TFStateMachine(param1:Function)
- {
- super();
- this.bReady = false;
- this.listState = new Array();
- this.listEvent = new Array();
- this.listTransition = new Array();
- this.eventHandler = param1;
- this.currentState = null;
- }
-
- private function clearList(param1:Array) : *
- {
- var _loc2_:Object = null;
- var _loc3_:* = undefined;
- for(_loc3_ in param1)
- {
- _loc2_ = param1.pop();
- _loc2_.destroy();
- }
- }
-
- public function getCurrentState() : TFStateMachineState
- {
- return currentState;
- }
-
- private function init(param1:TFStateMachineState) : Boolean
- {
- if(this.currentState != null)
- {
- if(this.currentState.getCallBackStop() != null)
- {
- this.currentState.getCallBackStop().call(param1.getParam());
- }
- }
- this.currentState = param1;
- if(this.eventHandler != null)
- {
- this.eventHandler(SM_STATECHANGE,this.currentState);
- if(this.currentState.isEndState())
- {
- this.eventHandler(SM_ENDSTATE,this.currentState);
- }
- }
- if(this.currentState.getCallBackStart() != null)
- {
- this.currentState.getCallBackStart().call(this.currentState.getParam());
- }
- return true;
- }
-
- private function searchTransition(param1:String, param2:String) : TFStateMachineTransition
- {
- var _loc3_:Array = null;
- var _loc4_:String = null;
- var _loc5_:* = undefined;
- _loc3_ = this.listTransition[param1 + "-" + param2];
- if(_loc3_ == null)
- {
- throw new Error("Transition state: " + param1 + " event: " + param2 + " does not exist!");
- }
- var _loc6_:int = 0;
- var _loc7_:* = _loc3_;
- for(_loc5_ in _loc7_)
- {
- _loc4_ = _loc5_;
- }
- return _loc3_[_loc4_];
- }
-
- public function initialize(param1:String) : Boolean
- {
- var _loc2_:TFStateMachineState = null;
- if(this.listTransition.length == 0)
- {
- throw new Error("It does not have any transitions registered!");
- }
- if(!this.existState(param1))
- {
- throw new Error("State " + param1 + " does not exist!");
- }
- _loc2_ = this.listState[param1];
- if(this.eventHandler != null)
- {
- this.eventHandler(SM_INITIALIZE,_loc2_);
- }
- this.init(_loc2_);
- this.bReady = true;
- return true;
- }
-
- private function existEvent(param1:String) : Boolean
- {
- return listEvent[param1] != null;
- }
-
- private function existState(param1:String) : Boolean
- {
- return listState[param1] != null;
- }
-
- private function existTransition(param1:String, param2:String, param3:String) : *
- {
- var _loc4_:Array = null;
- if((_loc4_ = this.listTransition[param1 + "-" + param2]) == null)
- {
- return false;
- }
- return _loc4_[param3] != null;
- }
-
- public function process(param1:Object) : *
- {
- if(!bReady)
- {
- throw new Error("State Machine not initialized!");
- }
- if(this.eventHandler != null)
- {
- this.eventHandler(SM_PROCESS,this.currentState);
- }
- if(this.currentState.getCallBackRun() != null)
- {
- (this.currentState.getCallBackRun() as Function).call(this.currentState.getParam(),param1);
- }
- }
-
- public function createTransition(param1:String, param2:String, param3:String) : *
- {
- var _loc4_:TFStateMachineState = null;
- var _loc5_:TFStateMachineEvent = null;
- var _loc6_:TFStateMachineState = null;
- var _loc7_:TFStateMachineTransition = null;
- var _loc8_:Array = null;
- var _loc9_:TFStateMachineTransition = null;
- if(!this.existState(param1))
- {
- throw new Error("State " + param1 + " does not exist!");
- }
- if(!this.existState(param3))
- {
- throw new Error("State " + param3 + " does not exist!");
- }
- if(!existEvent(param2))
- {
- throw new Error("Event " + param2 + " does not exist!");
- }
- if(existTransition(param1,param2,param3))
- {
- throw new Error("Transition " + param1 + "-" + param2 + "-" + param3 + " already exists!");
- }
- _loc4_ = this.listState[param1];
- _loc5_ = this.listEvent[param2];
- _loc6_ = this.listState[param3];
- _loc9_ = new TFStateMachineTransition(_loc4_,_loc5_,_loc6_);
- if((_loc8_ = this.listTransition[param1 + "-" + param2]) == null)
- {
- _loc8_ = new Array();
- this.listTransition[param1 + "-" + param2] = _loc8_;
- ++this.listTransition.length;
- }
- _loc8_[param3] = _loc9_;
- ++_loc8_.length;
- }
-
- public function createState(param1:String, param2:Boolean, param3:Object, param4:Function, param5:Function, param6:Function) : *
- {
- var _loc7_:TFStateMachineState = null;
- if(this.existState(param1))
- {
- throw new Error("State " + param1 + " already exists!");
- }
- _loc7_ = new TFStateMachineState(param1,param2,param3,param4,param5,param6);
- this.listState[param1] = _loc7_;
- ++this.listState.length;
- }
-
- public function receiveEvent(param1:String) : Boolean
- {
- var _loc2_:TFStateMachineTransition = null;
- if(!bReady)
- {
- throw new Error("State Machine not initialized!");
- }
- if(this.eventHandler != null)
- {
- this.eventHandler(SM_EVENT,this.listEvent[param1]);
- }
- if(this.currentState.isEndState())
- {
- return false;
- }
- if(!existEvent(param1))
- {
- throw new Error("Event " + param1 + " does not exist!");
- }
- if(this.eventHandler != null)
- {
- this.eventHandler(SM_EVENTARRIVE,this.listEvent[param1]);
- }
- _loc2_ = this.searchTransition(this.currentState.getName(),param1);
- return this.init(_loc2_.getNewState());
- }
-
- public function destroy() : *
- {
- this.clearList(this.listState);
- this.clearList(this.listEvent);
- this.clearList(this.listTransition);
- delete global[this];
- }
-
- public function createEvent(param1:String, param2:Object) : *
- {
- var _loc3_:TFStateMachineEvent = null;
- if(existEvent(param1))
- {
- throw new Error("Event " + param1 + " already exists!");
- }
- _loc3_ = new TFStateMachineEvent(param1,param2);
- this.listEvent[param1] = _loc3_;
- ++this.listEvent.length;
- }
- }
- }
-