csys: move proc steps to CSys, start with synapse ops / op steps
This commit is contained in:
parent
31f35aa2f8
commit
d3771d0a77
3 changed files with 33 additions and 26 deletions
|
|
@ -20,10 +20,11 @@ defmodule Scopes.Core.Actor do
|
||||||
end
|
end
|
||||||
|
|
||||||
def send(ac, msg, delay \\ 0) do
|
def send(ac, msg, delay \\ 0) do
|
||||||
|
if msg do
|
||||||
Process.sleep(delay)
|
Process.sleep(delay)
|
||||||
#Process.send_after(ac, {:message, msg}, delay)
|
|
||||||
Kernel.send(ac, {:message, msg})
|
Kernel.send(ac, {:message, msg})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def become(ac, bhv) do
|
def become(ac, bhv) do
|
||||||
Kernel.send(ac, {:become, bhv})
|
Kernel.send(ac, {:become, bhv})
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,26 @@ defmodule Scopes.CSys do
|
||||||
fn msg -> Actor.send(rcvr, op.(msg), delay) end
|
fn msg -> Actor.send(rcvr, op.(msg), delay) end
|
||||||
end
|
end
|
||||||
|
|
||||||
# helper functions
|
# message handlers / proc steps
|
||||||
|
|
||||||
def process(msg, scope) do
|
def process(msg, scope) do
|
||||||
proc(scope).(msg, scope)
|
proc(scope).(msg, scope)
|
||||||
end
|
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
|
def send_message(rcvr, head, data) do
|
||||||
Actor.send(rcvr, Shape.create(head, data: data))
|
Actor.send(rcvr, Shape.create(head, data: data))
|
||||||
end
|
end
|
||||||
|
|
@ -31,6 +45,8 @@ defmodule Scopes.CSys do
|
||||||
send_message(rcvr, [:csys, :data], %{value: val})
|
send_message(rcvr, [:csys, :data], %{value: val})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# scope access shortcuts
|
||||||
|
|
||||||
def state(scope), do: elem(scope, 0)
|
def state(scope), do: elem(scope, 0)
|
||||||
def proc(scope), do: elem(scope, 1)
|
def proc(scope), do: elem(scope, 1)
|
||||||
def syns(scope), do: elem(scope, 2)
|
def syns(scope), do: elem(scope, 2)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
defmodule Scopes.CSys.Program do
|
defmodule Scopes.CSys.Program do
|
||||||
import Scopes.CSys, only: [
|
import Scopes.CSys, only: [
|
||||||
neuron: 1, update_neuron: 1, synapse: 3,
|
neuron: 1, update_neuron: 1, synapse: 3
|
||||||
syns: 1, env: 1
|
|
||||||
]
|
]
|
||||||
alias Scopes.Core.Actor
|
alias Scopes.CSys
|
||||||
|
|
||||||
defmodule State do
|
defmodule State do
|
||||||
defstruct [:value, :stage, :prog]
|
defstruct [:value, :stage, :prog]
|
||||||
|
|
@ -14,14 +13,13 @@ defmodule Scopes.CSys.Program do
|
||||||
{state, get_proc(state)}
|
{state, get_proc(state)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_proc(stages, stage) do
|
|
||||||
stages[stage] || stages[:default]
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_proc(state) do
|
def get_proc(state) do
|
||||||
get_proc(elem(state.prog, 0), state.stage)
|
get_proc(elem(state.prog, 0), state.stage)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_proc(stages, stage) do
|
||||||
|
stages[stage] || stages[:default]
|
||||||
|
end
|
||||||
|
|
||||||
# basic program
|
# basic program
|
||||||
|
|
||||||
|
|
@ -62,23 +60,15 @@ defmodule Scopes.CSys.Program do
|
||||||
#[:csys, :next | _rest] -> next(scope, meta)
|
#[:csys, :next | _rest] -> next(scope, meta)
|
||||||
#[:csys, :data | rest] -> process_data(rest, Shape.data(msg), scope)
|
#[:csys, :data | rest] -> process_data(rest, Shape.data(msg), scope)
|
||||||
#[:csys, action | rest] -> transition(action, scope)
|
#[:csys, action | rest] -> transition(action, scope)
|
||||||
_ -> forward(msg, scope) || notify(msg, scope)
|
_ -> CSys.forward(msg, scope) || CSys.notify(msg, scope)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def basic(msg, scope) do
|
def basic(msg, scope) do
|
||||||
forward(msg, scope) || notify(msg, scope)
|
CSys.forward(msg, scope) || CSys.notify(msg, scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
# processor steps, helper functions
|
# 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
|
|
||||||
|
|
||||||
def restart({state, _proc, syns, env}) do
|
def restart({state, _proc, syns, env}) do
|
||||||
nstate = %{state | stage: :initial}
|
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, ...
|
# depending on state and stage (proc) of self, as well as message, env, ...
|
||||||
|
|
||||||
def create_pred(scope = {state, proc, _syns, env}) do
|
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}))
|
new = neuron(restart({state, proc, [syn], env}))
|
||||||
notify({:created, new}, scope)
|
CSys.notify({:created, new}, scope)
|
||||||
update_neuron(restart(scope))
|
update_neuron(restart(scope))
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_succ (scope = {state, proc, syns, env}) do
|
def create_succ (scope = {state, proc, syns, env}) do
|
||||||
new = neuron(restart({state, proc, [], env}))
|
new = neuron(restart({state, proc, [], env}))
|
||||||
notify({:created, new}, scope)
|
CSys.notify({:created, new}, scope)
|
||||||
syn = synapse(new, &Function.identity/1, 0)
|
syn = synapse(new, &CSys.noop/1, 0)
|
||||||
update_neuron(restart({state, proc, [syn | syns], env}))
|
update_neuron(restart({state, proc, [syn | syns], env}))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue