gen_fsm之门禁管理
生活随笔
收集整理的這篇文章主要介紹了
gen_fsm之门禁管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
昨天寫了看了一下erlang gen_sfm 模式下一些應用,加上之前 Thomas 給我布置的任務
Erlang Programming一書中也有涉及,不過我草草看了一下
對于下面的狀態改變寫法的自己寫的感覺還可以,呵呵...
任務描述如下:
實現一個門鎖狀態機,使用gen_fsm模式
初始狀態為open,可以接受的event為lock,轉為close狀態
close狀態,接受的event為unlock,轉為open狀態
在以上兩個狀態,如果有其它事件,不可崩潰,需要保持狀態不變
下面是代碼:
door_lock.erl 1-module(door_lock).
2-export([start/0, stop/0, unlock_or_lock/1]).
3-export([locked/2, open/2]).
4-export([init/1, handle_event/3, code_change/4, handle_sync_event/4, handle_info/3, terminate/3]).
5
6-include_lib("eunit/include/eunit.hrl").
7-behaviour(gen_fsm).
?
door_lock.erl 1%%% 外部接口2
3% 開始
4% start_link(FsmName, Module, Args, Options)
5% 利用 init 初始化狀態
6start() ->
7 gen_fsm:start_link({local, ?MODULE}, ?MODULE, unlock, []).
8
9% 結束
10stop() ->
11 gen_fsm:send_all_state_event(?MODULE, stop). % 提交到Module:handle_event/3
12
13% 外部改變狀態
14% State::lock | unlock -> 正則匹配
15unlock_or_lock(State) ->
16 case State == lock orelse State == unlock of
17 true ->
18 gen_fsm:send_event(?MODULE, State);
19 false ->
20 io:format('Input error,Door not Change!')
21 end.
?
door_lock.erl 1%%% 回調函數
2
3init(State) ->
4 {ok, locked, State}.
5
6% 鎖住 door
7locked(Event, StateData) ->
8 % 判斷發送過來的 Event 是不是和之前的狀態一樣
9 case Event /= StateData of
10 true ->
11 NextStateName = open, % 下一個狀態(調用)
12 NewStateData = Event, % 下一個狀態數據
13 outputMsg(Event);
14 %io:format("~p Success!~n", [Event]);
15 % {next_state, open, lock};
16 false ->
17 NextStateName = locked,
18 NewStateData = StateData,
19 outputError(StateData)
20 %io:format("Now the door is ~p~n Not thing to change~n",[StateData])
21 % {next_state, locked, StateData}
22 end,
23
24 {next_state, NextStateName, NewStateData}.
25
26% 打開 door
27open(Event, StateData) ->
28 case Event /= StateData of
29 true ->
30 NextStateName = locked,
31 NewStateData = Event,
32 outputMsg(Event);
33 %io:format("~p Success!~n", [Event]);
34 % {next_state, locked, StateData} ;
35 false ->
36 NextStateName = open,
37 NewStateData = StateData,
38 outputError(StateData)
39 %io:format("Now the door is ~p~n Not thing to change~n",[StateData])
40 %io:format("~p~n", [StateData]),
41 % {next_state, open, lock}
42 end,
43
44 {next_state, NextStateName, NewStateData}.
45
46% 信息的輸出
47outputError(Msg) ->
48 io:format("Now the door is ~p~nNot thing to change~n",[Msg]).
49
50outputMsg(Msg) ->
51 io:format("~p Success!~n", [Msg]).
52
53% 匹配退出
54handle_event(stop, _StateName, StateData) ->
55 {stop, normal, StateData}.
56
57% 鎖定事件
58% {reply,Reply,NextStateName,NewStateData}
59handle_sync_event(_Event, _From, StateName, StateData) ->
60 {reply,ok, StateName, StateData}.
61
62% 接收跟高級的信息
63% than a synchronous or asynchronous event (or a system message).
64handle_info(_Info, StateName, StateData) ->
65 {next_state,StateName,StateData}.
66
67% 熱替換
68code_change(_OldVsn, StateName, StateData, _Extra) ->
69 {ok, StateName, StateData}.
70
71% 退出調用
72terminate(_Reason, _StateName, _StateData) ->
73 ok.
74
?
?
?
?
轉載于:https://www.cnblogs.com/clinglin/archive/2011/01/24/1942940.html
總結
以上是生活随笔為你收集整理的gen_fsm之门禁管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# Replace函数与JS repl
- 下一篇: 使用ActionScript实现滤镜效果