Stiffstream / sobjectizer

An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.

Home Page:https://stiffstream.com/en/products/sobjectizer.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use so_default_state as a parent state for an another state

zamazan4ik opened this issue · comments

Hi!

Is it possible with SObjectizer to use default agent state (which is returned with so_default_state) as a parent state for another state? I am trying to do something like this:

state_t NotStarted{initial_substate_of(so_default_state()), "NotStarted"};

Unfortunately initial_substate_of takes a non-const reference but so_default_state returns const reference, so it doesn't compile.

Is there any workaround (except introducing own "default" state and making it as a parent for another events)? Is it a limitation by design?

Thank you.

Good question! :)

I think it's some heritage from ancient times. I don't think there are some reasons that prevent making agent_t::st_default non-const member of agent_t. I'll try to look deeper tomorrow (or Monday) and if there is no any other issues I'll change the st_default and add a new overload for so_default_state.

There is a hidden underwater rock: a call to on_enter handlers for child states. For example:

class demo final : public so_5::agent_t {
   state_t st_parent{this, "parent"};
   state_t st_child_1{initial_substate_of{st_parent}, "child_1"};
   state_t st_child_2{substate_of{st_parent}, "child_2"};
   ...
   void so_define_agent() override {
      st_child.on_enter([]{ std::cout << "Hello, World!" << std::endl; });
      ...
      this >>= st_parent;
   }
};

In that case "Hello, World" would be printed at the start of demo agent.

But in that case:

class demo final : public so_5::agent_t {
   state_t st_child_1{initial_substate_of{so_default_state()}, "child_1"};
   state_t st_child_2{substate_of{so_default_state()}, "child_2"};
   ...
   void so_define_agent() override {
      st_child.on_enter([]{ std::cout << "Hello, World!" << std::endl; });
      ...
   }
};

The enter handler for st_child won't be called at the start of demo agent. It's because st_default is already the current state for the agent and SObjectizer doesn't know about changes that should be taken into the account.

Hm... interesting case. In my opinion that's just "nice to know" thing, which shall be documented, if you want to allow so_default_state as a parent state in SObjectizer. In my case the example above doesn't matter at all.

For now locally I just created my own default state :)