The MAUVE Toolchain
FiniteStateMachine.hpp
1 /*
2  * Copyright 2017 ONERA
3  *
4  * This file is part of the MAUVE Runtime project.
5  *
6  * MAUVE Runtime is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version 3 as
8  * published by the Free Software Foundation.
9  *
10  * MAUVE Runtime is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with MAUVE. If not, see <https://www.gnu.org/licenses/lgpl-3.0>.
17  */
18 #ifndef MAUVE_RUNTIME_FINITE_STATE_MACHINE_HPP
19 #define MAUVE_RUNTIME_FINITE_STATE_MACHINE_HPP
20 
21 #include "common.hpp"
22 #include "AbstractFiniteStateMachine.hpp"
23 #include "WithLogger.hpp"
24 #include "WithHook.hpp"
25 #include "FSMContainer.hpp"
26 
27 #include <string>
28 #include <vector>
29 #include <functional>
30 
31 namespace mauve {
32  namespace runtime {
33 
34  template <typename T>
35  class State;
36 
37  template <typename T>
38  class SynchroState;
39 
40  template <typename T>
41  class ExecState;
42 
43  template <typename SHELL, typename CORE>
46  , virtual public WithLogger
47  , virtual public WithHook
48  {
49  public:
50  using Guard_t = typename ExecState<CORE>::Guard_t;
51  using Update_t = typename ExecState<CORE>::Update_t;
52 
53  template <typename S, typename C, typename F>
54  friend class Component;
55 
56  FiniteStateMachine(FiniteStateMachine const & other) = delete;
57  FiniteStateMachine<SHELL, CORE> & operator=(FiniteStateMachine<SHELL, CORE> const & other) = delete;
58 
59  std::string shell_type_name() const override;
60  std::string core_type_name () const override;
61 
62  inline AbstractLogger& logger() const override { return _container->logger(); };
63 
64  inline bool is_configured() const override final { return _configured; }
65 
66  inline bool configure() override final { return _container->configure_fsm(); }
67  inline void cleanup() override final { _container->cleanup_fsm(); }
68 
69  std::size_t get_states_size() const override { return states.size(); }
70  AbstractState* get_state(std::string const & name) const override;
71  AbstractState* get_state(int index) const override;
72  int get_state_index(const AbstractState* state) const override;
73 
74  bool check_reachable() const;
75 
76  protected:
78  virtual ~FiniteStateMachine() noexcept;
79 
80  ExecState<CORE> & mk_execution(std::string const & name, Update_t fun);
81  SynchroState<CORE> & mk_synchronization(std::string const & name, time_ns_t clock);
82 
83  void set_initial(State<CORE> & state);
84  void set_next(State<CORE> & state, State<CORE> & next);
85  void mk_transition(ExecState<CORE> & state, Guard_t guard, State<CORE> & next);
86 
87  inline SHELL & shell() const { return _container->shell(); };
88  inline CORE & core () const { return _container->core(); };
89 
90  private:
91  FSMContainer<SHELL, CORE>* _container;
92  bool _configured;
93  State<CORE> * initial;
94  std::vector<State<CORE> *> states;
95 
96  bool _configure();
97  void _cleanup();
98  };
99 
101  struct AllreadyDefinedState: public std::exception {
102  AllreadyDefinedState(std::string const & name) throw() : name(name) {}
104  const std::string name;
106  virtual const char* what() const throw() {
107  std::string message = "Allready Defined State " + name;
108  return message.c_str();
109  }
110  };
111 
112  }
113 } // namespace
114 
115 #include "ipp/FiniteStateMachine.ipp"
116 
117 #endif
bool is_configured() const overridefinal
Get the configuration status of the configurable object.
Definition: FiniteStateMachine.hpp:64
void cleanup() overridefinal
Cleanup the object.
Definition: FiniteStateMachine.hpp:67
AbstractState * get_state(std::string const &name) const override
Get a state of the FSM by name.
Definition: FiniteStateMachine.ipp:72
Definition: FSMContainer.hpp:30
int get_state_index(const AbstractState *state) const override
Get a index of state in the FSM.
Definition: FiniteStateMachine.ipp:88
AbstractLogger & logger() const override
Get the logger.
Definition: FiniteStateMachine.hpp:62
A Finite State Machine describes the component behavior.
Definition: AbstractFiniteStateMachine.hpp:34
std::string shell_type_name() const override
Get the type of the FSM shell.
Definition: FiniteStateMachine.ipp:58
Objects with hooks.
Definition: WithHook.hpp:27
Object with a Logger.
Definition: WithLogger.hpp:27
std::string core_type_name() const override
Get the type of the FSM core.
Definition: FiniteStateMachine.ipp:65
Component class.
Definition: Architecture.hpp:34
Class of a generic MAUVE logger.
Definition: logger.hpp:39
Definition: Component.hpp:46
The MAUVE namespace.
Definition: tracing.hpp:24
Definition: AbstractCore.hpp:32
const std::string name
Exception name.
Definition: FiniteStateMachine.hpp:104
Abstract State class.
Definition: AbstractState.hpp:34
Definition: AbstractCore.hpp:30
bool configure() overridefinal
Configure the object.
Definition: FiniteStateMachine.hpp:66
Definition: FiniteStateMachine.hpp:44
Exception for Already Defined States.
Definition: FiniteStateMachine.hpp:101
std::size_t get_states_size() const override
Definition: FiniteStateMachine.hpp:69
virtual const char * what() const
Exception explanation.
Definition: FiniteStateMachine.hpp:106