为什么我收到两次相同的消息?

Why do I get twice the same message?

我是 Erlang 的新手,我正在开发一个小程序来模拟收集罐头的机器人:

-export([start/0]).

start()->
    Believes=#{bin_position=>0, num_cans=>1, can_position=>4, my_position=>1},
    PID=spawn(fun()-> believes(Believes) end),
    action(PID,Believes).

believes(NewBelieves)->

    receive
        {add,Key,Value}->
            N1=maps:put(Key,Value,NewBelieves),
            print(N1),
            action(self(),N1),
            believes(N1);
        {remove, Key}->
            N2=maps:remove(Key, NewBelieves),
            print(N2),
            action(self(),N2),
            believes(N2);
        {update, Key, Value}->
            N3=NewBelieves#{Key => Value},
            print(N3),
            action(self(),N3),
            believes(N3);
        {get}->
            action(self(),NewBelieves),
            believes(NewBelieves)
    end.

print(Map)->
    List=maps:to_list(Map),
    io:format("Believes: ~p~n",[List]).

action(PID, Map)->
    case {is_holding(Map),is_over_bin(PID,Map)} of
            {false,false}->
                %io:format("Is not holding and is not over the bin ~n"),
                %timer:sleep(1000),
                case is_over_can(PID,Map) of
                    false->move(PID,right,Map);
                          % timer:sleep(1000);
                    _->hold(PID)
                       %timer:sleep(1000)
               end;
            {_,false}->
                %io:format("Is holding but is not over the bin ~n"),
                %timer:sleep(1000),
                move(PID, left, Map);
                %timer:sleep(1000);
            {_,_}->
                %io:format("Is holding and is over the bin ~n"),
                %timer:sleep(1000),
                drop(PID),
                timer:sleep(10000),
                exit(self(),kill)
        end.

is_over_can(PID, Map)->
    case same_position(maps:find(my_position,Map),maps:find(can_position, Map)) of
        equal->hold(PID);
        _->false
    end.

is_over_bin(PID, Map)->
    case same_position(maps:find(my_position,Map),maps:find(bin_position, Map)) of
        equal->drop(PID);
        _->false
    end.

is_holding(Map)->
    maps:is_key(holding,Map).

%is_gripper_on(Map)->
%   map:is_key(gripper_on,Map).
%is_touching(Map)->
%   map:is_key(touching,Map).

same_position({ok,A},{ok,B}) when A=:=B -> equal;
same_position(_, _) -> not_equal.

move(PID, Dir, Map)->
    {ok,MyPosition}=maps:find(my_position,Map),
    case Dir of
        right->PID ! {update, my_position, MyPosition+1};
        left->PID ! {update, my_position, MyPosition-1}
    end.  

hold(PID)->
    PID ! {add, holding, []},
    PID ! {update, can_position, nil}.

drop(PID)->
    PID ! {remove, holding},
    PID ! {update, num_cans, 0},
    PID ! {add, cans_collected, 1},
    timer:sleep(10000),
    io:format("Todas las latas han sido recogidas.~n"),
    timer:sleep(10000),
    exit(PID,kill).

并且当我 运行 代码时,在某些情况下我打印了两次(并且我没有从 drop 函数中打印最后 3 次):

prueba2:start().

Believes: [{bin_position,0},{can_position,4},{my_position,2},{num_cans,1}]

Believes: [{bin_position,0},{can_position,4},{my_position,3},{num_cans,1}]

{update,my_position,2}

Believes: [{bin_position,0},{can_position,4},{my_position,4},{num_cans,1}]

(Prueba@Usuario)2> Believes: [{bin_position,0}, {can_position,4}, {holding,[]}, {my_position,4}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,4}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,4}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,4}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,3}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,3}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,3}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,3}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,2}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,2}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,2}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,2}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,1}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,1}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,1}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,1}, {num_cans,1}]

Believes: [{bin_position,0}, {can_position,nil}, {holding,[]}, {my_position,0}, {num_cans,1}]

Todas las latas han sido recogidas.

我是不是做错了什么?是不是跟每个进程需要执行的时间有关?我也不知道是否有办法用 OTP 实现它,我的意思是,使用 gen_server 或其他东西。

非常感谢!

试试 运行 这个代码:

-module(so1).
-export([start/0]).

start()->
    Believes = #{bin_position=>0, num_cans=>1, can_position=>4, my_position=>1},
    PID = spawn(fun()-> believes(Believes) end),
    action(PID,Believes).

believes(NewBelieves)->

    receive
        {add,Key,Value}->
            N1=maps:put(Key,Value,NewBelieves),
            io:format("add clause:~n"),
            print(N1),
            action(self(),N1),
            believes(N1);
        {remove, Key}->
            N2=maps:remove(Key, NewBelieves),
            io:format("remove clause:~n"),
            print(N2),
            action(self(),N2),
            believes(N2);
        {update, Key, Value}->
            N3=NewBelieves#{Key => Value},
            io:format("update clause:~n"),
            print(N3),
            action(self(),N3),
            believes(N3);
        {get}->
            action(self(),NewBelieves),
            believes(NewBelieves)
    end.

print(Map)->
    List=maps:to_list(Map),
    io:format("Believes: ~p~n",[List]).

action(PID, Map)->
    case {is_holding(Map),is_over_bin(PID,Map)} of
            {false,false}->
                %io:format("Is not holding and is not over the bin ~n"),
                %timer:sleep(1000),
                case is_over_can(PID,Map) of
                    false->
                        io:format("action() is calling move()~n"),
                        move(PID,right,Map);
                          % timer:sleep(1000);
                    _->hold(PID)
                       %timer:sleep(1000)
               end;
            {_,false}->
                %io:format("Is holding but is not over the bin ~n"),
                %timer:sleep(1000),
                move(PID, left, Map);
                %timer:sleep(1000);
            {_,_}->
                %io:format("Is holding and is over the bin ~n"),
                %timer:sleep(1000),
                drop(PID),
                timer:sleep(10000),
                exit(self(),kill)
        end.

is_over_can(PID, Map)->
    case same_position(maps:find(my_position,Map),maps:find(can_position, Map)) of
        equal->hold(PID);
        _->false
    end.

is_over_bin(PID, Map)->
    case same_position(maps:find(my_position,Map),maps:find(bin_position, Map)) of
        equal->drop(PID);
        _->false
    end.

is_holding(Map)->
    maps:is_key(holding,Map).

%is_gripper_on(Map)->
%   map:is_key(gripper_on,Map).
%is_touching(Map)->
%   map:is_key(touching,Map).

same_position({ok,A},{ok,B}) when A=:=B -> equal;
same_position(_, _) -> not_equal.

move(PID, Dir, Map)->
    {ok,MyPosition}=maps:find(my_position,Map),
    io:format("move() is going to send update message~n"),
    case Dir of
        right-> PID ! {update, my_position, MyPosition+1};
        left -> PID ! {update, my_position, MyPosition-1}
    end.  

hold(PID)->
    io:format("hold() is sending add, update messages,~n"),
    io:format("which will result in two prints:~n"),
    PID ! {add, holding, []},
    PID ! {update, can_position, nil}.

drop(PID)->
    io:format("drop() is sending remove, update, add messages,~n"),
    io:format("which will result in three prints:~n"),
    PID ! {remove, holding},
    PID ! {update, num_cans, 0},
    PID ! {add, cans_collected, 1},
    timer:sleep(10000),
    io:format("Todas las latas han sido recogidas.~n"),
    timer:sleep(10000),
    exit(PID,kill).

输出将显示您的代码一团糟。此外,当您创建多个进程时,输出可以交错排列,并以不同于写入的顺序出现。

I'm quite new in Erlang,

您只能编写您的调试技能可以处理的程序。现在坚持 20 行或更少。您可能还想尝试使用 erlang debugger 单步执行代码。

I also don't know if there is a way to implement it with OTP, I mean, using gen_server or something.

任何带有接收子句和保存状态的循环变量的无限循环都可以被视为服务器,因此 believes() 可以实现为 gen_server。