Extended usage instructions.
[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 ,gen_began :: erlang:timestamp()
37 }).
38
39
40%% ============================================================================
41%% API
42%% ============================================================================
43
44start_link(X, Y, Cells) ->
45 ServerName = {local, ?MODULE},
46 Args = [X, Y, Cells],
47 Opts = [],
48 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
49
50
51report_state(CellID, GenID, CellState) ->
52 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
53
54
55%% ============================================================================
56%% Callbacks (unused)
57%% ============================================================================
58
59handle_call(_Msg, _From, State) -> {reply, ok, 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_info(next_gen,
82 #state{cells=Cells
83 ,num_cells=NumCells
84 ,state_pairs=[]
85 ,gen_id=GenID
86 }=State) ->
87
88 GenBegan = os:timestamp(),
89 NewGenID = GenID + 1,
90 ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
91 NewState = State#state{replies_pending=NumCells
92 ,gen_id=NewGenID
93 ,gen_began=GenBegan
94 ,num_dead=0
95 ,num_alive=0
96 },
97 {noreply, NewState};
98
99handle_info(_Msg, State) ->
100 {noreply, State}.
101
102
103handle_cast({report_state, {CellID, GenID, CellState}},
104 #state{x=X
105 ,y=Y
106 ,num_dead=NDead
107 ,num_alive=NAlive
108 ,state_pairs=StatePairs
109 ,replies_pending=RepliesPending
110 ,gen_id=GenID
111 ,gen_began=GenBegan
112 ,num_cells=NumCells
113 }=State) ->
114
115 NewStatePairs = [{CellID, CellState} | StatePairs],
116 NewRepliesPending = RepliesPending - 1,
117 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
118 NewState = State#state{replies_pending=NewRepliesPending
119 ,num_dead=NewNDead
120 ,num_alive=NewNAlive
121 },
122
123 case NewRepliesPending of
124 0 ->
125 SortedStatePairs = lists:sort(
126 fun({A, _}, {B, _}) -> A < B end,
127 NewStatePairs
128 ),
129 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
130
131 GenDuration = timer:now_diff(os:timestamp(), GenBegan) / 1000000,
132
133 ok = life_observer:log_generation(GenID, GenDuration, NewNDead, NewNAlive),
134
135 ok = io:format(
136 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n",
137 [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDuration]
138 ),
139 ok = do_print_bar(X),
140 ok = do_print_state_chars(X, StateChars),
141
142 ok = schedule_next_gen(),
143 {noreply, NewState#state{state_pairs=[]}};
144
145 _N ->
146 {noreply, NewState#state{state_pairs=NewStatePairs}}
147 end;
148
149handle_cast(_Msg, State) ->
150 {noreply, State}.
151
152
153%% ============================================================================
154%% Internal
155%% ============================================================================
156
157increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
158increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
159
160
161schedule_next_gen() ->
162 erlang:send_after(?INTERVAL, self(), next_gen),
163 ok.
164
165
166state_to_char(0) -> ?CHAR_DEAD;
167state_to_char(1) -> ?CHAR_ALIVE.
168
169
170do_print_state_chars(_, []) -> ok;
171do_print_state_chars(X, Chars) ->
172 {XChars, RestChars} = lists:split(X, Chars),
173 ok = io:format([XChars, $\n]),
174 do_print_state_chars(X, RestChars).
175
176
177do_print_bar(X) ->
178 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.017126 seconds and 4 git commands to generate.