Model.hpp 5.6 KB

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