diff --git a/lib/csys/program.ex b/lib/csys/program.ex index d16f9cc..85985c5 100644 --- a/lib/csys/program.ex +++ b/lib/csys/program.ex @@ -11,7 +11,7 @@ defmodule Scopes.CSys.Program do def prepare(prog, args) do state = %State{ - value: args[:bias], + value: args[:bias] || 0, count: 0, stage: :initial, prog: prog @@ -45,7 +45,7 @@ defmodule Scopes.CSys.Program do {stages, transitions} end - def prepare_basic(args \\ [threshold: 0, bias: 0]) do + def prepare_basic(args \\ []) do prepare(basic_prog(args), args) end @@ -59,13 +59,11 @@ defmodule Scopes.CSys.Program do basic(args) end - #def basic(msg, scope) do # + proc_ops - def basic(args) do # + proc_ops + def basic(args) do fn msg, scope -> 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, :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) @@ -78,26 +76,20 @@ defmodule Scopes.CSys.Program do # message handlers // proc steps - def process_value(_msg, _scope, _ops) 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 + def process_basic(msg, scope, args) do threshold = args[:threshold] || 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 - 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) - state_n = if bias, do: %{state | value: bias}, else: state + state_n = %{state | value: value_next} CSys.update(put_elem(scope, 0, state_n)) else state_n = %{state | value: value_n} @@ -105,16 +97,6 @@ defmodule Scopes.CSys.Program do 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 new = CSys.create(msg, restart(scope)) data = Shape.data(msg)