Model.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * @file common/Model.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2022 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_MODEL
  26. #define COMMON_MODEL
  27. #include "artis-star/common/event/Bag.hpp"
  28. #include "artis-star/common/event/ExternalEvent.hpp"
  29. #include "artis-star/common/event/InternalEvent.hpp"
  30. #include <artis-star/common/Scheduler.hpp>
  31. #include "artis-star/common/event/Value.hpp"
  32. #include <artis-star/common/context/State.hpp>
  33. #include <algorithm>
  34. #include <cassert>
  35. #include <map>
  36. #include <sstream>
  37. namespace artis::common {
  38. namespace event {
  39. template<typename Time>
  40. class ExternalEvent;
  41. template<typename Time>
  42. class InternalEvent;
  43. template<typename Time>
  44. class Bag;
  45. }
  46. struct Port {
  47. unsigned int index;
  48. std::string name;
  49. };
  50. typedef std::vector<unsigned int> Ports;
  51. typedef std::map<unsigned int, std::string> PortMap;
  52. template<typename Time>
  53. class Model {
  54. public:
  55. Model(const std::string &name)
  56. :
  57. _tl(0), _tn(0), _parent(0), _name(name), _inputs(0) {}
  58. virtual ~Model() {
  59. if (_inputs) {
  60. delete _inputs;
  61. }
  62. }
  63. // structure
  64. void add_in_port(const Port &port) {
  65. assert(not exist_in_port(port.index));
  66. _in_ports.push_back(port.index);
  67. _in_port_map[port.index] = port.name;
  68. }
  69. void add_out_port(const Port &port) {
  70. assert(not exist_out_port(port.index));
  71. _out_ports.push_back(port.index);
  72. _out_port_map[port.index] = port.name;
  73. }
  74. void delete_in_port(const Port &port) {
  75. assert(not exist_in_port(port.index));
  76. _in_ports.erase(std::find(_in_ports.begin(), _in_ports.end(),
  77. port.index));
  78. _in_port_map.erase(port.index);
  79. }
  80. void delete_out_port(const Port &port) {
  81. assert(not exist_out_port(port.index));
  82. _out_ports.erase(std::find(_out_ports.begin(), _out_ports.end(),
  83. port.index));
  84. _out_port_map.erase(port.index);
  85. }
  86. bool exist_in_port(unsigned int port_index) const {
  87. return _in_port_map.find(port_index) != _in_port_map.end();
  88. }
  89. bool exist_out_port(unsigned int port_index) const {
  90. return _out_port_map.find(port_index) != _out_port_map.end();
  91. }
  92. std::string get_full_name() const {
  93. if (_parent) {
  94. return _parent->get_full_name() + ":" + _name;
  95. } else {
  96. return _name;
  97. }
  98. }
  99. std::string get_in_port_name(unsigned int port_index) const {
  100. assert(exist_in_port(port_index));
  101. return _in_port_map.find(port_index)->second;
  102. }
  103. size_t get_in_port_number() const { return _in_port_map.size(); }
  104. std::string get_out_port_name(unsigned int port_index) const {
  105. assert(exist_out_port(port_index));
  106. return _out_port_map.find(port_index)->second;
  107. }
  108. size_t get_out_port_number() const { return _out_port_map.size(); }
  109. const std::string &get_name() const { return _name; }
  110. Model<Time> *get_parent() const { return _parent; }
  111. virtual const Model<Time> *get_sub_model(unsigned int index) const {
  112. (void) index;
  113. assert(false);
  114. return nullptr;
  115. }
  116. virtual const Model<Time> *get_sub_model(unsigned int index,
  117. unsigned int rank) const {
  118. (void) index;
  119. (void) rank;
  120. assert(false);
  121. return nullptr;
  122. }
  123. virtual unsigned int get_sub_model_number(unsigned int index) const {
  124. (void) index;
  125. assert(false);
  126. return 0;
  127. }
  128. virtual bool is_atomic() const = 0;
  129. virtual bool is_remote() const { return false; }
  130. virtual std::string observable_name(unsigned int observable_index) const {
  131. (void) observable_index;
  132. assert(false);
  133. return std::string();
  134. }
  135. std::string path() const {
  136. return (_parent != nullptr ? _parent->path() : "") + ":" + get_name();
  137. }
  138. virtual void restore(const common::context::State<Time> &state) {
  139. _tl = state.last_time();
  140. _tn = state.next_time();
  141. }
  142. virtual void save(common::context::State<Time> &state) const {
  143. state.last_time(_tl);
  144. state.next_time(_tn);
  145. }
  146. void set_parent(Model<Time> *parent) { _parent = parent; }
  147. virtual std::string to_string(int /* level */) const = 0;
  148. // event
  149. void add_event(const common::event::ExternalEvent<Time> &message) {
  150. if (_inputs == 0) {
  151. _inputs = new common::event::Bag<Time>;
  152. }
  153. _inputs->push_back(message);
  154. }
  155. void clear_bag() {
  156. if (_inputs) {
  157. delete _inputs;
  158. _inputs = 0;
  159. }
  160. }
  161. unsigned int event_number() const {
  162. return _inputs ? _inputs->size() : 0;
  163. }
  164. const common::event::Bag<Time> &get_bag() {
  165. if (_inputs == 0) {
  166. _inputs = new common::event::Bag<Time>;
  167. }
  168. return *_inputs;
  169. }
  170. // time
  171. typename Time::type get_tl() const { return _tl; }
  172. typename Time::type get_tn() const { return _tn; }
  173. // devs methods
  174. virtual void finish(const typename Time::type & /* t */) = 0;
  175. virtual common::event::Value observe(const typename Time::type & /* t */,
  176. unsigned int /* index */) const = 0;
  177. virtual void output(const typename Time::type & /* t */) = 0;
  178. virtual void post_event(const typename Time::type & /* t */,
  179. const common::event::ExternalEvent<Time> & /* event */) = 0;
  180. virtual typename Time::type start(const typename Time::type & /* t */) = 0;
  181. virtual typename Time::type transition(const typename Time::type & /* t */) = 0;
  182. virtual typename Time::type lookahead(const typename Time::type &t) const { return t; }
  183. // scheduler
  184. void handle(SchedulerHandle handle) { _handle.handle(handle); }
  185. const SchedulerHandle &handle() const { return _handle.handle(); }
  186. protected:
  187. typename Time::type _tl;
  188. typename Time::type _tn;
  189. private :
  190. Model<Time> *_parent;
  191. std::string _name;
  192. Ports _in_ports;
  193. PortMap _in_port_map;
  194. Ports _out_ports;
  195. PortMap _out_port_map;
  196. common::event::Bag<Time> *_inputs;
  197. SchedulerHandle _handle;
  198. };
  199. template<typename Time>
  200. class ModelMap : public std::map<unsigned int, Model<Time> *> {
  201. public:
  202. ModelMap() {}
  203. virtual ~ModelMap() {}
  204. std::string to_string() const {
  205. std::ostringstream ss;
  206. ss << "{ ";
  207. for (typename ModelMap<Time>::const_iterator it = ModelMap<Time>::begin(); it != ModelMap<Time>::end(); ++it) {
  208. ss << it->second->get_name() << " ";
  209. }
  210. ss << "}";
  211. return ss.str();
  212. }
  213. };
  214. template<typename Time>
  215. class Models : public std::vector<Model<Time> *> {
  216. public:
  217. Models() {}
  218. virtual ~Models() {}
  219. std::string to_string() const {
  220. std::ostringstream ss;
  221. ss << "{ ";
  222. for (typename Models<Time>::const_iterator it = Models<Time>::begin(); it != Models<Time>::end(); ++it) {
  223. ss << (*it)->get_name() << " ";
  224. }
  225. ss << "}";
  226. return ss.str();
  227. }
  228. };
  229. template<typename Time>
  230. class ModelsMap : public std::map<unsigned int, Models<Time> > {
  231. public:
  232. ModelsMap() {}
  233. virtual ~ModelsMap() {}
  234. std::string to_string() const {
  235. std::ostringstream ss;
  236. ss << "{ ";
  237. for (typename ModelsMap<Time>::const_iterator it = ModelsMap<Time>::begin(); it != ModelsMap<Time>::end(); ++it) {
  238. ss << it->second.to_string() << " ";
  239. }
  240. ss << "}";
  241. return ss.str();
  242. }
  243. };
  244. } // namespace artis common
  245. #endif