Model.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @file Model.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2016 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 <paradevs/common/Bag.hpp>
  28. #include <paradevs/common/ExternalEvent.hpp>
  29. #include <paradevs/common/InternalEvent.hpp>
  30. #include <paradevs/common/Scheduler.hpp>
  31. #include <paradevs/common/Value.hpp>
  32. #include <algorithm>
  33. #include <cassert>
  34. #include <map>
  35. #include <iostream>
  36. #include <sstream>
  37. namespace paradevs { 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 bool is_atomic() const = 0;
  105. virtual bool is_remote() const
  106. { return false; }
  107. void set_parent(Model < Time >* parent)
  108. { _parent = parent; }
  109. virtual std::string to_string(int /* level */) const =0;
  110. // event
  111. void add_event(const common::ExternalEvent < Time >& message)
  112. {
  113. if (_inputs == 0) {
  114. _inputs = new Bag < Time >;
  115. }
  116. _inputs->push_back(message);
  117. }
  118. void clear_bag()
  119. {
  120. if (_inputs) {
  121. delete _inputs;
  122. _inputs = 0;
  123. }
  124. }
  125. unsigned int event_number() const
  126. {
  127. if (_inputs) {
  128. return _inputs->size();
  129. } else {
  130. return 0;
  131. }
  132. }
  133. const common::Bag < Time >& get_bag()
  134. {
  135. if (_inputs == 0) {
  136. _inputs = new Bag < Time >;
  137. }
  138. return *_inputs;
  139. }
  140. // time
  141. typename Time::type get_tl() const
  142. { return _tl; }
  143. typename Time::type get_tn() const
  144. { return _tn; }
  145. // devs methods
  146. virtual common::Value observe(const typename Time::type& t,
  147. unsigned int index) const =0;
  148. virtual void output(const typename Time::type& t) =0;
  149. virtual void post_event(const typename Time::type& t,
  150. const common::ExternalEvent < Time >& event) =0;
  151. virtual typename Time::type start(const typename Time::type& t) =0;
  152. virtual typename Time::type transition(const typename Time::type& t) =0;
  153. // scheduler
  154. void handle(SchedulerHandle handle)
  155. { _handle.handle(handle); }
  156. const SchedulerHandle& handle() const
  157. { return _handle.handle(); }
  158. protected:
  159. typename Time::type _tl;
  160. typename Time::type _tn;
  161. private :
  162. Model < Time >* _parent;
  163. std::string _name;
  164. Ports _in_ports;
  165. Ports _out_ports;
  166. Bag < Time >* _inputs;
  167. SchedulerHandle _handle;
  168. };
  169. template < class Time >
  170. class ModelMap : public std::map < unsigned int, Model < Time >* >
  171. {
  172. public:
  173. ModelMap()
  174. { }
  175. virtual ~ModelMap()
  176. { }
  177. std::string to_string() const
  178. {
  179. std::ostringstream ss;
  180. ss << "{ ";
  181. for (typename ModelMap < Time >::const_iterator it =
  182. ModelMap < Time >::begin();
  183. it != ModelMap < Time >::end(); ++it) {
  184. ss << it->second->get_name() << " ";
  185. }
  186. ss << "}";
  187. return ss.str();
  188. }
  189. };
  190. template < class Time >
  191. class Models : public std::vector < Model < Time >* >
  192. {
  193. public:
  194. Models()
  195. { }
  196. virtual ~Models()
  197. { }
  198. std::string to_string() const
  199. {
  200. std::ostringstream ss;
  201. ss << "{ ";
  202. for (typename Models < Time >::const_iterator it =
  203. Models < Time >::begin();
  204. it != Models < Time >::end(); ++it) {
  205. ss << (*it)->get_name() << " ";
  206. }
  207. ss << "}";
  208. return ss.str();
  209. }
  210. };
  211. } } // namespace paradevs common
  212. #endif