ericsteen

ericsteen

How to do a synchronous timed update tick in a GenServer

I am working on a Decentralized Autonomous System Framework (seeking contributors) to help bring Carl Hewitt’s/Stanford University’s vision of Scalable Intelligent Systems to fruition.

TLDR; The Automaton is a user-defined BT that is ticked ( the whole user-defined tree/subtree is ticked via the corresponding update function(sequence/selector) etc..) at regular intervals. The problem is that the update function for the behavior tree has to tick(update) the tree synchronously as it needs to perform a depth first traversal to check for status of each node to make decisions.

When I use Process.send_after it is printing the tree out of order. I am assuming it is due to asynchronicity because when I remove that call it ticks the tree in DFS order once. With the call to send_after it prints the first node again before ticking the entire tree. I have tried using handle_call but cannot send to self (tried the GenServer.reply trick to send a message via spawing a process but that is just faking a synchronous call).
I have read in several places that one should not use a recieve/after type loop in GenServer because it can mess up GenServer’s own receive loop underlying the callbacks.

Is there another way to achieve this or am I missing something?

defmodule Automaton do
  @moduledoc """
    This is the primary user control interface to the Automata system. The
    configration parameters are used to inject the appropriate modules into the
    user-defined nodes based on their node_type and other options.

    TODO: store any currently processing nodes so they can be ticked directly
    within the behaviour tree engine rather than per tick traversal of the entire
    tree. Zipper Tree?
  """

  alias Automaton.Behavior
  alias Automaton.CompositeServer
  alias Automaton.ComponentServer

  defmacro __using__(user_opts) do
    prepend =
      quote do
        # all nodes are Behavior's
        use Behavior, user_opts: unquote(user_opts)
      end

    c_types = CompositeServer.types()
    cn_types = ComponentServer.types()
    allowed_node_types = c_types ++ cn_types
    node_type = user_opts[:node_type]
    unless Enum.member?(allowed_node_types, node_type), do: raise("NodeTypeError")

    node_type =
      cond do
        Enum.member?(c_types, node_type) ->
          quote do: use(CompositeServer, user_opts: unquote(user_opts))

        Enum.member?(cn_types, node_type) ->
          quote do: use(ComponentServer, user_opts: unquote(user_opts))
      end

    control =
      quote do
        def tick(state) do
          new_state = if state.status != :bh_running, do: on_init(state), else: state

          status = update(new_state)

          if status != :bh_running do
            on_terminate(status)
          else
            schedule_next_tick(new_state.tick_freq)
          end

          [status, new_state]
        end

        def schedule_next_tick(ms_delay) do
          Process.send_after(self(), :scheduled_tick, ms_delay)
        end

        @impl GenServer
        def handle_call(:tick, _from, state) do
          [status, new_state] = tick(state)
          {:reply, status, new_state}
        end

        @impl GenServer
        def handle_info(:scheduled_tick, state) do
          [status, new_state] = tick(state)

          {:noreply, new_state}
        end
      end

    [prepend, node_type, control]
  end
end

Where the actual Composite that is printing out of order is as follows:

defmodule Automaton.Composite.Sequence do
  @moduledoc """
    Behavior for user-defined sequence actions. When the execution of a sequence
    node starts, then the node’s children are executed in succession from left
    to right, returning to its parent a status failure (or running) as soon as a
    child that returns failure (or running) is found. It returns success only
    when all the children return success. The purpose of the sequence node is to
    carry out the tasks that are defined by a strict sequence of sub-tasks, in
    which all have to succeed.

    A Sequence will return immediately with a failure status code when one of
    its children fails. As long as its children are succeeding, it will keep
    going. If it runs out of children, it will return in success.
  """
  alias Automaton.{Composite, Behavior}

  defmacro __using__(opts) do
    quote do
      @impl Behavior
      def update(%{workers: workers} = state) do
        {w, status} = tick_workers(workers)
        status
      end

      def tick_workers(workers) do
        Enum.reduce_while(workers, :ok, fn w, _acc ->
          status = GenServer.call(w, :tick, 10_000)

          # If the child fails, or keeps running, do the same.
          cond do
            status == :bh_running ->
              {:cont, {w, :bh_running}}

            status != :bh_success ->
              {:halt, {w, status}}
          end
        end)
      end
    end
  end
end

Thanks and please help us improve massively on computer-human interaction for the 2020’s!

Eric

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement