CSys.Program: use proc parameters (args) in proc closure instead of state

This commit is contained in:
Helmut Merz 2026-06-02 07:12:35 +02:00
parent 03c25342c4
commit 458861c9d3
2 changed files with 40 additions and 36 deletions

View file

@ -7,11 +7,12 @@ defmodule Scopes.CSys.Environ do
defstruct [cells: %{}, init_seq: []] defstruct [cells: %{}, init_seq: []]
end end
def setup({state, proc}, seq \\ []) do def setup({state, proc}, init_seq, proc_env \\ &proc_env/2) do
scope = {%State{init_seq: seq}, &proc_env/2, [], self()} env_scope = {%State{init_seq: init_seq}, proc_env, [], self()}
env = CSys.neuron(scope) env = CSys.neuron(env_scope)
CSys.create(Shape.create([:csys, :zero], data: %{addr: [:csys, :c00, {0, 0}]}), msg = Shape.create([:csys, :zero], data: %{addr: [:csys, :c00, {0, 0}]})
{state, proc, [], env}) proc = {state, proc, [], env}
CSys.create(msg, proc)
env env
end end

View file

@ -6,15 +6,13 @@ defmodule Scopes.CSys.Program do
alias Scopes.Shape alias Scopes.Shape
defmodule State do defmodule State do
defstruct [:value, :count, :bias, :threshold, :stage, :prog] defstruct [:value, :count, :stage, :prog]
end end
def prepare(prog, args) do def prepare(prog, args) do
state = %State{ state = %State{
value: args[:bias], value: args[:bias],
count: 0, count: 0,
bias: args[:bias],
threshold: args[:threshold],
stage: :initial, stage: :initial,
prog: prog prog: prog
} }
@ -31,11 +29,11 @@ defmodule Scopes.CSys.Program do
# basic program # basic program
def basic_prog() do def basic_prog(args) do
default = &basic_active/2 default = basic_active(args)
stages = %{ stages = %{
default: default, default: default,
initial: &basic_initial/2, initial: basic_initial(args),
active: default, active: default,
retired: default retired: default
} }
@ -48,22 +46,24 @@ defmodule Scopes.CSys.Program do
end end
def prepare_basic(args \\ [threshold: 0, bias: 0]) do def prepare_basic(args \\ [threshold: 0, bias: 0]) do
prepare(basic_prog(), args) prepare(basic_prog(args), args)
end end
# basic processors # basic processors
def basic_initial(msg, scope) do def basic_initial(args) do
basic(msg, scope) basic(args)
end end
def basic_active(msg, scope) do def basic_active(args) do
basic(msg, scope) basic(args)
end end
def basic(msg, scope) do # + proc_ops #def basic(msg, scope) do # + proc_ops
def basic(args) do # + proc_ops
fn msg, scope ->
case Shape.head(msg) do case Shape.head(msg) do
[:csys, :data | _rest] -> process_data(msg, scope) [:csys, :data | _rest] -> process_data(msg, scope, args)
#[:csys, :trigger | _rest] -> process_trigger(msg, scope) #[:csys, :trigger | _rest] -> process_trigger(msg, scope)
[:csys, :set | _rest] -> set(msg, scope) [:csys, :set | _rest] -> set(msg, scope)
[:csys, :connect | _rest] -> CSys.connect(msg, scope) [:csys, :connect | _rest] -> CSys.connect(msg, scope)
@ -74,27 +74,30 @@ defmodule Scopes.CSys.Program do
_ -> CSys.forward(msg, scope) || CSys.notify(msg, scope) _ -> CSys.forward(msg, scope) || CSys.notify(msg, scope)
end end
end end
end
# message handlers // proc steps # message handlers // proc steps
def process_value(_msg, _scope, _ops) do def process_value(_msg, _scope, _ops) do
#proc ops: #proc ops:
#map_op, (identity) #reduce, (&(&1 + &2))
#reduce_op, (+) #trigger_cond, (&(&1 > 0))
#trigger_cond, (>) #out_op, (&(&1)) # (&(rem(&1 - 1, 5) + 1)
#modifier_op, (identity) #output (&forward_or_notify)
#output_op (forward_or_notify) #next_op, (const(0)) # (&max(&1 - 5, 0))
end end
def process_data(msg, scope) do def process_data(msg, scope, args) do
data = Shape.data(msg) data = Shape.data(msg)
state = CSys.state(scope) state = CSys.state(scope)
value_n = state.value + data.value value_n = state.value + data.value
Logger.info data: data.value, state: state.value, threshold: state.threshold threshold = args[:threshold] || 0
if value_n >= state.threshold do 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}) msg = Shape.create(Shape.head(msg), data: %{data | value: value_n})
CSys.forward(msg, scope) || CSys.notify(msg, scope) 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)) CSys.update(put_elem(scope, 0, state_n))
else else
state_n = %{state | value: value_n} state_n = %{state | value: value_n}