CSys.Program: introduce optional limit parameter

This commit is contained in:
Helmut Merz 2026-06-02 08:41:30 +02:00
parent 458861c9d3
commit 7dd2b1e137

View file

@ -11,7 +11,7 @@ defmodule Scopes.CSys.Program do
def prepare(prog, args) do def prepare(prog, args) do
state = %State{ state = %State{
value: args[:bias], value: args[:bias] || 0,
count: 0, count: 0,
stage: :initial, stage: :initial,
prog: prog prog: prog
@ -45,7 +45,7 @@ defmodule Scopes.CSys.Program do
{stages, transitions} {stages, transitions}
end end
def prepare_basic(args \\ [threshold: 0, bias: 0]) do def prepare_basic(args \\ []) do
prepare(basic_prog(args), args) prepare(basic_prog(args), args)
end end
@ -59,13 +59,11 @@ defmodule Scopes.CSys.Program do
basic(args) basic(args)
end end
#def basic(msg, scope) do # + proc_ops def basic(args) do
def basic(args) do # + proc_ops
fn msg, scope -> fn msg, scope ->
case Shape.head(msg) do case Shape.head(msg) do
[:csys, :data | _rest] -> process_data(msg, scope, args) [:csys, :data | _rest] -> process_basic(msg, scope, args)
#[:csys, :trigger | _rest] -> process_trigger(msg, scope) #[:csys, :trigger | _rest] -> process_trigger(msg, scope)
[:csys, :set | _rest] -> set(msg, scope)
[:csys, :connect | _rest] -> CSys.connect(msg, scope) [:csys, :connect | _rest] -> CSys.connect(msg, scope)
[:csys, :create, :succ | _rest] -> create_succ(msg, scope) [:csys, :create, :succ | _rest] -> create_succ(msg, scope)
[:csys, :create, :pred | _rest] -> create_pred(msg, scope) [:csys, :create, :pred | _rest] -> create_pred(msg, scope)
@ -78,26 +76,20 @@ defmodule Scopes.CSys.Program do
# message handlers // proc steps # message handlers // proc steps
def process_value(_msg, _scope, _ops) do def process_basic(msg, scope, args) do
#proc ops:
#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, args) do
data = Shape.data(msg)
state = CSys.state(scope)
value_n = state.value + data.value
threshold = args[:threshold] || 0 threshold = args[:threshold] || 0
bias = args[:bias] || 0 bias = args[:bias] || 0
Logger.info data: data.value, state: state.value, threshold: threshold limit = args[:limit]
data = Shape.data(msg)
state = CSys.state(scope)
Logger.info data: data, state_value: state.value, threshold: threshold
value_n = state.value + data.value
if value_n >= threshold do if value_n >= threshold do
msg = Shape.create(Shape.head(msg), data: %{data | value: value_n}) value_out = limit && rem(value_n - 1, limit) + limit || value_n
value_next = limit && max(value_n - limit, bias) || bias
msg = Shape.create(Shape.head(msg), data: %{data | value: value_out})
CSys.forward(msg, scope) || CSys.notify(msg, scope) CSys.forward(msg, scope) || CSys.notify(msg, scope)
state_n = if bias, do: %{state | value: bias}, else: state state_n = %{state | value: value_next}
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}
@ -105,16 +97,6 @@ defmodule Scopes.CSys.Program do
end end
end end
def set(msg, scope) do
data = Shape.data(msg)
state = CSys.state(scope)
value = data[:value] || state.value
threshold = data[:threshold] || state.threshold
bias = data[:bias] || state.bias
state_n = %{state | value: value, threshold: threshold, bias: bias}
CSys.update(put_elem(scope, 0, state_n))
end
def create_succ(msg, scope) do def create_succ(msg, scope) do
new = CSys.create(msg, restart(scope)) new = CSys.create(msg, restart(scope))
data = Shape.data(msg) data = Shape.data(msg)