Model.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. virtual std::string observable_name(unsigned int observable_index) const
  117. { (void)observable_index; assert(false); }
  118. std::string path() const
  119. { return (_parent != nullptr ? _parent->path() : "") + ":" + get_name(); }
  120. void set_parent(Model < Time >* parent)
  121. { _parent = parent; }
  122. virtual std::string to_string(int /* level */) const =0;
  123. // event
  124. void add_event(const common::ExternalEvent < Time >& message)
  125. {
  126. if (_inputs == 0) {
  127. _inputs = new Bag < Time >;
  128. }
  129. _inputs->push_back(message);
  130. }
  131. void clear_bag()
  132. {
  133. if (_inputs) {
  134. delete _inputs;
  135. _inputs = 0;
  136. }
  137. }
  138. unsigned int event_number() const
  139. {
  140. if (_inputs) {
  141. return _inputs->size();
  142. } else {
  143. return 0;
  144. }
  145. }
  146. const common::Bag < Time >& get_bag()
  147. {
  148. if (_inputs == 0) {
  149. _inputs = new Bag < Time >;
  150. }
  151. return *_inputs;
  152. }
  153. // time
  154. typename Time::type get_tl() const
  155. { return _tl; }
  156. typename Time::type get_tn() const
  157. { return _tn; }
  158. // devs methods
  159. virtual common::Value observe(const typename Time::type& t,
  160. unsigned int index) const =0;
  161. virtual void output(const typename Time::type& t) =0;
  162. virtual void post_event(const typename Time::type& t,
  163. const common::ExternalEvent < Time >& event) =0;
  164. virtual typename Time::type start(const typename Time::type& t) =0;
  165. virtual typename Time::type transition(const typename Time::type& t) =0;
  166. // scheduler
  167. void handle(SchedulerHandle handle)
  168. { _handle.handle(handle); }
  169. const SchedulerHandle& handle() const
  170. { return _handle.handle(); }
  171. protected:
  172. typename Time::type _tl;
  173. typename Time::type _tn;
  174. private :
  175. Model < Time >* _parent;
  176. std::string _name;
  177. Ports _in_ports;
  178. PortMap _in_port_map;
  179. Ports _out_ports;
  180. PortMap _out_port_map;
  181. Bag < Time >* _inputs;
  182. SchedulerHandle _handle;
  183. };
  184. template < class Time >
  185. class ModelMap : public std::map < unsigned int, Model < Time >* >
  186. {
  187. public:
  188. ModelMap()
  189. { }
  190. virtual ~ModelMap()
  191. { }
  192. std::string to_string() const
  193. {
  194. std::ostringstream ss;
  195. ss << "{ ";
  196. for (typename ModelMap < Time >::const_iterator it =
  197. ModelMap < Time >::begin();
  198. it != ModelMap < Time >::end(); ++it) {
  199. ss << it->second->get_name() << " ";
  200. }
  201. ss << "}";
  202. return ss.str();
  203. }
  204. };
  205. template < class Time >
  206. class Models : public std::vector < Model < Time >* >
  207. {
  208. public:
  209. Models()
  210. { }
  211. virtual ~Models()
  212. { }
  213. std::string to_string() const
  214. {
  215. std::ostringstream ss;
  216. ss << "{ ";
  217. for (typename Models < Time >::const_iterator it =
  218. Models < Time >::begin();
  219. it != Models < Time >::end(); ++it) {
  220. ss << (*it)->get_name() << " ";
  221. }
  222. ss << "}";
  223. return ss.str();
  224. }
  225. };
  226. template < class Time >
  227. class ModelsMap : public std::map < unsigned int, Models < Time > >
  228. {
  229. public:
  230. ModelsMap()
  231. { }
  232. virtual ~ModelsMap()
  233. { }
  234. std::string to_string() const
  235. {
  236. std::ostringstream ss;
  237. ss << "{ ";
  238. for (typename ModelsMap < Time >::const_iterator it =
  239. ModelsMap < Time >::begin();
  240. it != ModelsMap < Time >::end(); ++it) {
  241. ss << it->second.to_string() << " ";
  242. }
  243. ss << "}";
  244. return ss.str();
  245. }
  246. };
  247. } } // namespace artis common
  248. #endif