Model.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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-2021 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/Bag.hpp>
  28. #include <artis-star/common/ExternalEvent.hpp>
  29. #include <artis-star/common/InternalEvent.hpp>
  30. #include <artis-star/common/Scheduler.hpp>
  31. #include <artis-star/common/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 {
  38. namespace common {
  39. template<class Time>
  40. class ExternalEvent;
  41. template<class Time>
  42. class InternalEvent;
  43. template<class Time>
  44. class Bag;
  45. struct Port
  46. {
  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<class Time>
  53. class Model
  54. {
  55. public:
  56. Model(const std::string &name)
  57. :
  58. _tl(0), _tn(0), _parent(0), _name(name), _inputs(0)
  59. {}
  60. virtual ~Model()
  61. {
  62. if (_inputs) {
  63. delete _inputs;
  64. }
  65. }
  66. // structure
  67. void add_in_port(const Port &port)
  68. {
  69. assert(not exist_in_port(port.index));
  70. _in_ports.push_back(port.index);
  71. _in_port_map[port.index] = port.name;
  72. }
  73. void add_out_port(const Port &port)
  74. {
  75. assert(not exist_out_port(port.index));
  76. _out_ports.push_back(port.index);
  77. _out_port_map[port.index] = port.name;
  78. }
  79. void delete_in_port(const Port &port)
  80. {
  81. assert(not exist_in_port(port.index));
  82. _in_ports.erase(std::find(_in_ports.begin(), _in_ports.end(),
  83. port.index));
  84. _in_port_map.erase(port.index);
  85. }
  86. void delete_out_port(const Port &port)
  87. {
  88. assert(not exist_out_port(port.index));
  89. _out_ports.erase(std::find(_out_ports.begin(), _out_ports.end(),
  90. port.index));
  91. _out_port_map.erase(port.index);
  92. }
  93. bool exist_in_port(unsigned int port_index) const
  94. {
  95. return _in_port_map.find(port_index) != _in_port_map.end();
  96. }
  97. bool exist_out_port(unsigned int port_index) const
  98. {
  99. return _out_port_map.find(port_index) != _out_port_map.end();
  100. }
  101. std::string get_full_name() const
  102. {
  103. if (_parent) {
  104. return _parent->get_full_name() + ":" + _name;
  105. } else {
  106. return _name;
  107. }
  108. }
  109. std::string get_in_port_name(unsigned int port_index) const
  110. {
  111. assert(exist_in_port(port_index));
  112. return _in_port_map.find(port_index)->second;
  113. }
  114. size_t get_in_port_number() const
  115. { return _in_port_map.size(); }
  116. std::string get_out_port_name(unsigned int port_index) const
  117. {
  118. assert(exist_out_port(port_index));
  119. return _out_port_map.find(port_index)->second;
  120. }
  121. size_t get_out_port_number() const
  122. { return _out_port_map.size(); }
  123. const std::string &get_name() const
  124. { return _name; }
  125. Model<Time> *get_parent() const
  126. { return _parent; }
  127. virtual const Model<Time> *get_submodel(unsigned int index) const
  128. {
  129. (void) index;
  130. assert(false);
  131. return nullptr;
  132. }
  133. virtual const Model<Time> *get_submodel(unsigned int index,
  134. unsigned int rank) const
  135. {
  136. (void) index;
  137. (void) rank;
  138. assert(false);
  139. return nullptr;
  140. }
  141. virtual unsigned int get_submodel_number(unsigned int index) const
  142. {
  143. (void) index;
  144. assert(false);
  145. return 0;
  146. }
  147. virtual bool is_atomic() const = 0;
  148. virtual bool is_remote() const
  149. { return false; }
  150. virtual std::string observable_name(unsigned int observable_index) const
  151. {
  152. (void) observable_index;
  153. assert(false);
  154. return std::string();
  155. }
  156. std::string path() const
  157. {
  158. return (_parent != nullptr ? _parent->path() : "") + ":" + get_name();
  159. }
  160. virtual void restore(const common::context::State<Time> &state)
  161. {
  162. _tl = state.last_time();
  163. _tn = state.next_time();
  164. }
  165. virtual void save(common::context::State<Time> &state) const
  166. {
  167. state.last_time(_tl);
  168. state.next_time(_tn);
  169. }
  170. void set_parent(Model<Time> *parent)
  171. { _parent = parent; }
  172. virtual std::string to_string(int /* level */) const = 0;
  173. // event
  174. void add_event(const common::ExternalEvent<Time> &message)
  175. {
  176. if (_inputs == 0) {
  177. _inputs = new Bag<Time>;
  178. }
  179. _inputs->push_back(message);
  180. }
  181. void clear_bag()
  182. {
  183. if (_inputs) {
  184. delete _inputs;
  185. _inputs = 0;
  186. }
  187. }
  188. unsigned int event_number() const
  189. {
  190. return _inputs ? _inputs->size() : 0;
  191. }
  192. const common::Bag<Time> &get_bag()
  193. {
  194. if (_inputs == 0) {
  195. _inputs = new Bag<Time>;
  196. }
  197. return *_inputs;
  198. }
  199. // time
  200. typename Time::type get_tl() const
  201. { return _tl; }
  202. typename Time::type get_tn() const
  203. { return _tn; }
  204. // devs methods
  205. virtual void finish(const typename Time::type &t) = 0;
  206. virtual common::Value observe(const typename Time::type &t,
  207. unsigned int index) const = 0;
  208. virtual void output(const typename Time::type &t) = 0;
  209. virtual void post_event(const typename Time::type &t,
  210. const common::ExternalEvent<Time> &event) = 0;
  211. virtual typename Time::type start(const typename Time::type &t) = 0;
  212. virtual typename Time::type transition(const typename Time::type &t) = 0;
  213. virtual typename Time::type lookahead(const typename Time::type &t) const
  214. { return t; }
  215. // scheduler
  216. void handle(SchedulerHandle handle)
  217. { _handle.handle(handle); }
  218. const SchedulerHandle &handle() const
  219. { return _handle.handle(); }
  220. protected:
  221. typename Time::type _tl;
  222. typename Time::type _tn;
  223. private :
  224. Model<Time> *_parent;
  225. std::string _name;
  226. Ports _in_ports;
  227. PortMap _in_port_map;
  228. Ports _out_ports;
  229. PortMap _out_port_map;
  230. Bag<Time> *_inputs;
  231. SchedulerHandle _handle;
  232. };
  233. template<class Time>
  234. class ModelMap : public std::map<unsigned int, Model<Time> *>
  235. {
  236. public:
  237. ModelMap()
  238. {}
  239. virtual ~ModelMap()
  240. {}
  241. std::string to_string() const
  242. {
  243. std::ostringstream ss;
  244. ss << "{ ";
  245. for (typename ModelMap<Time>::const_iterator it =
  246. ModelMap<Time>::begin();
  247. it != ModelMap<Time>::end(); ++it) {
  248. ss << it->second->get_name() << " ";
  249. }
  250. ss << "}";
  251. return ss.str();
  252. }
  253. };
  254. template<class Time>
  255. class Models : public std::vector<Model<Time> *>
  256. {
  257. public:
  258. Models()
  259. {}
  260. virtual ~Models()
  261. {}
  262. std::string to_string() const
  263. {
  264. std::ostringstream ss;
  265. ss << "{ ";
  266. for (typename Models<Time>::const_iterator it =
  267. Models<Time>::begin();
  268. it != Models<Time>::end(); ++it) {
  269. ss << (*it)->get_name() << " ";
  270. }
  271. ss << "}";
  272. return ss.str();
  273. }
  274. };
  275. template<class Time>
  276. class ModelsMap : public std::map<unsigned int, Models<Time> >
  277. {
  278. public:
  279. ModelsMap()
  280. {}
  281. virtual ~ModelsMap()
  282. {}
  283. std::string to_string() const
  284. {
  285. std::ostringstream ss;
  286. ss << "{ ";
  287. for (typename ModelsMap<Time>::const_iterator it =
  288. ModelsMap<Time>::begin();
  289. it != ModelsMap<Time>::end(); ++it) {
  290. ss << it->second.to_string() << " ";
  291. }
  292. ss << "}";
  293. return ss.str();
  294. }
  295. };
  296. }
  297. } // namespace artis common
  298. #endif