From 3a9d281f65935a8d256e8b114020c40de7c527b4 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 20 May 2026 19:51:31 +0200 Subject: [PATCH] csys: a second test with just 1 neuron, linked to itself, + 1 effector --- lib/csys/csys.ex | 1 + lib/csys/environ.ex | 8 +++++--- lib/csys/program.ex | 25 +++++++++++++------------ test/csys_test.exs | 21 ++++++++++++++++++++- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/csys/csys.ex b/lib/csys/csys.ex index 5d87c9a..bbc2c0d 100644 --- a/lib/csys/csys.ex +++ b/lib/csys/csys.ex @@ -29,6 +29,7 @@ defmodule Scopes.CSys do end def forward(msg, scope) do + #for s <- syns(scope), reduce: 0, do: (acc -> s.(msg); acc + 1) for s <- syns(scope), reduce: false, do: (_acc -> s.(msg)) #Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg); true end) end diff --git a/lib/csys/environ.ex b/lib/csys/environ.ex index 7a798cc..879f1a8 100644 --- a/lib/csys/environ.ex +++ b/lib/csys/environ.ex @@ -4,7 +4,7 @@ defmodule Scopes.CSys.Environ do alias Scopes.Shape defmodule State do - defstruct [neurons: [], init_seq: []] + defstruct [neurons: [], names: %{}, init_seq: []] end def setup({state, proc}, seq \\ []) do @@ -23,10 +23,12 @@ defmodule Scopes.CSys.Environ do end def process_creation(msg, {state, proc, syns, env}) do - new = Shape.data(msg).new + data = Shape.data(msg) + new = data.new neurons = [new | state.neurons] + names = data[:name] && Map.put(state.names, data.name, new) || state.names {step, seq} = List.pop_at(state.init_seq, 0) + CSys.update({%State{neurons: neurons, names: names, init_seq: seq}, proc, syns, env}) if step, do: step.(neurons) - CSys.update({%{state | neurons: neurons, init_seq: seq}, proc, syns, env}) end end diff --git a/lib/csys/program.ex b/lib/csys/program.ex index 2e00db0..12e5483 100644 --- a/lib/csys/program.ex +++ b/lib/csys/program.ex @@ -4,14 +4,15 @@ alias Scopes.CSys alias Scopes.Shape defmodule State do - defstruct [:value, :threshold, :bias, :stage, :prog] + defstruct [:value, :count, :bias, :threshold, :stage, :prog] end def prepare(prog, args) do state = %State{ value: args[:bias], - threshold: args[:threshold], + count: 0, bias: args[:bias], + threshold: args[:threshold], stage: :initial, prog: prog } @@ -132,15 +133,15 @@ alias Scopes.CSys # demo init sequences def init_seq_b1() do [ - fn [zero | _ns] -> CSys.send_message(zero, [:csys, :create, :pred], - %{op: [CSys.data_only(), negate()]}) end, - fn [one | _ns] -> CSys.send_message(one, [:csys, :create, :succ], - %{op: CSys.data_only()}) end, - fn [two | _ns] -> CSys.send_message(two, [:csys, :create, :pred]) end, - fn [three | others] -> - zero = List.last(others) - CSys.send_message(three, [:csys, :connect], %{target: zero}) - end - ] + fn [zero | _ns] -> CSys.send_message(zero, [:csys, :create, :pred], + %{op: [CSys.data_only(), negate()]}) end, + fn [one | _ns] -> CSys.send_message(one, [:csys, :create, :succ], + %{op: CSys.data_only()}) end, + fn [two | _ns] -> CSys.send_message(two, [:csys, :create, :pred]) end, + fn [three | others] -> + zero = List.last(others) + CSys.send_message(three, [:csys, :connect], %{target: zero}) + end + ] end end diff --git a/test/csys_test.exs b/test/csys_test.exs index fa7def2..c23ba26 100644 --- a/test/csys_test.exs +++ b/test/csys_test.exs @@ -25,14 +25,33 @@ defmodule Scopes.CSysTest do one = receive_data([:csys, :created]).new _two = receive_data([:csys, :created]).new three = receive_data([:csys, :created]).new - Process.sleep(5) + Process.sleep(50) CSys.send_value(one, 1) assert receive_data().value == 1 #assert receive_data().value in [-1, 1] CSys.send_value(three, 3) assert receive_data().value in [2, 3] assert receive_data().value in [2, 3] + refute_received msg, "unhandled message(s): #{inspect(msg)}" + end + end + + def init_recursive_1() do [ + fn [zero] -> + CSys.send_message(zero, [:csys, :connect], %{target: zero, op: Program.negate()}) + CSys.send_message(zero, [:csys, :create, :succ]) + end + ] + end + + describe "recursive:" do + test "zero + effector" do + _env = Environ.setup(Program.prepare_basic(), init_recursive_1()) + zero = receive_data([:csys, :created]).new + _eff = receive_data([:csys, :created]).new Process.sleep(50) + CSys.send_value(zero, 42) + assert receive_data().value == 42 refute_received msg, "unhandled message(s): #{inspect(msg)}" end end