[erlang-questions] Newbie problems with gen_fsm

Oliver Korpilla <>
Sun May 15 19:50:00 CEST 2016


Further observations:

request_ticket(Price) ->
  gen_fsm:start(?MODULE, Price, []).

http://erlang.org/doc/man/gen_fsm.html#start-3

This functions returns {ok, Pid} or {error, Reason}

so to fit with the rest of your program, it probably should look like this:

request_ticket(Price) ->
  {ok, Pid}  gen_fsm:start(?MODULE, Price, []),
  Pid.

Cheers,
Oliver 
 

Gesendet: Sonntag, 15. Mai 2016 um 19:33 Uhr
Von: "Oliver Korpilla" <>
An: 
Cc: "Erlang Users' List" <>
Betreff: Re: [erlang-questions] Newbie problems with gen_fsm
Hello,

When I see this:

awaiting_payment({payment, Amount}, From, {Price, Paid}) when Amount >= (Price - Paid) ->
io:format("~p paid ~p", [From, Amount]),
Change = Amount + Paid - Price,
{next_state, valid_ticket, {change, Change}, {Price, Price}};

There are too many elements in the tuple returned.

| {next_state,NextStateName,NewStateData}
| {next_state,NextStateName,NewStateData,Timeout}
| {next_state,NextStateName,NewStateData,hibernate}

You are allowed to return these when selecting next_state as your return value. But you return a 4-tuple instead of a 3-tuple.

If one of these is a reply back to the original requestor, then you need:

{reply,Reply,NextStateName,NewStateData}

Also, not 100% sure, but:

init(Price) ->
{ok, awaiting_payment, {Price, 0}}.


I think it has to be:

init([Price]) ->

because init/1 takes the start_link params as a list.

Sorry, no Erlang system under my fingertips to test. Hope this helps.

Cheers,
Oliver 
 

Gesendet: Sonntag, 15. Mai 2016 um 16:26 Uhr
Von: TA <>
An: "Erlang Users' List" <>
Betreff: [erlang-questions] Newbie problems with gen_fsm

Hello,
 
I wrote some code for a parking meter to understand how gen_fsm works. There are two states (awaiting_payment, valid_ticket), two input messages (payment, cancel_ticket), and three output messages (unpaid, change, refund).
 
I'm getting the following error and don't understand what's wrong. Any pointers?
 
Error:
** exception exit: {{function_clause,[{gen,do_for_proc,
                                           [{ok,<0.40.0>},#Fun<gen.0.132519590>],
                                           [{file,"gen.erl"},{line,252}]},
                                      {gen_fsm,sync_send_event,2,
                                               [{file,"gen_fsm.erl"},{line,219}]},
                                      {ticketing_fsm,demo1,0,
                                                     [{file,"ticketing_fsm.erl"},{line,25}]},
                                      {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
                                      {shell,exprs,7,[{file,"shell.erl"},{line,686}]},
                                      {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},
                                      {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]},
                    {gen_fsm,sync_send_event,[{ok,<0.40.0>},{payment,100}]}}
     in function  gen_fsm:sync_send_event/2 (gen_fsm.erl, line 223)
     in call from ticketing_fsm:demo1/0 (ticketing_fsm.erl, line 25)
 
Source: https://transfer.sh/M7n3b/ticketing-fsm.erl
 
Tobias_______________________________________________ erlang-questions mailing list  http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions][http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions]]
_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions]


More information about the erlang-questions mailing list