Model.hpp 7.0 KB

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