Nice TWiki > Doc > CodeExamples > FiniteStateMachineMultiMethodExample (r1.4) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the MultiMethodExamples
To compile:
   nicec --sourcepath=.. -a benderfsm.jar benderfsm
To run:
   java -jar benderfsm.jar
*/

private enum FsmState {Idle, On, Active}

class BenderFSM {
   public void event(String e) = this.transition(state,e);
   
   private FsmState state = Idle;
   String last = "Unknown";


   private void transition(FsmState s, String e){    last = "Unknown"; }

// Transition  State    Event        Action 
  
   transition( Idle,   "TurnOn"   ){ state = On;     last = "TurnOn"; }
   transition( On,     "TurnOff"  ){ state = Idle;   last = "TurnOff"; }
   transition( On,     "Activate" ){ state = Active; last = "Activate"; }
   transition( Active, "Walk"     ){                 last = "Walk"; }
   transition( Active, "Run"      ){                 last = "Run"; }
   transition( Active, "Talk"     ){                 last = "Talk"; }
   transition( Active, "Stop"     ){ state = On;     last = "Stop"; }
}


class SuperBenderFSM extends BenderFSM {

// Transition  State    Event        Action

   transition( Active, "Skip"     ){                 last = "Skip"; }
   transition( Active, "Jump"     ){                 last = "Jump"; }
}


void main(String[] args){ 

   let String[] events = [ 
      "TurnOn", "Activate",  
      "Talk", "Run", 
      "Skip", "Jump",       // Only SuperBenderFSM 
      "Stop", "TurnOff"
      ];

   let bender = new BenderFSM();
   let superB = new SuperBenderFSM();
           
   for (e : events){ 
      println("\n" + "Bender" +"\t"+"\t"+ "Super Bender");
      println("State: " + bender.state + "\t" + superB.state);

      bender.event(e); superB.event(e);

      println("Event: " + bender.last + "\t" + superB.last);
      println("State: " + bender.state + "\t" + superB.state);
   }
}


/* Notes - language
Compare with Java and C++ at "Finite State Machine Tutorial"
http://www.generation5.org/content/2003/FSM_Tutorial.asp

   private enum FsmState {Idle, On, Active} 
Simple enumeration of possible Bender FSM states

// Transition  State    Event        Action
   transition( Idle,   "TurnOn"   ){ state = On;     last = "TurnOn"; }

Multi methods allow a declarative encoding of FSM transitions as simple 
message sends. In BenderFSM, Nice will dispatch the transition method by 
BenderFSM instance, and by FsmState instance, and by String value, so we
simply provide an implementation of transition for each legal transition.

   class SuperBenderFSM extends BenderFSM {
Simply subclass the Bender Finite State Machine to create a FSM with
additional transitions.

*/

-- IsaacGouy - 29 Jan 2004 展示架 生物反应器 笔记本电脑 成人用品 翻译 鲜花 移民 翻译公司 租房 游戏 游戏下载 摸屏 壁纸 服务器 爱滋病 办公自动化 出国 短信 干燥设备 交友 聊天 门禁 内衣 升降机 视频 写真 仪器仪表 移动存储 音乐 自考 算命 惠普 诺基亚 体育彩票 网页制作 前列腺 索尼 宠物 大连 电力 福利彩票 个人主页 广告公司 国际贸易 会计 计算机等级考试 交通 宽带 免费空间 农业 企业文化 人才 人才网 设计 手机报价 图书 网络 网络游戏 物业管理 西门子 小灵通 幼儿教育 幼儿园 娱乐 注册会计师 财务 财务管理 出国留学 电脑培训 多媒体 翻译软件 飞机票 购物 管理 婚纱摄影 激光 计算机培训 空间 律师 律师事务所 贸易 美容美发 模型 纳米 商标 数据库 通讯 网络安全 项目管理 雅思 英语培训 园林 招标 中医

Topic FiniteStateMachineMultiMethodExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More }
Revision r1.4 - 25 Jan 2005 - 06:17 GMT - LiYan
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.

Doc.FiniteStateMachineMultiMethodExample moved from Doc.BenderFsmMultiMethodExample on 06 Feb 2004 - 15:40 by IsaacGouy - put it back