From 458861c9d38f195a277e3f072401766afbf089bf Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Tue, 2 Jun 2026 07:12:35 +0200 Subject: [PATCH] CSys.Program: use proc parameters (args) in proc closure instead of state --- lib/csys/environ.ex | 11 ++++---- lib/csys/program.ex | 65 ++++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/lib/csys/environ.ex b/lib/csys/environ.ex index 5caf495..783124b 100644 --- a/lib/csys/environ.ex +++ b/lib/csys/environ.ex @@ -7,11 +7,12 @@ defmodule Scopes.CSys.Environ do defstruct [cells: %{}, init_seq: []] end - def setup({state, proc}, seq \\ []) do - scope = {%State{init_seq: seq}, &proc_env/2, [], self()} - env = CSys.neuron(scope) - CSys.create(Shape.create([:csys, :zero], data: %{addr: [:csys, :c00, {0, 0}]}), - {state, proc, [], env}) + def setup({state, proc}, init_seq, proc_env \\ &proc_env/2) do + env_scope = {%State{init_seq: init_seq}, proc_env, [], self()} + env = CSys.neuron(env_scope) + msg = Shape.create([:csys, :zero], data: %{addr: [:csys, :c00, {0, 0}]}) + proc = {state, proc, [], env} + CSys.create(msg, proc) env end diff --git a/lib/csys/program.ex b/lib/csys/program.ex index e2e2432..d16f9cc 100644 --- a/lib/csys/program.ex +++ b/lib/csys/program.ex @@ -6,15 +6,13 @@ defmodule Scopes.CSys.Program do alias Scopes.Shape defmodule State do - defstruct [:value, :count, :bias, :threshold, :stage, :prog] + defstruct [:value, :count, :stage, :prog] end def prepare(prog, args) do state = %State{ value: args[:bias], count: 0, - bias: args[:bias], - threshold: args[:threshold], stage: :initial, prog: prog } @@ -31,11 +29,11 @@ defmodule Scopes.CSys.Program do # basic program - def basic_prog() do - default = &basic_active/2 + def basic_prog(args) do + default = basic_active(args) stages = %{ default: default, - initial: &basic_initial/2, + initial: basic_initial(args), active: default, retired: default } @@ -48,30 +46,33 @@ defmodule Scopes.CSys.Program do end def prepare_basic(args \\ [threshold: 0, bias: 0]) do - prepare(basic_prog(), args) + prepare(basic_prog(args), args) end # basic processors - def basic_initial(msg, scope) do - basic(msg, scope) + def basic_initial(args) do + basic(args) end - def basic_active(msg, scope) do - basic(msg, scope) + def basic_active(args) do + basic(args) end - def basic(msg, scope) do # + proc_ops - case Shape.head(msg) do - [:csys, :data | _rest] -> process_data(msg, scope) - #[:csys, :trigger | _rest] -> process_trigger(msg, scope) - [:csys, :set | _rest] -> set(msg, scope) - [:csys, :connect | _rest] -> CSys.connect(msg, scope) - [:csys, :create, :succ | _rest] -> create_succ(msg, scope) - [:csys, :create, :pred | _rest] -> create_pred(msg, scope) - #[:csys, :next | _rest] -> next(scope, meta) - #[:csys, action | rest] -> transition(action, scope) - _ -> CSys.forward(msg, scope) || CSys.notify(msg, scope) + #def basic(msg, scope) do # + proc_ops + def basic(args) do # + proc_ops + fn msg, scope -> + case Shape.head(msg) do + [:csys, :data | _rest] -> process_data(msg, scope, args) + #[:csys, :trigger | _rest] -> process_trigger(msg, scope) + [:csys, :set | _rest] -> set(msg, scope) + [:csys, :connect | _rest] -> CSys.connect(msg, scope) + [:csys, :create, :succ | _rest] -> create_succ(msg, scope) + [:csys, :create, :pred | _rest] -> create_pred(msg, scope) + #[:csys, :next | _rest] -> next(scope, meta) + #[:csys, action | rest] -> transition(action, scope) + _ -> CSys.forward(msg, scope) || CSys.notify(msg, scope) + end end end @@ -79,22 +80,24 @@ defmodule Scopes.CSys.Program do def process_value(_msg, _scope, _ops) do #proc ops: - #map_op, (identity) - #reduce_op, (+) - #trigger_cond, (>) - #modifier_op, (identity) - #output_op (forward_or_notify) + #reduce, (&(&1 + &2)) + #trigger_cond, (&(&1 > 0)) + #out_op, (&(&1)) # (&(rem(&1 - 1, 5) + 1) + #output (&forward_or_notify) + #next_op, (const(0)) # (&max(&1 - 5, 0)) end - def process_data(msg, scope) do + def process_data(msg, scope, args) do data = Shape.data(msg) state = CSys.state(scope) value_n = state.value + data.value - Logger.info data: data.value, state: state.value, threshold: state.threshold - if value_n >= state.threshold do + threshold = args[:threshold] || 0 + bias = args[:bias] || 0 + Logger.info data: data.value, state: state.value, threshold: threshold + if value_n >= threshold do msg = Shape.create(Shape.head(msg), data: %{data | value: value_n}) CSys.forward(msg, scope) || CSys.notify(msg, scope) - state_n = if state.bias, do: %{state | value: state.bias}, else: state + state_n = if bias, do: %{state | value: bias}, else: state CSys.update(put_elem(scope, 0, state_n)) else state_n = %{state | value: value_n}