Model.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-2015 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 <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. virtual bool is_atomic() const = 0;
  96. virtual bool is_remote() const
  97. { return false; }
  98. void set_parent(Model < Time >* parent)
  99. { _parent = parent; }
  100. virtual std::string to_string(int /* level */) const =0;
  101. // event
  102. void add_event(const common::ExternalEvent < Time >& message)
  103. {
  104. if (_inputs == 0) {
  105. _inputs = new Bag < Time >;
  106. }
  107. _inputs->push_back(message);
  108. }
  109. void clear_bag()
  110. {
  111. if (_inputs) {
  112. delete _inputs;
  113. _inputs = 0;
  114. }
  115. }
  116. unsigned int event_number() const
  117. {
  118. if (_inputs) {
  119. return _inputs->size();
  120. } else {
  121. return 0;
  122. }
  123. }
  124. const common::Bag < Time >& get_bag()
  125. {
  126. if (_inputs == 0) {
  127. _inputs = new Bag < Time >;
  128. }
  129. return *_inputs;
  130. }
  131. // time
  132. typename Time::type get_tl() const
  133. { return _tl; }
  134. typename Time::type get_tn() const
  135. { return _tn; }
  136. // devs methods
  137. virtual void observation(std::ostream& file) const =0;
  138. virtual void output(typename Time::type t) =0;
  139. virtual void post_event(typename Time::type t,
  140. const common::ExternalEvent < Time >& 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 >* _parent;
  153. std::string _name;
  154. Ports _in_ports;
  155. Ports _out_ports;
  156. Bag < Time >* _inputs;
  157. SchedulerHandle _handle;
  158. };
  159. template < class Time >
  160. class Models : public std::vector < Model < Time >* >
  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 >::const_iterator it =
  172. Models < Time >::begin();
  173. it != Models < Time >::end(); ++it) {
  174. ss << (*it)->get_name() << " ";
  175. }
  176. ss << "}";
  177. return ss.str();
  178. }
  179. };
  180. } } // namespace paradevs common
  181. #endif