diff --git a/lib/core/actor.ex b/lib/core/actor.ex index e45d2bf..0355f9f 100644 --- a/lib/core/actor.ex +++ b/lib/core/actor.ex @@ -20,9 +20,10 @@ defmodule Scopes.Core.Actor do end def send(ac, msg, delay \\ 0) do - Process.sleep(delay) - #Process.send_after(ac, {:message, msg}, delay) - Kernel.send(ac, {:message, msg}) + if msg do + Process.sleep(delay) + Kernel.send(ac, {:message, msg}) + end end def become(ac, bhv) do diff --git a/lib/csys/csys.ex b/lib/csys/csys.ex index 65e10bd..ff6c6e8 100644 --- a/lib/csys/csys.ex +++ b/lib/csys/csys.ex @@ -17,12 +17,26 @@ defmodule Scopes.CSys do fn msg -> Actor.send(rcvr, op.(msg), delay) end end - # helper functions + # message handlers / proc steps def process(msg, scope) do proc(scope).(msg, scope) end + def notify(msg, scope) do + Actor.send(env(scope), msg) + end + + def forward(msg, scope) do + Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg); true end) + end + + # synapse operations + + def noop(msg), do: msg + + # sending shortcuts + def send_message(rcvr, head, data) do Actor.send(rcvr, Shape.create(head, data: data)) end @@ -31,6 +45,8 @@ defmodule Scopes.CSys do send_message(rcvr, [:csys, :data], %{value: val}) end + # scope access shortcuts + def state(scope), do: elem(scope, 0) def proc(scope), do: elem(scope, 1) def syns(scope), do: elem(scope, 2) diff --git a/lib/csys/program.ex b/lib/csys/program.ex index a86628a..fc1ccee 100644 --- a/lib/csys/program.ex +++ b/lib/csys/program.ex @@ -1,9 +1,8 @@ defmodule Scopes.CSys.Program do import Scopes.CSys, only: [ - neuron: 1, update_neuron: 1, synapse: 3, - syns: 1, env: 1 + neuron: 1, update_neuron: 1, synapse: 3 ] - alias Scopes.Core.Actor + alias Scopes.CSys defmodule State do defstruct [:value, :stage, :prog] @@ -14,14 +13,13 @@ defmodule Scopes.CSys.Program do {state, get_proc(state)} end - def get_proc(stages, stage) do - stages[stage] || stages[:default] - end - def get_proc(state) do get_proc(elem(state.prog, 0), state.stage) end + defp get_proc(stages, stage) do + stages[stage] || stages[:default] + end # basic program @@ -62,23 +60,15 @@ defmodule Scopes.CSys.Program do #[:csys, :next | _rest] -> next(scope, meta) #[:csys, :data | rest] -> process_data(rest, Shape.data(msg), scope) #[:csys, action | rest] -> transition(action, scope) - _ -> forward(msg, scope) || notify(msg, scope) + _ -> CSys.forward(msg, scope) || CSys.notify(msg, scope) end end def basic(msg, scope) do - forward(msg, scope) || notify(msg, scope) + CSys.forward(msg, scope) || CSys.notify(msg, scope) end -# processor steps, helper functions - - def notify(msg, scope) do - Actor.send(env(scope), msg) - end - - def forward(msg, scope) do - Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg); true end) - end +# helper functions def restart({state, _proc, syns, env}) do nstate = %{state | stage: :initial} @@ -92,16 +82,16 @@ defmodule Scopes.CSys.Program do # depending on state and stage (proc) of self, as well as message, env, ... def create_pred(scope = {state, proc, _syns, env}) do - syn = synapse(self(), &Function.identity/1, 0) + syn = synapse(self(), &CSys.noop/1, 0) new = neuron(restart({state, proc, [syn], env})) - notify({:created, new}, scope) + CSys.notify({:created, new}, scope) update_neuron(restart(scope)) end def create_succ (scope = {state, proc, syns, env}) do new = neuron(restart({state, proc, [], env})) - notify({:created, new}, scope) - syn = synapse(new, &Function.identity/1, 0) + CSys.notify({:created, new}, scope) + syn = synapse(new, &CSys.noop/1, 0) update_neuron(restart({state, proc, [syn | syns], env})) end end