Search code examples
erlangerlang-otpfsm

Can not run fsm in Erlang


Hello i am trying to run a fsm using gen_statem in a separate process from the caller.I am also trying to keep in its state arguments the caller, so it can be updated after each state change. I keep getting this error :

  ** State machine <0.123.0> terminating
** Last event = {internal,init_state}
** When server state  = {sitting_home,{state,"None",0,0,[<0.116.0>]}}
** Reason for termination = error:{'function not exported',
                                      {fsm,callback_mode,0}}
** Callback mode = undefined
** Stacktrace =
**  [{gen_statem,call_callback_mode,1,[{file,"gen_statem.erl"},{line,1610}]},
     {gen_statem,enter,7,[{file,"gen_statem.erl"},{line,679}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,249}]}]

=CRASH REPORT==== 12-Dec-2019::00:14:42.717000 ===
  crasher:
    initial call: fsm:init/1
    pid: <0.123.0>
    registered_name: []
    exception error: undefined function fsm:callback_mode/0
      in function  gen_statem:call_callback_mode/1 (gen_statem.erl, line 1610)
      in call from gen_statem:enter/7 (gen_statem.erl, line 679)
    ancestors: [<0.116.0>]
    message_queue_len: 0
    messages: []
    links: []
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 1598
    stack_size: 27
    reductions: 5805
  neighbours:

I have looked in the erlang documentation and i can see no callback mode that needs to be implemented here.

Module

-module(fsm).
-record(state,{
    current="None",
    intvCount=0,
    jobCount=0,
    monitor
}).
-export([init/1,start/0]).
-export([hire/2,fire/1,interview/2]).

-behaviour(gen_statem).


start()->
    gen_statem:start(?MODULE,[self()],[]).

init(Monitor)->
    {ok,sitting_home,#state{current="None",jobCount=0,intvCount=0,monitor=Monitor}}.

sitting_home({intv,Company},State=#state{intvCount=C,monitor=M})->
     gen_statem:reply(M,"Got an interview from:"++Company++" going interviewing"),
     {next_state,interviewing,State#state{intvCount=C+1}};
sitting_home(Event,State)->
     gen_statem:reply(State#state.monitor,{unexpected , Event}),
     {next_state,sitting_home,State}.

interviewing({rejected,Company},State)->
    gen_statem:reply("Booh got rejected by :"++ Company),
    {next_state,sitting_home,State};
interviewing({accepted,Company},State=#state{jobCount=J})->
    gen_statem:reply("Hooray got accepted"),
    {next_state,working,State#state{jobCount=J+1,current=Company}};
interviewing(_,State)->
    gen_statem:reply("Unknown message"),
    {next_state,interviewing,State}.


working(fire,State=#state{current=C})->
    gen_statem:reply("Unexpected event"),
    {next_state,working,State#state{current="None"}};
working(Event,State)->
        gen_statem:reply("Unexpected event"),
        {next_state,working,State}.

Events

hire(Company,PID)->
    gen_statem:sync_send_event(PID,{hire,self(),Company},0).
fire(PID)->
    gen_statem:sync_send_event(PID,{fire,self()},0).
interview(Company,PID)->
    gen_state:sync_send_event(PID,{intv,Company},0).

P.S I use gen_statem because the shell told me the gen_fsm is deprecated.I do not know the equivalent for sync_send_event.Could this be the problem?


Solution

    1. Module:callback_mode is documented at the page you linked. When you have a function for each state (sitting_home, interviewing, etc.) it should be

      callback_mode() -> state_functions.
      
    2. The state functions take 3 arguments, not 2. You are missing the first one, EventType which should be {call, From} in your case.

    3. The equivalent for sync_send_event can just be call, but then your state functions should always reply.

    4. You can combine reply and next_state in the return value of a callback, e.g.

      working(Event,State)->
          gen_statem:reply("Unexpected event"),
          {next_state,working,State}.
      

      can become

      working({call, From}, Event, State)->
          {next_state, working, State, {reply, From, "Unexpected event"}}.
      

      Even if you don't do this, reply needs a From argument:

      working({call, From}, Event, State)->
          gen_statem:reply(From, "Unexpected event"),
          {next_state,working,State}.