move basic (standard) program and neuron processors to CSys.Programs
This commit is contained in:
parent
5a27454f0c
commit
6318b80196
4 changed files with 48 additions and 34 deletions
|
|
@ -1,6 +1,5 @@
|
|||
defmodule Scopes.CSys do
|
||||
alias Scopes.Core.Actor
|
||||
alias Scopes.CSys.Environ
|
||||
|
||||
def neuron(scope) do
|
||||
Actor.create(fn msg -> process(msg, scope) end)
|
||||
|
|
@ -10,31 +9,10 @@ defmodule Scopes.CSys do
|
|||
fn msg -> Actor.send(rcvr, op.(msg)) end
|
||||
end
|
||||
|
||||
# predefined neuron processors
|
||||
|
||||
def process(msg, scope) do
|
||||
proc(scope).(msg, scope)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def std_proc({:parent}, scope) do
|
||||
env = env(scope)
|
||||
syn = synapse(self(), &Function.identity/1)
|
||||
new = neuron({[], Environ.get_prog(env, :basic), [syn], env})
|
||||
notify({:created, new}, scope)
|
||||
end
|
||||
|
||||
def std_proc(msg, scope) do
|
||||
unless forward(msg, scope), do: notify(msg, scope)
|
||||
end
|
||||
|
||||
# helper functions
|
||||
|
||||
def proc(scope), do: hd(code(scope))
|
||||
|
|
|
|||
|
|
@ -1,22 +1,27 @@
|
|||
defmodule Scopes.CSys.Environ do
|
||||
use Agent
|
||||
|
||||
def create(parent, progs) do
|
||||
{:ok, env} = Agent.start_link(
|
||||
fn -> %{parent: parent, programs: progs} end,
|
||||
name: __MODULE__)
|
||||
def create(parent, progs \\ %{}) do
|
||||
{:ok, env} = Agent.start_link( fn -> {parent, progs} end, name: __MODULE__)
|
||||
env
|
||||
end
|
||||
|
||||
def get_prog(env, name) do
|
||||
Agent.get(env, fn state ->
|
||||
state.programs[name]
|
||||
Agent.get(env, fn {_parent, progs} ->
|
||||
progs[name]
|
||||
end)
|
||||
end
|
||||
|
||||
def put_prog(env, name, prog) do
|
||||
Agent.update(env, fn {parent, progs} ->
|
||||
{parent, Map.put(progs, name, prog)}
|
||||
end)
|
||||
end
|
||||
|
||||
def notify(env, msg) do
|
||||
Agent.cast(env, fn state ->
|
||||
send(state.parent, msg)
|
||||
{parent, _progs} = state
|
||||
send(parent, msg)
|
||||
state
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
33
lib/csys/programs.ex
Normal file
33
lib/csys/programs.ex
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
defmodule Scopes.CSys.Programs do
|
||||
import Scopes.CSys
|
||||
|
||||
alias Scopes.CSys.Environ
|
||||
|
||||
# programs
|
||||
|
||||
def basic_prog() do
|
||||
[&std_proc/2]
|
||||
end
|
||||
|
||||
# processors
|
||||
|
||||
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
|
||||
|
||||
def std_proc({:parent}, scope) do
|
||||
env = env(scope)
|
||||
syn = synapse(self(), &Function.identity/1)
|
||||
new = neuron({[], Environ.get_prog(env, :basic), [syn], env})
|
||||
notify({:created, new}, scope)
|
||||
end
|
||||
|
||||
def std_proc(msg, scope) do
|
||||
unless forward(msg, scope), do: notify(msg, scope)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -5,14 +5,12 @@ defmodule Scopes.CSysTest do
|
|||
alias Scopes.Core.Actor
|
||||
alias Scopes.CSys
|
||||
alias Scopes.CSys.Environ
|
||||
|
||||
def basic_prog() do
|
||||
%{basic: [&CSys.std_proc/2]}
|
||||
end
|
||||
alias Scopes.CSys.Programs
|
||||
|
||||
describe "basic:" do
|
||||
test "minimal-neural-net" do
|
||||
env = Environ.create(self(), basic_prog()) # or: Programs.basic()
|
||||
env = Environ.create(self())
|
||||
Environ.put_prog(env, :basic, Programs.basic_prog())
|
||||
std_program = Environ.get_prog(env, :basic)
|
||||
zero = CSys.neuron({[], std_program, [], env})
|
||||
Actor.send(zero, "Hello Zero!")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue