Model.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. template < class Time >
  44. class Model
  45. {
  46. public:
  47. Model(const std::string& name) :
  48. _tl(0), _tn(0), _parent(0), _name(name), _inputs(0)
  49. { }
  50. virtual ~Model()
  51. {
  52. if (_inputs) {
  53. delete _inputs;
  54. }
  55. }
  56. void add_event(const common::ExternalEvent < Time >& message)
  57. {
  58. if (_inputs == 0) {
  59. _inputs = new Bag < Time >;
  60. }
  61. _inputs->push_back(message);
  62. }
  63. const common::Bag < Time >& get_bag()
  64. {
  65. if (_inputs == 0) {
  66. _inputs = new Bag < Time>;
  67. }
  68. return *_inputs;
  69. }
  70. void clear_bag()
  71. {
  72. if (_inputs) {
  73. delete _inputs;
  74. _inputs = 0;
  75. }
  76. }
  77. unsigned int event_number() const
  78. {
  79. if (_inputs) {
  80. return _inputs->size();
  81. } else {
  82. return 0;
  83. }
  84. }
  85. virtual void observation(std::ostream& file) const =0;
  86. virtual void output(typename Time::type t) =0;
  87. virtual void post_event(typename Time::type t,
  88. const common::ExternalEvent < Time >& event) = 0;
  89. virtual typename Time::type start(typename Time::type t) =0;
  90. virtual typename Time::type transition(typename Time::type t) =0;
  91. virtual const std::string& get_name() const
  92. { return _name; }
  93. Model < Time >* get_parent() const
  94. { return _parent; }
  95. typename Time::type get_tl() const
  96. { return _tl; }
  97. typename Time::type get_tn() const
  98. { return _tn; }
  99. void heap_id(typename boost::heap::fibonacci_heap <
  100. InternalEvent < Time >,
  101. boost::heap::compare <
  102. EventCompare < InternalEvent <
  103. Time > > > >::handle_type id)
  104. { _heap_id = id; }
  105. typename boost::heap::fibonacci_heap <
  106. InternalEvent < Time >,
  107. boost::heap::compare <
  108. EventCompare < InternalEvent <
  109. Time > > > >::handle_type heap_id()
  110. { return _heap_id; }
  111. void set_parent(Model < Time >* parent)
  112. { _parent = parent; }
  113. protected:
  114. typename Time::type _tl;
  115. typename Time::type _tn;
  116. private :
  117. Model < Time >* _parent;
  118. std::string _name;
  119. Bag < Time >* _inputs;
  120. typename boost::heap::fibonacci_heap <
  121. InternalEvent < Time >,
  122. boost::heap::compare <
  123. EventCompare < InternalEvent <
  124. Time > > > >::handle_type _heap_id;
  125. };
  126. template < class Time >
  127. class Models : public std::vector < Model < Time >* >
  128. {
  129. public:
  130. Models()
  131. { }
  132. virtual ~Models()
  133. { }
  134. std::string to_string() const
  135. {
  136. std::ostringstream ss;
  137. ss << "{ ";
  138. for (typename Models < Time >::const_iterator it =
  139. Models < Time >::begin(); it != Models < Time >::end(); ++it) {
  140. ss << (*it)->get_name() << " ";
  141. }
  142. ss << "}";
  143. return ss.str();
  144. }
  145. };
  146. } } // namespace paradevs common
  147. #endif