csys: helper functions for setup and message handling, not only for testing
This commit is contained in:
parent
37920f3b98
commit
6bd81cad28
3 changed files with 30 additions and 19 deletions
|
|
@ -2,10 +2,13 @@ defmodule Scopes.CSys do
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
alias Scopes.Core.Actor
|
alias Scopes.Core.Actor
|
||||||
|
alias Scopes.CSys.Environ
|
||||||
|
alias Scopes.Shape
|
||||||
|
|
||||||
def neuron(scope) do
|
def neuron(scope) do
|
||||||
Logger.info([scope: inspect(scope)])
|
Logger.info([scope: inspect(scope)])
|
||||||
Actor.create(fn msg -> process(msg, scope) end)
|
Actor.create(fn msg -> process(msg, scope) end)
|
||||||
|
# Environ.register(env, ac, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_neuron(scope) do
|
def update_neuron(scope) do
|
||||||
|
|
@ -22,6 +25,18 @@ defmodule Scopes.CSys do
|
||||||
proc(scope).(msg, scope)
|
proc(scope).(msg, scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def setup(progname, prog) do
|
||||||
|
env = Environ.create(self())
|
||||||
|
Environ.put_prog(env, progname, prog)
|
||||||
|
proc = Environ.get_stage(env, progname, :initial)
|
||||||
|
neuron({[], proc, [], env})
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_value(rcvr, val) do
|
||||||
|
msg = Shape.create([:csys, :data], data: %{value: val})
|
||||||
|
Actor.send(rcvr, msg)
|
||||||
|
end
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,8 @@ defmodule Scopes.CSys.Program do
|
||||||
|
|
||||||
def basic_active(msg = {head, _info}, scope) do
|
def basic_active(msg = {head, _info}, scope) do
|
||||||
case head do
|
case head do
|
||||||
[:csys, :create, :parent | _rest] -> create_parent(scope)
|
[:csys, :create, :pred | _rest] -> create_pred(scope)
|
||||||
[:csys, :create, :child | _rest] -> create_child(scope)
|
[:csys, :create, :succ | _rest] -> create_succ(scope)
|
||||||
_ -> forward(msg, scope) || notify(msg, scope)
|
_ -> forward(msg, scope) || notify(msg, scope)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -46,14 +46,14 @@ defmodule Scopes.CSys.Program do
|
||||||
# todo: provide parameters for initial state, proc / stage, op,
|
# todo: provide parameters for initial state, proc / stage, op,
|
||||||
# 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_parent(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(), &Function.identity/1, 0)
|
||||||
#neuron({[], Environ.get_stage(env, :basic, :initial), [syn], env})
|
#neuron({[], Environ.get_stage(env, :basic, :initial), [syn], env})
|
||||||
new = neuron({[], proc, [syn], env})
|
new = neuron({[], proc, [syn], env})
|
||||||
notify({:created, new}, scope)
|
notify({:created, new}, scope)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_child (scope = {state, proc, syns, env}) do
|
def create_succ (scope = {state, proc, syns, env}) do
|
||||||
new = neuron({[], proc, [], env})
|
new = neuron({[], proc, [], env})
|
||||||
syn = synapse(new, &Function.identity/1, 0)
|
syn = synapse(new, &Function.identity/1, 0)
|
||||||
update_neuron({state, proc, [syn | syns], env})
|
update_neuron({state, proc, [syn | syns], env})
|
||||||
|
|
|
||||||
|
|
@ -4,32 +4,28 @@ defmodule Scopes.CSysTest do
|
||||||
|
|
||||||
alias Scopes.Core.Actor
|
alias Scopes.Core.Actor
|
||||||
alias Scopes.CSys
|
alias Scopes.CSys
|
||||||
alias Scopes.CSys.Environ
|
|
||||||
alias Scopes.CSys.Program
|
alias Scopes.CSys.Program
|
||||||
alias Scopes.Shape
|
alias Scopes.Shape
|
||||||
|
|
||||||
def send_value(rcvr, val) do
|
def receive_with(head) do
|
||||||
msg = Shape.create([:csys, :data], data: %{value: val})
|
|
||||||
Actor.send(rcvr, msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
def receive_head(head) do
|
|
||||||
assert_receive {^head, info}
|
assert_receive {^head, info}
|
||||||
{head, info}
|
{head, info}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def receive_data() do
|
||||||
|
Shape.data(receive_with([:csys, :data]))
|
||||||
|
end
|
||||||
|
|
||||||
describe "basic:" do
|
describe "basic:" do
|
||||||
test "minimal-neural-net" do
|
test "minimal-neural-net" do
|
||||||
env = Environ.create(self())
|
zero = CSys.setup(:basic, Program.basic_prog())
|
||||||
Environ.put_prog(env, :basic, Program.basic_prog())
|
|
||||||
proc = Environ.get_stage(env, :basic, :initial)
|
|
||||||
zero = CSys.neuron({[], proc, [], env})
|
|
||||||
#Actor.send(zero, "Hello Zero!")
|
#Actor.send(zero, "Hello Zero!")
|
||||||
send_value(zero, "Hello Zero!")
|
CSys.send_value(zero, "Hello Zero!")
|
||||||
assert "Hello Zero!" = Shape.data(receive_head([:csys, :data])).value
|
assert "Hello Zero!" = receive_data().value
|
||||||
Actor.send(zero, {[:csys, :create, :parent], nil})
|
Actor.send(zero, {[:csys, :create, :pred], nil})
|
||||||
|
#Actor.send(zero, {[:csys, :create, :pred], data: %{op: &neg_only})
|
||||||
assert_receive {:created, new1}
|
assert_receive {:created, new1}
|
||||||
Actor.send(new1, {[:csys, :create, :child], nil})
|
Actor.send(new1, {[:csys, :create, :succ], nil})
|
||||||
assert_receive {:created, _new2}
|
assert_receive {:created, _new2}
|
||||||
Actor.send(new1, "Hello New1!")
|
Actor.send(new1, "Hello New1!")
|
||||||
assert_receive "Hello New1!"
|
assert_receive "Hello New1!"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue