Model.hpp 9.9 KB

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