csys: + environ for storing programs and parent, with get and notify functions
This commit is contained in:
parent
386f6e1790
commit
5a27454f0c
4 changed files with 41 additions and 9 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
# Used by "mix format"
|
# Used by "mix format"
|
||||||
[
|
[
|
||||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
# inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
inputs: ["{mix,.formatter}.exs"] # avoid unintentional formatting of code
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
defmodule Scopes.CSys do
|
defmodule Scopes.CSys do
|
||||||
alias Scopes.Core.Actor
|
alias Scopes.Core.Actor
|
||||||
|
alias Scopes.CSys.Environ
|
||||||
|
|
||||||
def neuron(scope) do
|
def neuron(scope) do
|
||||||
Actor.create(fn msg -> process(msg, scope) end)
|
Actor.create(fn msg -> process(msg, scope) end)
|
||||||
|
|
@ -15,23 +16,23 @@ defmodule Scopes.CSys do
|
||||||
proc(scope).(msg, scope)
|
proc(scope).(msg, scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def echo(msg, scope) do
|
def notify(msg, scope) do
|
||||||
send(env(scope), msg)
|
Environ.notify(env(scope), msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
def forward(msg, scope) do
|
def forward(msg, scope) do
|
||||||
Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg) end)
|
Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg); true end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def std_proc({:parent}, scope) do
|
def std_proc({:parent}, scope) do
|
||||||
|
env = env(scope)
|
||||||
syn = synapse(self(), &Function.identity/1)
|
syn = synapse(self(), &Function.identity/1)
|
||||||
new = neuron({[], [&std_proc/2], [syn], env(scope)})
|
new = neuron({[], Environ.get_prog(env, :basic), [syn], env})
|
||||||
# send(env(scope), {:created, new})
|
notify({:created, new}, scope)
|
||||||
echo({:created, new}, scope)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def std_proc(msg, scope) do
|
def std_proc(msg, scope) do
|
||||||
unless forward(msg, scope), do: echo(msg, scope)
|
unless forward(msg, scope), do: notify(msg, scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
# helper functions
|
# helper functions
|
||||||
|
|
|
||||||
23
lib/csys/environ.ex
Normal file
23
lib/csys/environ.ex
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
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__)
|
||||||
|
env
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_prog(env, name) do
|
||||||
|
Agent.get(env, fn state ->
|
||||||
|
state.programs[name]
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify(env, msg) do
|
||||||
|
Agent.cast(env, fn state ->
|
||||||
|
send(state.parent, msg)
|
||||||
|
state
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -4,10 +4,17 @@ defmodule Scopes.CSysTest do
|
||||||
|
|
||||||
alias Scopes.Core.Actor
|
alias Scopes.Core.Actor
|
||||||
alias Scopes.CSys
|
alias Scopes.CSys
|
||||||
|
alias Scopes.CSys.Environ
|
||||||
|
|
||||||
|
def basic_prog() do
|
||||||
|
%{basic: [&CSys.std_proc/2]}
|
||||||
|
end
|
||||||
|
|
||||||
describe "basic:" do
|
describe "basic:" do
|
||||||
test "minimal-neural-net" do
|
test "minimal-neural-net" do
|
||||||
zero = CSys.neuron({[], [&CSys.std_proc/2], [], self()})
|
env = Environ.create(self(), basic_prog()) # or: Programs.basic()
|
||||||
|
std_program = Environ.get_prog(env, :basic)
|
||||||
|
zero = CSys.neuron({[], std_program, [], env})
|
||||||
Actor.send(zero, "Hello Zero!")
|
Actor.send(zero, "Hello Zero!")
|
||||||
assert_receive "Hello Zero!"
|
assert_receive "Hello Zero!"
|
||||||
Actor.send(zero, {:parent})
|
Actor.send(zero, {:parent})
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue