Model.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <boost/heap/fibonacci_heap.hpp>
  31. #include <iostream>
  32. #include <sstream>
  33. #include <vector>
  34. namespace paradevs { namespace common {
  35. template < class Time >
  36. struct EventCompare;
  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. void set_parent(Model < Time >* parent)
  96. { _parent = parent; }
  97. // event
  98. void add_event(const common::ExternalEvent < Time >& message)
  99. {
  100. if (_inputs == 0) {
  101. _inputs = new Bag < Time >;
  102. }
  103. _inputs->push_back(message);
  104. }
  105. const common::Bag < Time >& get_bag()
  106. {
  107. if (_inputs == 0) {
  108. _inputs = new Bag < Time>;
  109. }
  110. return *_inputs;
  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. // time
  128. typename Time::type get_tl() const
  129. { return _tl; }
  130. typename Time::type get_tn() const
  131. { return _tn; }
  132. // devs methods
  133. virtual void observation(std::ostream& file) const =0;
  134. virtual void output(typename Time::type t) =0;
  135. virtual void post_event(typename Time::type t,
  136. const common::ExternalEvent < Time >& event) = 0;
  137. virtual typename Time::type start(typename Time::type t) =0;
  138. virtual typename Time::type transition(typename Time::type t) =0;
  139. void heap_id(typename boost::heap::fibonacci_heap <
  140. InternalEvent < Time >,
  141. boost::heap::compare <
  142. EventCompare < InternalEvent <
  143. Time > > > >::handle_type id)
  144. { _heap_id = id; }
  145. const typename boost::heap::fibonacci_heap <
  146. InternalEvent < Time >,
  147. boost::heap::compare <
  148. EventCompare < InternalEvent <
  149. Time > > > >::handle_type& heap_id() const
  150. { return _heap_id; }
  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. typename boost::heap::fibonacci_heap <
  161. InternalEvent < Time >,
  162. boost::heap::compare <
  163. EventCompare < InternalEvent <
  164. Time > > > >::handle_type _heap_id;
  165. };
  166. template < class Time >
  167. class Models : public std::vector < Model < Time >* >
  168. {
  169. public:
  170. Models()
  171. { }
  172. virtual ~Models()
  173. { }
  174. std::string to_string() const
  175. {
  176. std::ostringstream ss;
  177. ss << "{ ";
  178. for (typename Models < Time >::const_iterator it =
  179. Models < Time >::begin(); it != Models < Time >::end(); ++it) {
  180. ss << (*it)->get_name() << " ";
  181. }
  182. ss << "}";
  183. return ss.str();
  184. }
  185. };
  186. } } // namespace paradevs common
  187. #endif