Model.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // event
  98. void add_event(const common::ExternalEvent < Time, SchedulerHandle >&
  99. message)
  100. {
  101. if (_inputs == 0) {
  102. _inputs = new Bag < Time, SchedulerHandle >;
  103. }
  104. _inputs->push_back(message);
  105. }
  106. const common::Bag < Time, SchedulerHandle >& get_bag()
  107. {
  108. if (_inputs == 0) {
  109. _inputs = new Bag < Time, SchedulerHandle >;
  110. }
  111. return *_inputs;
  112. }
  113. void clear_bag()
  114. {
  115. if (_inputs) {
  116. delete _inputs;
  117. _inputs = 0;
  118. }
  119. }
  120. unsigned int event_number() const
  121. {
  122. if (_inputs) {
  123. return _inputs->size();
  124. } else {
  125. return 0;
  126. }
  127. }
  128. // time
  129. typename Time::type get_tl() const
  130. { return _tl; }
  131. typename Time::type get_tn() const
  132. { return _tn; }
  133. // devs methods
  134. virtual void observation(std::ostream& file) const =0;
  135. virtual void output(typename Time::type t) =0;
  136. virtual void post_event(typename Time::type t,
  137. const common::ExternalEvent < Time,
  138. SchedulerHandle >&
  139. event) = 0;
  140. virtual typename Time::type start(typename Time::type t) =0;
  141. virtual typename Time::type transition(typename Time::type t) =0;
  142. // scheduler
  143. void handle(SchedulerHandle handle)
  144. { _handle.handle(handle); }
  145. const SchedulerHandle& handle() const
  146. { return _handle.handle(); }
  147. protected:
  148. typename Time::type _tl;
  149. typename Time::type _tn;
  150. private :
  151. Model < Time, SchedulerHandle >* _parent;
  152. std::string _name;
  153. Ports _in_ports;
  154. Ports _out_ports;
  155. Bag < Time, SchedulerHandle >* _inputs;
  156. SchedulerHandle _handle;
  157. };
  158. template < class Time, class SchedulerHandle >
  159. class Models : public std::vector < Model < Time, SchedulerHandle >* >
  160. {
  161. public:
  162. Models()
  163. { }
  164. virtual ~Models()
  165. { }
  166. std::string to_string() const
  167. {
  168. std::ostringstream ss;
  169. ss << "{ ";
  170. for (typename Models < Time, SchedulerHandle >::const_iterator it =
  171. Models < Time, SchedulerHandle >::begin();
  172. it != Models < Time, SchedulerHandle >::end(); ++it) {
  173. ss << (*it)->get_name() << " ";
  174. }
  175. ss << "}";
  176. return ss.str();
  177. }
  178. };
  179. } } // namespace paradevs common
  180. #endif