Model.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 1
  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 <algorithm>
  32. #include <cassert>
  33. #include <iostream>
  34. #include <sstream>
  35. #include <vector>
  36. namespace paradevs { namespace common {
  37. template < class Time >
  38. class ExternalEvent;
  39. template < class Time >
  40. class InternalEvent;
  41. template < class Time >
  42. class Bag;
  43. typedef std::string Port;
  44. typedef std::vector < Port > Ports;
  45. template < class Time >
  46. class Model
  47. {
  48. public:
  49. Model(const std::string& name) :
  50. _tl(0), _tn(0), _parent(0), _name(name), _inputs(0)
  51. { }
  52. virtual ~Model()
  53. {
  54. if (_inputs) {
  55. delete _inputs;
  56. }
  57. }
  58. // structure
  59. void add_in_port(const std::string& port_name)
  60. {
  61. assert(not exist_in_port(port_name));
  62. _in_ports.push_back(port_name);
  63. }
  64. void add_out_port(const std::string& port_name)
  65. {
  66. assert(not exist_out_port(port_name));
  67. _out_ports.push_back(port_name);
  68. }
  69. void delete_in_port(const std::string& port_name)
  70. {
  71. assert(not exist_in_port(port_name));
  72. _in_ports.erase(std::find(_in_ports.begin(), _in_ports.end(),
  73. port_name));
  74. }
  75. void delete_out_port(const std::string& port_name)
  76. {
  77. assert(not exist_out_port(port_name));
  78. _out_ports.erase(std::find(_out_ports.begin(), _out_ports.end(),
  79. port_name));
  80. }
  81. bool exist_in_port(const std::string& port_name)
  82. {
  83. return std::find(_in_ports.begin(), _in_ports.end(),
  84. port_name) != _in_ports.end();
  85. }
  86. bool exist_out_port(const std::string& port_name)
  87. {
  88. return std::find(_out_ports.begin(), _out_ports.end(),
  89. port_name) != _out_ports.end();
  90. }
  91. const std::string& get_name() const
  92. { return _name; }
  93. Model < Time >* get_parent() const
  94. { return _parent; }
  95. // TODO: to remove
  96. virtual int get_receiver_number(typename Time::type t)
  97. { (void)t; return 0; }
  98. virtual bool is_atomic() const = 0;
  99. virtual bool is_remote() const
  100. { return false; }
  101. void set_parent(Model < Time >* parent)
  102. { _parent = parent; }
  103. virtual std::string to_string(int /* level */) const =0;
  104. // event
  105. void add_event(const common::ExternalEvent < Time >& message)
  106. {
  107. if (_inputs == 0) {
  108. _inputs = new Bag < Time >;
  109. }
  110. _inputs->push_back(message);
  111. }
  112. void clear_bag()
  113. {
  114. if (_inputs) {
  115. delete _inputs;
  116. _inputs = 0;
  117. }
  118. }
  119. unsigned int event_number() const
  120. {
  121. if (_inputs) {
  122. return _inputs->size();
  123. } else {
  124. return 0;
  125. }
  126. }
  127. const common::Bag < Time >& get_bag()
  128. {
  129. if (_inputs == 0) {
  130. _inputs = new Bag < Time >;
  131. }
  132. return *_inputs;
  133. }
  134. // time
  135. typename Time::type get_tl() const
  136. { return _tl; }
  137. typename Time::type get_tn() const
  138. { return _tn; }
  139. // devs methods
  140. virtual void observation(std::ostream& file) const =0;
  141. virtual void output(const typename Time::type& t) =0;
  142. virtual void post_event(const typename Time::type& t,
  143. const common::ExternalEvent < Time >& event) = 0;
  144. virtual typename Time::type start(const typename Time::type& t) =0;
  145. virtual typename Time::type transition(const typename Time::type& t) =0;
  146. // scheduler
  147. void handle(SchedulerHandle handle)
  148. { _handle.handle(handle); }
  149. const SchedulerHandle& handle() const
  150. { return _handle.handle(); }
  151. protected:
  152. typename Time::type _tl;
  153. typename Time::type _tn;
  154. private :
  155. Model < Time >* _parent;
  156. std::string _name;
  157. Ports _in_ports;
  158. Ports _out_ports;
  159. Bag < Time >* _inputs;
  160. SchedulerHandle _handle;
  161. };
  162. template < class Time >
  163. class Models : public std::vector < Model < Time >* >
  164. {
  165. public:
  166. Models()
  167. { }
  168. virtual ~Models()
  169. { }
  170. std::string to_string() const
  171. {
  172. std::ostringstream ss;
  173. ss << "{ ";
  174. for (typename Models < Time >::const_iterator it =
  175. Models < Time >::begin();
  176. it != Models < Time >::end(); ++it) {
  177. ss << (*it)->get_name() << " ";
  178. }
  179. ss << "}";
  180. return ss.str();
  181. }
  182. };
  183. } } // namespace paradevs common
  184. #endif