Observer logs stats for each run.
[cellular-automata.git] / 001 / src / life_time.erl
... / ...
CommitLineData
1-module(life_time).
2-behaviour(gen_server).
3
4
5%% API
6-export([start_link/3
7 ,report_state/3
8 ]).
9
10%% Callbacks
11-export([init/1
12 ,handle_call/3
13 ,handle_cast/2
14 ,handle_info/2
15 ,terminate/2
16 ,code_change/3
17 ]).
18
19
20-define(INTERVAL, 100). % In milliseconds
21
22-define(CHAR_DEAD, 32). % " "
23-define(CHAR_ALIVE, 111). % "o"
24-define(CHAR_BAR, 45). % "-"
25
26
27-record(state, {x :: integer()
28 ,y :: integer()
29 ,cells :: list(atom())
30 ,num_cells :: integer()
31 ,num_dead :: integer()
32 ,num_alive :: integer()
33 ,state_pairs :: list(tuple(integer(), integer())) | []
34 ,replies_pending :: integer()
35 ,gen_id :: integer()
36 }).
37
38
39%% ============================================================================
40%% API
41%% ============================================================================
42
43start_link(X, Y, Cells) ->
44 ServerName = {local, ?MODULE},
45 Args = [X, Y, Cells],
46 Opts = [],
47 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
48
49
50report_state(CellID, GenID, CellState) ->
51 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
52
53
54%% ============================================================================
55%% Callbacks (unused)
56%% ============================================================================
57
58handle_call(_Msg, _From, State) -> {reply, ok, State}.
59handle_info(_Msg, State) -> {noreply, State}.
60code_change(_Old, State, _Other) -> {ok, State}.
61terminate(_Reason, State) -> {ok, State}.
62
63
64%% ============================================================================
65%% Callbacks
66%% ============================================================================
67
68init([X, Y, Cells]) ->
69 State = #state{x = X
70 ,y = Y
71 ,cells = Cells
72 ,num_cells = length(Cells)
73 ,state_pairs = []
74 ,replies_pending = 0
75 ,gen_id = 0
76 },
77 ok = schedule_next_gen(),
78 {ok, State}.
79
80
81handle_cast(next_gen,
82 #state{cells=Cells
83 ,num_cells=NumCells
84 ,state_pairs=[]
85 ,gen_id=GenID
86 }=State) ->
87
88 NewGenID = GenID + 1,
89 ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
90 NewState = State#state{replies_pending=NumCells
91 ,gen_id=NewGenID
92 ,num_dead=0
93 ,num_alive=0
94 },
95 {noreply, NewState};
96
97handle_cast({report_state, {CellID, GenID, CellState}},
98 #state{x=X
99 ,y=Y
100 ,num_dead=NDead
101 ,num_alive=NAlive
102 ,state_pairs=StatePairs
103 ,replies_pending=RepliesPending
104 ,gen_id=GenID
105 ,num_cells=NumCells
106 }=State) ->
107
108 NewStatePairs = [{CellID, CellState} | StatePairs],
109 NewRepliesPending = RepliesPending - 1,
110 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
111 NewState = State#state{replies_pending=NewRepliesPending
112 ,num_dead=NewNDead
113 ,num_alive=NewNAlive
114 },
115
116 case NewRepliesPending of
117 0 ->
118 SortedStatePairs = lists:sort(
119 fun({A, _}, {B, _}) -> A < B end,
120 NewStatePairs
121 ),
122 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
123
124 ok = life_observer:log_generation(GenID, NewNDead, NewNAlive),
125
126 ok = io:format(
127 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
128 [X, Y, NumCells, NewNDead, NewNAlive, GenID]
129 ),
130 ok = do_print_bar(X),
131 ok = do_print_state_chars(X, StateChars),
132 ok = timer:sleep(?INTERVAL),
133 ok = schedule_next_gen(),
134 {noreply, NewState#state{state_pairs=[]}};
135
136 _N ->
137 {noreply, NewState#state{state_pairs=NewStatePairs}}
138 end;
139
140handle_cast(_Msg, State) ->
141 {noreply, State}.
142
143
144%% ============================================================================
145%% Internal
146%% ============================================================================
147
148increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
149increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
150
151
152schedule_next_gen() ->
153 ok = gen_server:cast(?MODULE, next_gen).
154
155
156state_to_char(0) -> ?CHAR_DEAD;
157state_to_char(1) -> ?CHAR_ALIVE.
158
159
160do_print_state_chars(_, []) -> ok;
161do_print_state_chars(X, Chars) ->
162 {XChars, RestChars} = lists:split(X, Chars),
163 ok = io:format([XChars, $\n]),
164 do_print_state_chars(X, RestChars).
165
166
167do_print_bar(X) ->
168 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.02116 seconds and 4 git commands to generate.