SlideShare una empresa de Scribd logo
1 de 47
Descargar para leer sin conexión
Dive into SObjectizer-5.5
SObjectizer Team, Jan 2016
Second Part: Agent’s States
at v.5.5.15
The main features of SObjectizer-5.5, like agents,
cooperations, messages/signal, mboxes,
dispatchers and delayed delivery, were described
in the previous part.
The next important feature is agent’s state.
SObjectizer Team, Jan 2016
Agent in SObjectizer is a finite-state machine.
The behaviour of an agent depends on the current
state of the agent and the received message.
SObjectizer Team, Jan 2016
An agent can receive and process different messages
in each state. In other words an agent can receive a
message in one state but ignore it in another state.
Or, if an agent receives the same message in several
states, it can handle the message differently in each
state.
SObjectizer Team, Jan 2016
Where it can be useful?
SObjectizer Team, Jan 2016
Let’s imagine a simple agent which controls LED
indicator on some device.
It receives just one signal turn_on_off.
When LED indicator is off this signal should turn
indicator on. If LED indicator is on this signal should
turn indicator off.
SObjectizer Team, Jan 2016
This logic can be represented by simple statechart:
SObjectizer Team, Jan 2016
off on
enter/turn_led_on
exit/turn_led_off
turn_on_off
turn_on_off
It's easy to see that led_indicator agent requires two
states: off and on.
They can be directly expressed in C++ code via
usage of so_5::state_t class.
The definition of new state for an agent means
creation of new instance of state_t.
SObjectizer Team, Jan 2016
States are usually represented as members of agent’
s class:
class led_indicator final : public so_5::agent_t
{
state_t off{ this }, on{ this };
SObjectizer Team, Jan 2016
A state can have a textual name:
class led_indicator final : public so_5::agent_t
{
state_t off{ this, "off" }, on{ this, "on" };
It could be useful for debugging and logging.
SObjectizer Team, Jan 2016
There are several ways of changing agent’s state:
// Very old and basic way.
so_change_state( off );
// More modern and short way.
off.activate();
// Yet more modern way.
this >>= off;
The current state can be obtained via so_current_state()
method:
if( off == so_current_state() ) …
SObjectizer Team, Jan 2016
Every agent already has one state: the default one.
The default state can be accessed via
so_default_state() method:
// Returning agent to the default state.
this >>= so_default_state();
SObjectizer Team, Jan 2016
Even ad-hoc agents have the default state.
But it is the only one state they have.
Because there is no user-defined class for an ad-hoc
agent then there is no possibility to define new states
for ad-hoc agent. Thus there is no possibility to
change state of ad-hoc agent.
SObjectizer Team, Jan 2016
The most important part of usage of agent’s states is
subscription to a message with respect to a specific
state...
SObjectizer Team, Jan 2016
The simple usage of so_subscribe() and
so_subscribe_self() methods leads to subscription
only for the default agent’s state.
It means that:
so_subscribe_self().event(…);
is the equivalent of:
so_subscribe_self().in( so_default_state() ).event(…);
SObjectizer Team, Jan 2016
To make subscription to a message for a specific
state it is necessary to use in() method in a
subscription chain:
so_subscribe_self().in( off ).event(…);
so_subscribe_self().in( on ).event(…);
SObjectizer Team, Jan 2016
The in() methods can be chained if the same event
handler is used in several states:
so_subscribe_self()
.in( off )
.in( on )
.event< get_name >( [] -> std::string { return "led_indicator"; } );
so_subscribe_self()
.in( off )
.event< get_status >( [] -> std::string { return "off"; } );
SObjectizer Team, Jan 2016
There is another way to make subscription for a
specific state:
off.event< get_name >( [] -> std::string { return "led_indicator"; } )
.event< get_status >( [] -> std::string { return "off"; } );
on.event< get_name >( [] -> std::string { return "led_indicator"; } )
.event(...)
...;
SObjectizer Team, Jan 2016
There are also on_enter/on_exit methods in state_t class.
Method on_enter sets up an enter handler. Enter handler
is automatically called when the state is activated.
Contrary on_exit method sets an exit handler. Exit handler
is automatically called when the state is activated.
Enter and exit handler can be a lambda-function or a
pointer to member method of agent's class.
SObjectizer Team, Jan 2016
In the led_indicator example enter and exit handlers
are necessary for on state:
on.on_enter( [this]{ /* some device-dependent code */ } )
.on_exit( [this]{ /* some device-dependent code */ } )
...
SObjectizer Team, Jan 2016
Let’s see a full example of led_indicator agent.
SObjectizer Team, Jan 2016
The full led_indicator agent code:
class led_indicator final : public so_5::agent_t
{
state_t off{ this, "off" }, on{ this, "on" };
public :
struct turn_on_off : public so_5::signal_t {};
led_indicator( context_t ctx ) : so_5::agent_t{ ctx }
{
this >>= off;
off.event< turn_on_off >( [this]{ this >>= on; } );
on.on_enter( [this]{ /* some device-dependent code */ } )
.on_exit( [this]{ /* some device-dependent code */ } )
.event< turn_on_off >( [this]{ this >>= off; } );
}
};
SObjectizer Team, Jan 2016
A few more things must be mentioned in the code
above...
SObjectizer Team, Jan 2016
The first one is the place where all subscription is
defined.
In led_indicator example agent definition is performed in
the agent's constructor.
Usually all agent definition is done in so_define_agent()
method. It has sense if class inheritance is used. But
led_indicator is final class so we can do all these actions
in led_indicator's constructor.
SObjectizer Team, Jan 2016
The second one is switching of led_indicator agent to
off state.
By default the initial state of any agent is the default state
(which is accessible via so_default_state() method). But
we need to start led_indicator agent in off state. So we
change agent's state at the very beginning.
SObjectizer Team, Jan 2016
The third one is the guarantee of calling exit handler
for state.
If an agent enters into state S and state S has exit handler
then SObjectizer guarantees the invocation of that exit
handler in case of:
● switching of agent to different state;
● so_evt_finish() for agent is called (as a result of
agent's coop deregistration or SObjectizer
Environment shutdown).
SObjectizer Team, Jan 2016
The example shown above demonstrate simple finite-
state machine.
Since v.5.5.15 SObjectizer supports more advanced
features of agents' states like composite states,
shallow- and deep-history, time limitations and so on.
These advanced features allow to implement agents
as hierarchical state machines.
SObjectizer Team, Jan 2016
Let's see an ability to create hierarchical state
machines on a slightly complex example: an agent
which implements blinking of LED indicator.
This agent receives `turn_on_off` signal for turning
blinking on and off. When blinking is turned on then
agent switches LED indicator on for 1s then switches
it off for 1s then switches on again and so on until the
agent receives next `turn_on_off` signal.
SObjectizer Team, Jan 2016
A statechart for that agent can be represented as:
SObjectizer Team, Jan 2016
off
blinking
enter/turn_timer_on
exit/turn_timer_off
turn_on_off
turn_on_off
blink_on
enter/turn_led_on
exit/turn_led_off
blink_off
timer
timer
Declaration of agent's states:
class blinking_led final : public so_5::agent_t
{
state_t off{ this, "off" },
blinking{ this, "blinking" },
blink_on{ initial_substate_of{ blinking }, "on" },
blink_off{ substate_of{ blinking }, "off" };
Agent has two top level states: off and blinking.
State blinking is a composite state with two substates:
blink_on and blink_off.
SObjectizer Team, Jan 2016
Substate blink_on is marked as initial substate of
composite state blinking.
It means that when state blinking is activated then
substate blink_on is activated too.
Moreover so_current_state() method will return a
reference to blink_on, not to blinking. It is because
so_current_state() always returns a reference to a leaf
state in agent's state tree.
SObjectizer Team, Jan 2016
Every composite state must have the initial substate.
It means that exactly one substate must be declared by
using initial_substate_of indicator:
blink_on{ initial_substate_of{ blinking }, "on" },
blink_off{ substate_of{ blinking }, "off" };
An attempt to activate a composite state without the initial
substate defined will lead to an error at run-time.
SObjectizer Team, Jan 2016
Behaviour of a state can be defined anywhere in the
agent's code. Usually it is done in agent's constructor
or in so_define_agent() method.
Definition of state's behaviour inside so_define_agent() is
preferable if inheritance of agent's classes is required or
indented.
In this example inheritance is not used so we define
behaviour of agent's state in the agent's constructor:
SObjectizer Team, Jan 2016
Definition of states behaviour:
off
.event< turn_on_off >( [this]{ this >>= blinking; } );
blinking
.on_enter( [this] {
m_timer = so_5::send_periodic< timer >(
*this, std::chrono::seconds::zero(), std::chrono::seconds{1} );
} )
.on_exit( [this]{ m_timer.release(); } )
.event< turn_on_off >( [this]{ this >>= off; } );
blink_on
.on_enter( &blinking_led::led_on )
.on_exit( &blinking_led::led_off )
.event< timer >( [this]{ this >>= blink_off; } );
blink_off
.event< timer >( [this]{ this >>= blink_on; } );
SObjectizer Team, Jan 2016
It's easy to see that many events look like:
event<Message>([this] { this >>= State; });
This is very typical case in complex statecharts.
There is a special method just_switch_to() which can
simplify such cases. By using this method we can rewrite
states behaviour that way:
SObjectizer Team, Jan 2016
Definition of states behaviour with just_switch_to:
off
.just_switch_to< turn_on_off >( blinking );
blinking
.on_enter( [this] {
m_timer = so_5::send_periodic< timer >(
*this, std::chrono::seconds::zero(), std::chrono::seconds{1} );
} )
.on_exit( [this]{ m_timer.release(); } )
.just_switch_to< turn_on_off >( off );
blink_on
.on_enter( &blinking_led::led_on )
.on_exit( &blinking_led::led_off )
.just_switch_to< timer >( blink_off );
blink_off
.just_switch_to< timer >( blink_on );
SObjectizer Team, Jan 2016
Note that reaction to turn_on_off signal is defined
only in off and blinking states.
There are no such handles in substates blink_on and
blink_off.
It is not necessary because substates inherit event
handlers from their parent states.
SObjectizer Team, Jan 2016
Inheritance of event handlers means that event
handler will be searched in the current state, then in
its parent state, then in its parent state and so on...
In blinking_led agent an event handler for turn_on_off
signal will be searched in blink_on and blink_off states
and then in blinking state.
That is why we don't need to create subscription for
turn_on_off in blink_on and blink_off states.
SObjectizer Team, Jan 2016
The full source for blinking_led agent (1/3):
class blinking_led final : public so_5::agent_t
{
state_t
off{ this, "off" },
blinking{ this, "blinking" },
blink_on{ initial_substate_of{ blinking }, "on" },
blink_off{ substate_of{ blinking }, "off" };
struct timer : public so_5::signal_t {};
public :
struct turn_on_off : public so_5::signal_t {};
SObjectizer Team, Jan 2016
The full source for blinking_led agent (2/3):
blinking_led( context_t ctx ) : so_5::agent_t{ ctx }
{
this >>= off;
off
.just_switch_to< turn_on_off >( blinking );
blinking
.on_enter( [this] {
m_timer = so_5::send_periodic< timer >(
*this, std::chrono::seconds::zero(), std::chrono::seconds{1} );
} )
.on_exit( [this]{ m_timer.release(); } )
.just_switch_to< turn_on_off >( off );
SObjectizer Team, Jan 2016
The full source for blinking_led agent (3/3):
blink_on
.on_enter( &blinking_led::led_on )
.on_exit( &blinking_led::led_off )
.just_switch_to< timer >( blink_off );
blink_off
.just_switch_to< timer >( blink_on );
}
private :
so_5::timer_id_t m_timer;
void led_on() { /* some device-dependent code */ }
void led_off() { /* some device-dependent code */ }
};
SObjectizer Team, Jan 2016
There are several other features which simplify
implementation of complex state machines.
One of them can simplify blinking_led example show
above.
It is time_limit for agent's state.
SObjectizer Team, Jan 2016
Let's imagine that blinking_led agent have to switch
LED on for 1.5s and then switch it off for 0.7s. How
can we do that?
The obvious way is to use delayed signals with different
timeouts in enter handlers for blink_on and blink_off
states. But this way is not very easy in fact...
Usage of time_limit is much simpler.
SObjectizer Team, Jan 2016
Just remove timer signal and m_timer_id from blinking_led
agent and rewrite states behaviour that way:
off
.just_switch_to< turn_on_off >( blinking );
blinking
.just_switch_to< turn_on_off >( off );
blink_on
.on_enter( &blinking_led::led_on )
.on_exit( &blinking_led::led_off )
.time_limit( std::chrono::milliseconds{1500}, blink_off );
blink_off
.time_limit( std::chrono::milliseconds{750}, blink_on );
SObjectizer Team, Jan 2016
The time_limit feature dictates SObjectizer to limit
time spent in the specific state.
A clause like:
SomeState.time_limit(Timeout, AnotherState);
tells the SObjectizer that agent must be automatically
switched from SomeState to AnotherState after Timeout
spent in SomeState.
SObjectizer Team, Jan 2016
The blinking_led example shows just few advanced
features of agent's states.
Some features like shallow- and deep-history and
transfer_to_state/suppress methods are not described
here. Please see the more detailed description of these
features in the Project's Wiki.
SObjectizer Team, Jan 2016
Additional Information:
Project’s home: http://sourceforge.net/projects/sobjectizer
Documentation: http://sourceforge.net/p/sobjectizer/wiki/
Forum: http://sourceforge.net/p/sobjectizer/discussion/
Google-group: https://groups.google.com/forum/#!forum/sobjectizer
GitHub mirror: https://github.com/masterspline/SObjectizer

Más contenido relacionado

Más de Yauheni Akhotnikau

arataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world examplearataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world exampleYauheni Akhotnikau
 
Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)Yauheni Akhotnikau
 
What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)Yauheni Akhotnikau
 
What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)Yauheni Akhotnikau
 
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...Yauheni Akhotnikau
 
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...Yauheni Akhotnikau
 
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...Yauheni Akhotnikau
 
Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?Yauheni Akhotnikau
 
25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My Eyes25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My EyesYauheni Akhotnikau
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allYauheni Akhotnikau
 
Dive into SObjectizer 5.5. Tenth part: Mutable Messages
Dive into SObjectizer 5.5. Tenth part: Mutable MessagesDive into SObjectizer 5.5. Tenth part: Mutable Messages
Dive into SObjectizer 5.5. Tenth part: Mutable MessagesYauheni Akhotnikau
 
Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Yauheni Akhotnikau
 
Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++Yauheni Akhotnikau
 
Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?Yauheni Akhotnikau
 
Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?Yauheni Akhotnikau
 
Dive into SObjectizer 5.5. Eighth Part: Dispatchers
Dive into SObjectizer 5.5. Eighth Part: DispatchersDive into SObjectizer 5.5. Eighth Part: Dispatchers
Dive into SObjectizer 5.5. Eighth Part: DispatchersYauheni Akhotnikau
 
What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9Yauheni Akhotnikau
 
Dive into SObjectizer 5.5. Seventh part: Message Limits
Dive into SObjectizer 5.5. Seventh part: Message LimitsDive into SObjectizer 5.5. Seventh part: Message Limits
Dive into SObjectizer 5.5. Seventh part: Message LimitsYauheni Akhotnikau
 
Погружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная частьПогружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная частьYauheni Akhotnikau
 

Más de Yauheni Akhotnikau (20)

arataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world examplearataga. SObjectizer and RESTinio in action: a real-world example
arataga. SObjectizer and RESTinio in action: a real-world example
 
Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)Actor Model and C++: what, why and how? (March 2020 Edition)
Actor Model and C++: what, why and how? (March 2020 Edition)
 
What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)What is SObjectizer 5.7 (at v.5.7.0)
What is SObjectizer 5.7 (at v.5.7.0)
 
What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)What is SObjectizer 5.6 (at v.5.6.0)
What is SObjectizer 5.6 (at v.5.6.0)
 
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
[C++ CoreHard Autumn 2018] Actors vs CSP vs Task...
 
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
Shrimp: A Rather Practical Example Of Application Development With RESTinio a...
 
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
Акторы в C++: взгляд старого практикующего актородела (St. Petersburg C++ Use...
 
Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?Акторы на C++: стоило ли оно того?
Акторы на C++: стоило ли оно того?
 
25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My Eyes25 Years of C++ History Flashed in Front of My Eyes
25 Years of C++ History Flashed in Front of My Eyes
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
 
Dive into SObjectizer 5.5. Tenth part: Mutable Messages
Dive into SObjectizer 5.5. Tenth part: Mutable MessagesDive into SObjectizer 5.5. Tenth part: Mutable Messages
Dive into SObjectizer 5.5. Tenth part: Mutable Messages
 
Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?
 
Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++Шишки, набитые за 15 лет использования акторов в C++
Шишки, набитые за 15 лет использования акторов в C++
 
Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?Для чего мы делали свой акторный фреймворк и что из этого вышло?
Для чего мы делали свой акторный фреймворк и что из этого вышло?
 
Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?Модель акторов и C++ что, зачем и как?
Модель акторов и C++ что, зачем и как?
 
Dive into SObjectizer 5.5. Eighth Part: Dispatchers
Dive into SObjectizer 5.5. Eighth Part: DispatchersDive into SObjectizer 5.5. Eighth Part: Dispatchers
Dive into SObjectizer 5.5. Eighth Part: Dispatchers
 
What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9
 
Dive into SObjectizer 5.5. Seventh part: Message Limits
Dive into SObjectizer 5.5. Seventh part: Message LimitsDive into SObjectizer 5.5. Seventh part: Message Limits
Dive into SObjectizer 5.5. Seventh part: Message Limits
 
Погружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная частьПогружение в SObjectizer 5.5. Вводная часть
Погружение в SObjectizer 5.5. Вводная часть
 
Обзор SObjectizer 5.5
Обзор SObjectizer 5.5Обзор SObjectizer 5.5
Обзор SObjectizer 5.5
 

Último

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 

Último (20)

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 

Dive into SObjectizer 5.5. Second part. States

  • 1. Dive into SObjectizer-5.5 SObjectizer Team, Jan 2016 Second Part: Agent’s States at v.5.5.15
  • 2. The main features of SObjectizer-5.5, like agents, cooperations, messages/signal, mboxes, dispatchers and delayed delivery, were described in the previous part. The next important feature is agent’s state. SObjectizer Team, Jan 2016
  • 3. Agent in SObjectizer is a finite-state machine. The behaviour of an agent depends on the current state of the agent and the received message. SObjectizer Team, Jan 2016
  • 4. An agent can receive and process different messages in each state. In other words an agent can receive a message in one state but ignore it in another state. Or, if an agent receives the same message in several states, it can handle the message differently in each state. SObjectizer Team, Jan 2016
  • 5. Where it can be useful? SObjectizer Team, Jan 2016
  • 6. Let’s imagine a simple agent which controls LED indicator on some device. It receives just one signal turn_on_off. When LED indicator is off this signal should turn indicator on. If LED indicator is on this signal should turn indicator off. SObjectizer Team, Jan 2016
  • 7. This logic can be represented by simple statechart: SObjectizer Team, Jan 2016 off on enter/turn_led_on exit/turn_led_off turn_on_off turn_on_off
  • 8. It's easy to see that led_indicator agent requires two states: off and on. They can be directly expressed in C++ code via usage of so_5::state_t class. The definition of new state for an agent means creation of new instance of state_t. SObjectizer Team, Jan 2016
  • 9. States are usually represented as members of agent’ s class: class led_indicator final : public so_5::agent_t { state_t off{ this }, on{ this }; SObjectizer Team, Jan 2016
  • 10. A state can have a textual name: class led_indicator final : public so_5::agent_t { state_t off{ this, "off" }, on{ this, "on" }; It could be useful for debugging and logging. SObjectizer Team, Jan 2016
  • 11. There are several ways of changing agent’s state: // Very old and basic way. so_change_state( off ); // More modern and short way. off.activate(); // Yet more modern way. this >>= off; The current state can be obtained via so_current_state() method: if( off == so_current_state() ) … SObjectizer Team, Jan 2016
  • 12. Every agent already has one state: the default one. The default state can be accessed via so_default_state() method: // Returning agent to the default state. this >>= so_default_state(); SObjectizer Team, Jan 2016
  • 13. Even ad-hoc agents have the default state. But it is the only one state they have. Because there is no user-defined class for an ad-hoc agent then there is no possibility to define new states for ad-hoc agent. Thus there is no possibility to change state of ad-hoc agent. SObjectizer Team, Jan 2016
  • 14. The most important part of usage of agent’s states is subscription to a message with respect to a specific state... SObjectizer Team, Jan 2016
  • 15. The simple usage of so_subscribe() and so_subscribe_self() methods leads to subscription only for the default agent’s state. It means that: so_subscribe_self().event(…); is the equivalent of: so_subscribe_self().in( so_default_state() ).event(…); SObjectizer Team, Jan 2016
  • 16. To make subscription to a message for a specific state it is necessary to use in() method in a subscription chain: so_subscribe_self().in( off ).event(…); so_subscribe_self().in( on ).event(…); SObjectizer Team, Jan 2016
  • 17. The in() methods can be chained if the same event handler is used in several states: so_subscribe_self() .in( off ) .in( on ) .event< get_name >( [] -> std::string { return "led_indicator"; } ); so_subscribe_self() .in( off ) .event< get_status >( [] -> std::string { return "off"; } ); SObjectizer Team, Jan 2016
  • 18. There is another way to make subscription for a specific state: off.event< get_name >( [] -> std::string { return "led_indicator"; } ) .event< get_status >( [] -> std::string { return "off"; } ); on.event< get_name >( [] -> std::string { return "led_indicator"; } ) .event(...) ...; SObjectizer Team, Jan 2016
  • 19. There are also on_enter/on_exit methods in state_t class. Method on_enter sets up an enter handler. Enter handler is automatically called when the state is activated. Contrary on_exit method sets an exit handler. Exit handler is automatically called when the state is activated. Enter and exit handler can be a lambda-function or a pointer to member method of agent's class. SObjectizer Team, Jan 2016
  • 20. In the led_indicator example enter and exit handlers are necessary for on state: on.on_enter( [this]{ /* some device-dependent code */ } ) .on_exit( [this]{ /* some device-dependent code */ } ) ... SObjectizer Team, Jan 2016
  • 21. Let’s see a full example of led_indicator agent. SObjectizer Team, Jan 2016
  • 22. The full led_indicator agent code: class led_indicator final : public so_5::agent_t { state_t off{ this, "off" }, on{ this, "on" }; public : struct turn_on_off : public so_5::signal_t {}; led_indicator( context_t ctx ) : so_5::agent_t{ ctx } { this >>= off; off.event< turn_on_off >( [this]{ this >>= on; } ); on.on_enter( [this]{ /* some device-dependent code */ } ) .on_exit( [this]{ /* some device-dependent code */ } ) .event< turn_on_off >( [this]{ this >>= off; } ); } }; SObjectizer Team, Jan 2016
  • 23. A few more things must be mentioned in the code above... SObjectizer Team, Jan 2016
  • 24. The first one is the place where all subscription is defined. In led_indicator example agent definition is performed in the agent's constructor. Usually all agent definition is done in so_define_agent() method. It has sense if class inheritance is used. But led_indicator is final class so we can do all these actions in led_indicator's constructor. SObjectizer Team, Jan 2016
  • 25. The second one is switching of led_indicator agent to off state. By default the initial state of any agent is the default state (which is accessible via so_default_state() method). But we need to start led_indicator agent in off state. So we change agent's state at the very beginning. SObjectizer Team, Jan 2016
  • 26. The third one is the guarantee of calling exit handler for state. If an agent enters into state S and state S has exit handler then SObjectizer guarantees the invocation of that exit handler in case of: ● switching of agent to different state; ● so_evt_finish() for agent is called (as a result of agent's coop deregistration or SObjectizer Environment shutdown). SObjectizer Team, Jan 2016
  • 27. The example shown above demonstrate simple finite- state machine. Since v.5.5.15 SObjectizer supports more advanced features of agents' states like composite states, shallow- and deep-history, time limitations and so on. These advanced features allow to implement agents as hierarchical state machines. SObjectizer Team, Jan 2016
  • 28. Let's see an ability to create hierarchical state machines on a slightly complex example: an agent which implements blinking of LED indicator. This agent receives `turn_on_off` signal for turning blinking on and off. When blinking is turned on then agent switches LED indicator on for 1s then switches it off for 1s then switches on again and so on until the agent receives next `turn_on_off` signal. SObjectizer Team, Jan 2016
  • 29. A statechart for that agent can be represented as: SObjectizer Team, Jan 2016 off blinking enter/turn_timer_on exit/turn_timer_off turn_on_off turn_on_off blink_on enter/turn_led_on exit/turn_led_off blink_off timer timer
  • 30. Declaration of agent's states: class blinking_led final : public so_5::agent_t { state_t off{ this, "off" }, blinking{ this, "blinking" }, blink_on{ initial_substate_of{ blinking }, "on" }, blink_off{ substate_of{ blinking }, "off" }; Agent has two top level states: off and blinking. State blinking is a composite state with two substates: blink_on and blink_off. SObjectizer Team, Jan 2016
  • 31. Substate blink_on is marked as initial substate of composite state blinking. It means that when state blinking is activated then substate blink_on is activated too. Moreover so_current_state() method will return a reference to blink_on, not to blinking. It is because so_current_state() always returns a reference to a leaf state in agent's state tree. SObjectizer Team, Jan 2016
  • 32. Every composite state must have the initial substate. It means that exactly one substate must be declared by using initial_substate_of indicator: blink_on{ initial_substate_of{ blinking }, "on" }, blink_off{ substate_of{ blinking }, "off" }; An attempt to activate a composite state without the initial substate defined will lead to an error at run-time. SObjectizer Team, Jan 2016
  • 33. Behaviour of a state can be defined anywhere in the agent's code. Usually it is done in agent's constructor or in so_define_agent() method. Definition of state's behaviour inside so_define_agent() is preferable if inheritance of agent's classes is required or indented. In this example inheritance is not used so we define behaviour of agent's state in the agent's constructor: SObjectizer Team, Jan 2016
  • 34. Definition of states behaviour: off .event< turn_on_off >( [this]{ this >>= blinking; } ); blinking .on_enter( [this] { m_timer = so_5::send_periodic< timer >( *this, std::chrono::seconds::zero(), std::chrono::seconds{1} ); } ) .on_exit( [this]{ m_timer.release(); } ) .event< turn_on_off >( [this]{ this >>= off; } ); blink_on .on_enter( &blinking_led::led_on ) .on_exit( &blinking_led::led_off ) .event< timer >( [this]{ this >>= blink_off; } ); blink_off .event< timer >( [this]{ this >>= blink_on; } ); SObjectizer Team, Jan 2016
  • 35. It's easy to see that many events look like: event<Message>([this] { this >>= State; }); This is very typical case in complex statecharts. There is a special method just_switch_to() which can simplify such cases. By using this method we can rewrite states behaviour that way: SObjectizer Team, Jan 2016
  • 36. Definition of states behaviour with just_switch_to: off .just_switch_to< turn_on_off >( blinking ); blinking .on_enter( [this] { m_timer = so_5::send_periodic< timer >( *this, std::chrono::seconds::zero(), std::chrono::seconds{1} ); } ) .on_exit( [this]{ m_timer.release(); } ) .just_switch_to< turn_on_off >( off ); blink_on .on_enter( &blinking_led::led_on ) .on_exit( &blinking_led::led_off ) .just_switch_to< timer >( blink_off ); blink_off .just_switch_to< timer >( blink_on ); SObjectizer Team, Jan 2016
  • 37. Note that reaction to turn_on_off signal is defined only in off and blinking states. There are no such handles in substates blink_on and blink_off. It is not necessary because substates inherit event handlers from their parent states. SObjectizer Team, Jan 2016
  • 38. Inheritance of event handlers means that event handler will be searched in the current state, then in its parent state, then in its parent state and so on... In blinking_led agent an event handler for turn_on_off signal will be searched in blink_on and blink_off states and then in blinking state. That is why we don't need to create subscription for turn_on_off in blink_on and blink_off states. SObjectizer Team, Jan 2016
  • 39. The full source for blinking_led agent (1/3): class blinking_led final : public so_5::agent_t { state_t off{ this, "off" }, blinking{ this, "blinking" }, blink_on{ initial_substate_of{ blinking }, "on" }, blink_off{ substate_of{ blinking }, "off" }; struct timer : public so_5::signal_t {}; public : struct turn_on_off : public so_5::signal_t {}; SObjectizer Team, Jan 2016
  • 40. The full source for blinking_led agent (2/3): blinking_led( context_t ctx ) : so_5::agent_t{ ctx } { this >>= off; off .just_switch_to< turn_on_off >( blinking ); blinking .on_enter( [this] { m_timer = so_5::send_periodic< timer >( *this, std::chrono::seconds::zero(), std::chrono::seconds{1} ); } ) .on_exit( [this]{ m_timer.release(); } ) .just_switch_to< turn_on_off >( off ); SObjectizer Team, Jan 2016
  • 41. The full source for blinking_led agent (3/3): blink_on .on_enter( &blinking_led::led_on ) .on_exit( &blinking_led::led_off ) .just_switch_to< timer >( blink_off ); blink_off .just_switch_to< timer >( blink_on ); } private : so_5::timer_id_t m_timer; void led_on() { /* some device-dependent code */ } void led_off() { /* some device-dependent code */ } }; SObjectizer Team, Jan 2016
  • 42. There are several other features which simplify implementation of complex state machines. One of them can simplify blinking_led example show above. It is time_limit for agent's state. SObjectizer Team, Jan 2016
  • 43. Let's imagine that blinking_led agent have to switch LED on for 1.5s and then switch it off for 0.7s. How can we do that? The obvious way is to use delayed signals with different timeouts in enter handlers for blink_on and blink_off states. But this way is not very easy in fact... Usage of time_limit is much simpler. SObjectizer Team, Jan 2016
  • 44. Just remove timer signal and m_timer_id from blinking_led agent and rewrite states behaviour that way: off .just_switch_to< turn_on_off >( blinking ); blinking .just_switch_to< turn_on_off >( off ); blink_on .on_enter( &blinking_led::led_on ) .on_exit( &blinking_led::led_off ) .time_limit( std::chrono::milliseconds{1500}, blink_off ); blink_off .time_limit( std::chrono::milliseconds{750}, blink_on ); SObjectizer Team, Jan 2016
  • 45. The time_limit feature dictates SObjectizer to limit time spent in the specific state. A clause like: SomeState.time_limit(Timeout, AnotherState); tells the SObjectizer that agent must be automatically switched from SomeState to AnotherState after Timeout spent in SomeState. SObjectizer Team, Jan 2016
  • 46. The blinking_led example shows just few advanced features of agent's states. Some features like shallow- and deep-history and transfer_to_state/suppress methods are not described here. Please see the more detailed description of these features in the Project's Wiki. SObjectizer Team, Jan 2016
  • 47. Additional Information: Project’s home: http://sourceforge.net/projects/sobjectizer Documentation: http://sourceforge.net/p/sobjectizer/wiki/ Forum: http://sourceforge.net/p/sobjectizer/discussion/ Google-group: https://groups.google.com/forum/#!forum/sobjectizer GitHub mirror: https://github.com/masterspline/SObjectizer