From 0eddc557de9b0a996ab42aefd66f3229a06f6675 Mon Sep 17 00:00:00 2001 From: Raimo Niskanen Date: Thu, 17 Mar 2016 15:41:26 +0100 Subject: Do documentation improvements from Fred Hebert --- lib/stdlib/doc/src/gen_statem.xml | 66 ++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/lib/stdlib/doc/src/gen_statem.xml b/lib/stdlib/doc/src/gen_statem.xml index 4708fc1301..90d2326954 100644 --- a/lib/stdlib/doc/src/gen_statem.xml +++ b/lib/stdlib/doc/src/gen_statem.xml @@ -35,10 +35,10 @@ A behaviour module for implementing a state machine. Two callback modes are supported. One for a finite state machine like gen_fsm - that require the state to be an atom and use that state as + that requires the state to be an atom and use that state as the name of the callback function for a particular state, - and one without restriction on the state that use the same - callback function for all states. + and one without restriction on the state data type + that uses the same callback function for all states.

A generic state machine process (gen_statem) implemented @@ -96,7 +96,7 @@ erlang:'!' -----> Module:StateName/3

When callback_mode - is state_functions the state has to be an atom and + is state_functions, the state has to be an atom and is used as the state function name. See Module:StateName/3 @@ -127,8 +127,8 @@ erlang:'!' -----> Module:StateName/3 state function in that order. The state function can postpone an event so it is not retried in the current state. - After a state change all enqueued events (including postponed) - are again presented to the state function. + After a state change all enqueued events (including postponed ones) + are presented again to the state function.

The gen_statem event queue model is sufficient @@ -177,8 +177,10 @@ erlang:'!' -----> Module:StateName/3

Note that a gen_statem does not trap exit signals - automatically, this must be explicitly initiated by - the callback module. + automatically, this must be explicitly initiated in + the callback module (by calling + + process_flag(trap_exit, true).

Unless otherwise stated, all functions in this module fail if @@ -293,6 +295,42 @@ ok in function gen:do_for_proc/2 (gen.erl, line 261) in call from gen_statem:call/3 (gen_statem.erl, line 386) + +

+ And just to compare styles here is the same example using + + callback_mode + + state_functions, or rather here is code to replace + from the init/1 function of the pushbutton + example module above: +

+ +init([]) -> + %% Set the callback mode and initial state + data. + %% Data is used only as a counter. + State = off, Data = 0, + {handle_event_function,State,Data}. + + +%%% Event handling + +handle_event({call,Caller}, push, off, Data) -> + %% Go to 'on', increment count and reply + %% that the resulting status is 'on' + {next_state,on,Data+1,[{reply,Caller,on}]}; +handle_event({call,Caller}, push, on, Data) -> + %% Go to 'off' and reply that the resulting status is 'off' + {next_state,off,Data,[{reply,Caller,off}]}; +%% +%% Event handling common to all states +handle_event({call,Caller}, get_count, State, Data) -> + %% Reply with the current count + {next_state,State,Data,[{reply,Caller,Data}]}; +handle_event(_, _, State, Data) -> + %% Ignore all other events + {next_state,State,Data}. + @@ -564,8 +602,8 @@ ok

If true postpone the current event and retry - it when the state changes that is: - NextState =/= State. + it when the state changes + (NextState =/= State).

@@ -778,14 +816,18 @@ ok The gen_statem will keep the current state, or do a state transition to the current state if you like, set NewData - and execute all Actions + and execute all Actions. + This is the same as + {next_state,CurrentState,NewData,Actions}. keep_state_and_data The gen_statem will keep the current state or do a state transition to the current state if you like, keep the current server data, - and execute all Actions + and execute all Actions. + This is the same as + {next_state,CurrentState,CurrentData,Actions}.

-- cgit v1.2.3