yasmine
state_machine_base.hpp
Go to the documentation of this file.
1 // //
3 // This file is part of the Seadex yasmine ecosystem (http://yasmine.seadex.de). //
4 // Copyright (C) 2016-2017 Seadex GmbH //
5 // //
6 // Licensing information is available in the folder "license" which is part of this distribution. //
7 // The same information is available on the www @ http://yasmine.seadex.de/Licenses.html. //
8 // //
10 
11 
12 #ifndef STATE_MACHINE_BASE_484EA235_EEDE_4C19_9C46_C0687D950A13
13 #define STATE_MACHINE_BASE_484EA235_EEDE_4C19_9C46_C0687D950A13
14 
15 
16 #include "essentials/compatibility/thread.hpp"
17 
19 #include "event_collector.hpp"
20 #include "interruptible.hpp"
21 #include "transition_kind.hpp"
22 #include "constraint_fwd.hpp"
23 #include "behavior_fwd.hpp"
24 #include "transition_fwd.hpp"
25 #include "composite_state_fwd.hpp"
26 #include "region_fwd.hpp"
27 #include "event_id.hpp"
29 
30 
31 namespace sxy
32 {
33 
34 
35  class vertex;
36  class async_event_handler;
37  class event_processing_callback;
38  class uri;
39  class region;
40 
41 
47  {
48  public:
53  explicit state_machine_base( const std::string& _name,
54  event_processing_callback* const _event_processing_callback = SX_NULLPTR );
55  virtual ~state_machine_base() SX_NOEXCEPT SX_OVERRIDE;
56  SX_NO_COPY( state_machine_base )
57 
58 
62 
63 #ifdef Y_PROFILER
64  sxe::uint32_t get_number_of_processed_events() const;
65 #endif
66 
70  virtual transition& add_transition( transition_uptr _transition );
71 
81  virtual transition& add_transition( const event_id _event_id, vertex& _source, vertex& _target,
83  const constraint_function& _guard = constraint_function(),
84  const behavior_function& _behavior = behavior_function() );
85 
95  virtual transition& add_transition( const event_ids& _event_ids, vertex& _source, vertex& _target,
97  const constraint_function& _guard = constraint_function(),
98  const behavior_function& _behavior = behavior_function() );
99 
108  virtual transition& add_transition( const event_id _event_id, vertex& _source, vertex& _target,
110 
119  virtual transition& add_transition( const event_ids& _event_ids, vertex& _source, vertex& _target,
121 
131  virtual transition& add_transition( const event_id _event_id, vertex& _source, vertex& _target,
132  const constraint_function& _guard, const behavior_function& _behavior,
134 
144  virtual transition& add_transition( const event_ids& _event_ids, vertex& _source, vertex& _target,
145  const constraint_function& _guard, const behavior_function& _behavior,
147 
156  virtual transition& add_transition( const event_id _event_id, vertex& _source, vertex& _target,
157  const behavior_function& _behavior, const sxy::transition_kind _kind = transition_kind::EXTERNAL );
158 
167  virtual transition& add_transition( const event_ids& _event_ids, vertex& _source, vertex& _target,
168  const behavior_function& _behavior, const sxy::transition_kind _kind = transition_kind::EXTERNAL );
169  virtual bool fire_event( const event_sptr& _event ) = 0;
170 
174  bool check( state_machine_defects& _defects ) const;
175 
176  virtual bool run() = 0;
177 
179  virtual void halt();
180 
183  virtual void interrupt();
184 
188  virtual bool is_interrupted() const SX_OVERRIDE;
189 
192  virtual void set_behavior_of_unhandled_event_handler( const behavior_function& _behavior );
193 
196  virtual std::string get_name() const;
197 
198 
199  protected:
206  bool run( async_event_handler* const _async_event_handler );
207  bool process_event( const event_sptr& _event, async_event_handler* const _async_event_handler );
208  static void stop_all_async_states( state& _state );
209 
210 
211  private:
212  virtual const events& get_deferred_events() const SX_OVERRIDE;
213  virtual raw_const_states get_active_state_configuration() const SX_OVERRIDE;
214  void get_active_states_from_region( raw_const_states& _active_state_configuration, const region& _region ) const;
215  void check_regions_for_active_states( raw_const_states& _active_state_configuration, const state& _state ) const;
216  void add_deferred_event( const event_sptr& _event_id );
217  bool process_deferred_events( async_event_handler* const _async_event_handler );
218  static void stop_all_async_states_from_region( region_uptr& _region );
219  virtual void interrupt_impl();
220  void handle_unhandled_event( const event_sptr& _event );
221 
222  const std::string name_;
223  event_processing_callback* event_processing_callback_;
224  composite_state_uptr root_state_;
225  transitions transitions_;
226  events deferred_events_;
227  sxe::atomic<bool> state_machine_is_running_;
228  sxe::atomic<bool> interrupt_;
229 #ifdef Y_PROFILER
230  sxe::uint32_t processed_events_;
231 #endif
232  behavior_uptr event_handler_behavior_;
233 
234  };
235 
236 
237 }
238 
239 
240 #endif
sxe::SX_UNIQUE_PTR< transition > transition_uptr
Definition: transition_fwd.hpp:25
Definition: event_collector.hpp:25
std::vector< state_machine_defect > state_machine_defects
Definition: state_machine_defect_fwd.hpp:23
The interface for state machine introspection. It is inherited by the state machine class state_machi...
Definition: state_machine_introspection.hpp:28
Definition: state.hpp:33
virtual std::string get_name() const
Get the name of the state machine.
Definition: state_machine_base.cpp:248
virtual void halt()
Stops the state machine. It checks for active asynchronous simple states and stops their do behaviors...
Definition: state_machine_base.cpp:215
virtual bool is_interrupted() const SX_OVERRIDE
Get the internal flag of the state machine that specify if the run of state machine is interrupted...
Definition: state_machine_base.cpp:236
sxe::uint32_t event_id
The type alias for yasmine&#39;s event IDs.
Definition: event_id.hpp:26
virtual bool run()=0
composite_state & get_root_state() const
Returns the root state of the state machine. Commonly used to add regions to the root state and to cr...
Definition: state_machine_base.cpp:70
Definition: composite_state.hpp:28
Definition: region.hpp:42
sxe::SX_UNIQUE_PTR< region > region_uptr
Definition: region_fwd.hpp:26
bool process_event(const event_sptr &_event, async_event_handler *const _async_event_handler)
Definition: state_machine_base.cpp:299
virtual ~state_machine_base() SX_NOEXCEPT SX_OVERRIDE
Definition: state_machine_base.cpp:56
Definition: async_event_handler.hpp:22
Definition: adapter_cpp11.hpp:21
Callback interface that is called by a state machine during event processing. See the different metho...
Definition: event_processing_callback.hpp:37
virtual bool fire_event(const event_sptr &_event)=0
bool check(state_machine_defects &_defects) const
Check the state machine for defects by checking the constraints of each component.
Definition: state_machine_base.cpp:195
Definition: vertex.hpp:36
Definition: interruptible.hpp:22
std::vector< const state *> raw_const_states
Definition: state_fwd.hpp:29
sxe::shared_ptr< event > event_sptr
Definition: event_fwd.hpp:25
Definition: transition.hpp:34
virtual void set_behavior_of_unhandled_event_handler(const behavior_function &_behavior)
Sets the behavior for handler of unhandled events.
Definition: state_machine_base.cpp:242
state_machine_base(const std::string &_name, event_processing_callback *const _event_processing_callback=SX_NULLPTR)
Constructor.
Definition: state_machine_base.cpp:35
sxe::SX_UNIQUE_PTR< composite_state > composite_state_uptr
Definition: composite_state_fwd.hpp:25
static void stop_all_async_states(state &_state)
Definition: state_machine_base.cpp:484
sxe::function< void(const event &, event_collector &) > behavior_function
Definition: behavior_fwd.hpp:29
std::vector< event_id > event_ids
The type alias for yasmine&#39;s event ID list.
Definition: event_id.hpp:29
virtual void interrupt()
Set the internal flag of the state machine, to interrupt all processes inside the state machine and t...
Definition: state_machine_base.cpp:229
transition_kind
Definition: transition_kind.hpp:25
Definition: state_machine_base.hpp:45
sxe::function< bool(const event &, event_collector &) > constraint_function
Definition: constraint_fwd.hpp:28
virtual transition & add_transition(transition_uptr _transition)
Add a transition to the state machine. The state machine takes ownership of the transition.
Definition: state_machine_base.cpp:85
std::vector< transition_uptr > transitions
Definition: transition_fwd.hpp:31
std::vector< event_sptr > events
Definition: event_fwd.hpp:27
sxe::SX_UNIQUE_PTR< behavior > behavior_uptr
Definition: behavior_fwd.hpp:25