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: []]
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

View file

@ -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}