gen_fsm

Van Oudheusden voudheus@REDACTED
Fri Jun 22 14:36:42 CEST 2001


Hello again,


It's firday afternoon and I'm getting a bit impatient with the code (see
attachment). It's a simple instantiation of the 'gen_fsm' behaviour.
Could somebody please tell me what I am doing wrong?


Besides that, I have a more fundamental question:  If I am not mistaken,
the implementation of gen_fsm implies that one or more processes are
spawned.  My question is whether it is a good idea to keep this issue
transparent from the user who is using the behaviour.  Doesn't this
influence the performance of his implementation?  How efficient is a
behavriour with respect to inheritance in an onject oriented program in
which the same design pattern is implemented?


any comments are welcome,
KVO.
-------------- next part --------------
-module(fsmkvo).
-behaviour(gen_fsm).

-import(lists, [append/2]).

%% --------------------------------------------------------------------
%% CALLBACK functions:
%% --------------------------------------------------------------------
%% Initialisation routine:
init(Args) ->
	{ok, sleeping, [1]}.

%% Termination routine:
terminate(Reason,State,Data) -> 
	ok.

%% Stop the FSM no matter what state it is in:
handle_event(stopit, AnyState, TheStateData) ->
	{stop, stopInvocation, []};

%% Print the State Data of the Current State of the FSM:
handle_event(printState, AnyState, TheStateData) ->
	io:format(" Currently in state  ~w~n", [AnyState]),
	io:format(" with state data: ~w~n", [TheStateData]),
	io:format(" ~n"),
	{next_state, AnyState, TheStateData}.

%% The STATE MACHINE:

sleeping({eat}, Data) ->
	%% ... actions for change from sleep to eat ...
	{next_state, eating, lists:append(Data,[2])}.

eating({work}, Data) ->
	{next_state, working, lists:append(Data,[3])}.

working({sleep}, Data) ->
	{next_state, sleeping, lists:append(Data,[4])};

working({eat}, Data) ->
	{next_state, eating, lists:append(Data,[5])}.
%% --------------------------------------------------------------------
%% CALLBACK end.
%% --------------------------------------------------------------------

%% ----------------------------------------------------------------------------------------------------------------

%% --------------------------------------------------------------------
%% interface routines:
%% --------------------------------------------------------------------
start() ->
	gen_fsm:start({local,fsmkvo}, fsmkvo, [], []).

letsEat() ->
	gen_fsm:send_event(fsmkvo, {eat}).

letsWork() ->
	gen_fsm:send_event(fsmkvo, {work}).

letsSleep() ->
	gen_fsm:send_event(fsmkvo, {sleep}).

printState() ->
	gen_fsm:send_all_state_event(fsmkvo, printState).

stop() ->
	gen_fsm:send_all_state_event(fsmkvo, stopit).
%% --------------------------------------------------------------------
%% interface end.
%% --------------------------------------------------------------------





More information about the erlang-questions mailing list