1 / 7

state transition 状態遷移 有限状態機械の設計と実装

state transition 状態遷移 有限状態機械の設計と実装. わんくま 同盟 episthmh episteme@cppll.jp Microsoft MVP for Visual Developer Visual C ++. 状態遷移ってナニ よ ?. 駐車場でよく見かけるコレ. 状態遷移図 state transition diagram. Pass / Alarm. Coin / Unlock. Close. Open. Pass / Lock. Coin / ThankYou. ここから開始.

clive
Download Presentation

state transition 状態遷移 有限状態機械の設計と実装

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. state transition 状態遷移 有限状態機械の設計と実装 わんくま同盟 episthmhepisteme@cppll.jp Microsoft MVP for Visual Developer Visual C++

  2. 状態遷移ってナニよ? 駐車場でよく見かけるコレ 状態遷移図 state transition diagram Pass /Alarm Coin /Unlock Close Open Pass /Lock Coin /ThankYou ここから開始 State : 状態  Event : 事象  Action : 動作

  3. 状態遷移表 (state transition table / state map) 状態遷移図の表による表現 Action : 動作 → 次の状態(遷移) State : 状態 Event : 事象

  4. 駐車場の遮断機をシミュレートしてみよう private enum State { Close, Open } private State state_ = State.Close; private void buttonPass_Click(object sender, EventArgs e) { switch ( state_ ) { case State.Close: textAction.Text = "コラ!"; break; case State.Open: textAction.Text = "ゲートを閉じます"; state_ = State.Close; break; } textState.Text = state_.ToString(); } 状態の数だけ並べなきゃなんないね…

  5. C++コメント抽出を行う状態遷移 一行 コメント / 7states ×6events = 42combination コメントかも * 開始 \n / コメント の外 ブロックコメント / ” ” \ * ブロック終わり 引用符内 escape (any)

  6. STATE Pattern を使うぞ、と。 抽象base class StateMachine void Coin() {state_ =state_.Coin(); }void Pass(){state_=state_.Pass(); } State State Coin();State Pass(); state_ Stateの数だけ用意する CloseState State Coin() { Unlock(); return OpenState; }State Pass() { Alarm(); return this; } OpenState State Coin() { ThankYou(); return this; }State Pass() { Lock(); return CloseState; }

  7. Next Step… StateMapCompiler • XMLで記述した状態遷移表に基づいて Context, State,そしてStateMachineの各コードを自動生成する。 GateFSM.cs GateFSM.xml namespace Gate { public enumStateCode { Close, Open, Unknown } public enumEventCode { Coin, Pass, Unknown } public interface Context { void Error(StateCode s, EventCode e); boolConfirm(); void Lock(); void Unlock(); void Alarm(); void ThankYou(); } public abstract class State { public abstract string Name { get; } public abstract StateCode Code { get; } internal virtual void Enter_(Context ctx) {} internal virtual void Exit_(Context ctx) {} internal virtual State Coin(Context ctx) { ctx.Error(Code,EventCode.Coin); return this; } internal virtual State Pass(Context ctx) { ctx.Error(Code,EventCode.Pass); return this; } } … } <?xml version='1.0' encoding='shift_jis' ?> <!DOCTYPE fsm SYSTEM 'smc.dtd'> <fsm name='Gate' initial='Close'> <state name='Close' entry='Lock'> <event name='Coin'> <transit state='Open' if='Confirm'/> </event> <event name='Pass'> <execute action='Alarm'/> </event> </state> <state name='Open' entry='Unlock'> <event name='Coin' guard='Confirm'> <execute action='ThankYou'/> </event> <event name='Pass'> <transit state='Close'/> </event> </state> </fsm>

More Related