62 lines
1.5 KiB
Elixir
62 lines
1.5 KiB
Elixir
defmodule Scopes.CSys.Program do
|
|
import Scopes.CSys, only: [
|
|
neuron: 1, update_neuron: 1, synapse: 3,
|
|
syns: 1, env: 1
|
|
]
|
|
|
|
alias Scopes.CSys.Environ
|
|
|
|
# programs
|
|
|
|
def basic_prog() do
|
|
%{default: &basic_active/2,
|
|
initial: &basic_active/2,
|
|
active: &basic_active/2,
|
|
retired: &basic_active/2
|
|
}
|
|
end
|
|
|
|
# processors
|
|
|
|
def basic_active(msg = {head, _info}, scope) do
|
|
case head do
|
|
[:csys, :create, :parent | _rest] -> create_parent(scope)
|
|
[:csys, :create, :child | _rest] -> create_child(scope)
|
|
_ -> forward(msg, scope) || notify(msg, scope)
|
|
end
|
|
end
|
|
|
|
def basic_active(msg, scope) do
|
|
forward(msg, scope) || notify(msg, scope)
|
|
end
|
|
|
|
# processor steps
|
|
|
|
def notify(msg, scope) do
|
|
Environ.notify(env(scope), msg)
|
|
end
|
|
|
|
def forward(msg, scope) do
|
|
Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg); true end)
|
|
end
|
|
|
|
# step functions
|
|
|
|
# create_... steps
|
|
# todo: provide parameters for initial state, proc / stage, op,
|
|
# depending on state and stage (proc) of self, as well as message, env, ...
|
|
|
|
def create_parent(scope = {_state, proc, _syns, env}) do
|
|
syn = synapse(self(), &Function.identity/1, 0)
|
|
#neuron({[], Environ.get_stage(env, :basic, :initial), [syn], env})
|
|
new = neuron({[], proc, [syn], env})
|
|
notify({:created, new}, scope)
|
|
end
|
|
|
|
def create_child (scope = {state, proc, syns, env}) do
|
|
new = neuron({[], proc, [], env})
|
|
syn = synapse(new, &Function.identity/1, 0)
|
|
update_neuron({state, proc, [syn | syns], env})
|
|
notify({:created, new}, scope)
|
|
end
|
|
end
|