Moved Life implementations into 'life' directory.
[cellular-automata.git] / life / 001 / src / life_god.erl
diff --git a/life/001/src/life_god.erl b/life/001/src/life_god.erl
new file mode 100644 (file)
index 0000000..ea3b236
--- /dev/null
@@ -0,0 +1,45 @@
+-module(life_god).
+-behaviour(supervisor).
+
+
+%% API
+-export([start_link/3]).
+
+%% Callbacks
+-export([init/1]).
+
+
+%% Helper macro for declaring children of supervisor
+-define(CHILD(Type, I, Args), {I, {I, start_link, Args}, permanent, 5000, Type, [I]}).
+
+
+%% ============================================================================
+%% API
+%% ============================================================================
+
+start_link(X, Y, CellData) ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, [X, Y, CellData]).
+
+
+%% ============================================================================
+%% Callbacks
+%% ============================================================================
+
+init([X, Y, CellData]) ->
+    CellNames = [Name || {_, Name, _} <- CellData],
+    RestartStrategy = {one_for_one, 1000000, 1},
+
+    Observer = ?CHILD(worker, life_observer, [X, Y]),
+    Cells = [spec_cell(Datum) || Datum <- CellData],
+    Time = ?CHILD(worker, life_time, [X, Y, CellNames]),
+
+    Children = [Observer | Cells ++ [Time]],
+
+    {ok, {RestartStrategy, Children}}.
+
+
+spec_cell({_, Name, _}=Datum) ->
+    M = life_cell,
+    F = start_link,
+    A = [Datum],
+    {Name, {M, F, A}, permanent, 5000, worker, [M]}.
This page took 0.029325 seconds and 4 git commands to generate.