Big state machines
Vance Shipley
vances@REDACTED
Tue Apr 19 19:41:49 CEST 2005
On Tue, Apr 19, 2005 at 01:39:43PM +0200, Ulf Wiger (AL/EAB) wrote:
}
} OK, not exactly what you requested, but here's a quick hack of
} gen_fsm that supports selective receive in the form of a receive
} vector (gen_fsm.erl and test.erl attached).
Nice.
I was also working on hacking gen_fsm to handle a receive vector. I
started by building a test module the way I would want to. The module
below is what I had in mind. I extended the return from init/1 and the
state handler callbacks to include two extra values; Receive & Save.
e.g. {next_state, State, StateData, Timeout, Receive, Save}
Receive = any | tuple()
Save = none | any | tuple()
So far I was only supporting atom() events and used the is_element()
method of your previous contribution. I changed the main loop in
gen_fsm to handle the extra arguments and made the default values
any and none. One difference is that you can now have gen_fsm consume
messages you are not interested in instead of doing it explicitly in
the state handler callback.
-module(sdl_fsm).
-export([init/1, s/2]).
-behaviour(gen_fsm).
init(_) ->
{ok, s, [], 0, {a,b}, none};
%% ___
%% (_s_)
%% |
%% +--+--+
%% __|_ __|_
%% >_a_| >_b_|
%% _|_ _|_
%% (_x_) (_y_)
%%
s(a, S) ->
{next_state, x, [], 0, {a,b}, any};
s(b, S) ->
{next_state, y, [], 0, {a,b}, {c,d}}.
%% ___
%% (_x_)
%% |
%% +-----+------+
%% __|_ __|_ __|_
%% >_a_| >_b_| /_*_/
%% _|_ _|_
%% (_x_) (_y_)
%%
x(a, S) ->
{next_state, x, [], 0, {a,b}, any};
x(b, S) ->
{next_state, y, [], 0, {a,b}, {c,d}}.
%% ___
%% (_y_)
%% |
%% +-----+------+-----+
%% __|_ __|_ __|_ __|_
%% >_a_| >_b_| /_c_/ /_d_/
%% _|_ _|_
%% (_x_) (_y_)
%%
y(a, S) ->
{next_state, x, [], 0, {a,b}, any};
y(b, S) ->
{next_state, y, [], 0, {a,b}, {c,d}}.
I hadn't yet contemplated how to handle real life events. I had been
thinking that ultimately what would be required some sort of match
spec format for the vectors and whatever (parse transforms?) else in
gen_fsm to handle it. Your approach to mandate the events take some
certain form may well be a perfectly reasonable way to go.
-Vance
More information about the erlang-questions
mailing list