Model.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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-2018 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 <algorithm>
  33. #include <cassert>
  34. #include <map>
  35. #include <iostream>
  36. #include <sstream>
  37. namespace artis { namespace common {
  38. template < class Time >
  39. class ExternalEvent;
  40. template < class Time >
  41. class InternalEvent;
  42. template < class Time >
  43. class Bag;
  44. struct Port
  45. {
  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. {
  54. public:
  55. Model(const std::string& name) :
  56. _tl(0), _tn(0), _parent(0), _name(name), _inputs(0)
  57. { }
  58. virtual ~Model()
  59. {
  60. if (_inputs) {
  61. delete _inputs;
  62. }
  63. }
  64. // structure
  65. void add_in_port(const Port& port)
  66. {
  67. assert(not exist_in_port(port.index));
  68. _in_ports.push_back(port.index);
  69. _in_port_map[port.index] = port.name;
  70. }
  71. void add_out_port(const Port& port)
  72. {
  73. assert(not exist_out_port(port.index));
  74. _out_ports.push_back(port.index);
  75. _out_port_map[port.index] = port.name;
  76. }
  77. void delete_in_port(const Port& port)
  78. {
  79. assert(not exist_in_port(port.index));
  80. _in_ports.erase(std::find(_in_ports.begin(), _in_ports.end(),
  81. port.index));
  82. _in_port_map.erase(port.index);
  83. }
  84. void delete_out_port(const Port& port)
  85. {
  86. assert(not exist_out_port(port.index));
  87. _out_ports.erase(std::find(_out_ports.begin(), _out_ports.end(),
  88. port.index));
  89. _out_port_map.erase(port.index);
  90. }
  91. bool exist_in_port(unsigned int port_index)
  92. {
  93. return _in_port_map.find(port_index) != _in_port_map.end();
  94. }
  95. bool exist_out_port(unsigned int port_index)
  96. {
  97. return _out_port_map.find(port_index) != _out_port_map.end();
  98. }
  99. const std::string& get_name() const
  100. { return _name; }
  101. Model < Time >* get_parent() const
  102. { return _parent; }
  103. // TODO: to remove
  104. virtual int get_receiver_number(typename Time::type t)
  105. { (void)t; return 0; }
  106. virtual const Model < Time >* get_submodel(unsigned int index) const
  107. { (void)index; assert(false); }
  108. virtual const Model < Time >* get_submodel(unsigned int index,
  109. unsigned int rank) const
  110. { (void)index; (void)rank; assert(false); }
  111. virtual unsigned int get_submodel_number(unsigned int index) const
  112. { (void)index; assert(false); }
  113. virtual bool is_atomic() const = 0;
  114. virtual bool is_remote() const
  115. { return false; }
  116. std::string path() const
  117. { return (_parent != nullptr ? _parent->path() : "") + ":" + get_name(); }
  118. void set_parent(Model < Time >* parent)
  119. { _parent = parent; }
  120. virtual std::string to_string(int /* level */) const =0;
  121. // event
  122. void add_event(const common::ExternalEvent < Time >& message)
  123. {
  124. if (_inputs == 0) {
  125. _inputs = new Bag < Time >;
  126. }
  127. _inputs->push_back(message);
  128. }
  129. void clear_bag()
  130. {
  131. if (_inputs) {
  132. delete _inputs;
  133. _inputs = 0;
  134. }
  135. }
  136. unsigned int event_number() const
  137. {
  138. if (_inputs) {
  139. return _inputs->size();
  140. } else {
  141. return 0;
  142. }
  143. }
  144. const common::Bag < Time >& get_bag()
  145. {
  146. if (_inputs == 0) {
  147. _inputs = new Bag < Time >;
  148. }
  149. return *_inputs;
  150. }
  151. // time
  152. typename Time::type get_tl() const
  153. { return _tl; }
  154. typename Time::type get_tn() const
  155. { return _tn; }
  156. // devs methods
  157. virtual common::Value observe(const typename Time::type& t,
  158. unsigned int index) const =0;
  159. virtual void output(const typename Time::type& t) =0;
  160. virtual void post_event(const typename Time::type& t,
  161. const common::ExternalEvent < Time >& event) =0;
  162. virtual typename Time::type start(const typename Time::type& t) =0;
  163. virtual typename Time::type transition(const typename Time::type& t) =0;
  164. // scheduler
  165. void handle(SchedulerHandle handle)
  166. { _handle.handle(handle); }
  167. const SchedulerHandle& handle() const
  168. { return _handle.handle(); }
  169. protected:
  170. typename Time::type _tl;
  171. typename Time::type _tn;
  172. private :
  173. Model < Time >* _parent;
  174. std::string _name;
  175. Ports _in_ports;
  176. PortMap _in_port_map;
  177. Ports _out_ports;
  178. PortMap _out_port_map;
  179. Bag < Time >* _inputs;
  180. SchedulerHandle _handle;
  181. };
  182. template < class Time >
  183. class ModelMap : public std::map < unsigned int, Model < Time >* >
  184. {
  185. public:
  186. ModelMap()
  187. { }
  188. virtual ~ModelMap()
  189. { }
  190. std::string to_string() const
  191. {
  192. std::ostringstream ss;
  193. ss << "{ ";
  194. for (typename ModelMap < Time >::const_iterator it =
  195. ModelMap < Time >::begin();
  196. it != ModelMap < Time >::end(); ++it) {
  197. ss << it->second->get_name() << " ";
  198. }
  199. ss << "}";
  200. return ss.str();
  201. }
  202. };
  203. template < class Time >
  204. class Models : public std::vector < Model < Time >* >
  205. {
  206. public:
  207. Models()
  208. { }
  209. virtual ~Models()
  210. { }
  211. std::string to_string() const
  212. {
  213. std::ostringstream ss;
  214. ss << "{ ";
  215. for (typename Models < Time >::const_iterator it =
  216. Models < Time >::begin();
  217. it != Models < Time >::end(); ++it) {
  218. ss << (*it)->get_name() << " ";
  219. }
  220. ss << "}";
  221. return ss.str();
  222. }
  223. };
  224. template < class Time >
  225. class ModelsMap : public std::map < unsigned int, Models < Time > >
  226. {
  227. public:
  228. ModelsMap()
  229. { }
  230. virtual ~ModelsMap()
  231. { }
  232. std::string to_string() const
  233. {
  234. std::ostringstream ss;
  235. ss << "{ ";
  236. for (typename ModelsMap < Time >::const_iterator it =
  237. ModelsMap < Time >::begin();
  238. it != ModelsMap < Time >::end(); ++it) {
  239. ss << it->second.to_string() << " ";
  240. }
  241. ss << "}";
  242. return ss.str();
  243. }
  244. };
  245. } } // namespace artis common
  246. #endif