models.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file tests/boost_graph/models.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 TESTS_BOOST_GRAPH_MODELS_HPP
  26. #define TESTS_BOOST_GRAPH_MODELS_HPP 1
  27. #include <paradevs/common/time/DoubleTime.hpp>
  28. #include <paradevs/kernel/pdevs/Dynamics.hpp>
  29. #define DELAY 100
  30. namespace paradevs { namespace tests { namespace boost_graph {
  31. void delay()
  32. {
  33. for (unsigned int i = 0; i < DELAY; ++i) {
  34. std::vector < int > v;
  35. for (unsigned int j = 1000; j > 0; --j) {
  36. v.push_back(j);
  37. }
  38. std::sort(v.begin(), v.end());
  39. }
  40. }
  41. struct TopPixelParameters
  42. { };
  43. class TopPixel :
  44. public paradevs::pdevs::Dynamics < common::DoubleTime, TopPixelParameters >
  45. {
  46. public:
  47. TopPixel(const std::string& name,
  48. const TopPixelParameters& parameters) :
  49. paradevs::pdevs::Dynamics < common::DoubleTime,
  50. TopPixelParameters >(name, parameters),
  51. _value(0)
  52. { }
  53. virtual ~TopPixel()
  54. { }
  55. virtual void dint(typename common::DoubleTime::type /* t */)
  56. {
  57. delay();
  58. // std::cout << TopPixel < SchedulerHandle >::get_name() << " at "
  59. // << t << ": dint" << std::endl;
  60. }
  61. virtual typename common::DoubleTime::type start(
  62. typename common::DoubleTime::type /* t */)
  63. {
  64. return 0;
  65. }
  66. virtual typename common::DoubleTime::type ta(
  67. typename common::DoubleTime::type /* t */) const
  68. { return 1; }
  69. virtual common::Bag < common::DoubleTime > lambda(
  70. typename common::DoubleTime::type t) const
  71. {
  72. // std::cout << get_name() << " at " << t << ": lambda" << std::endl;
  73. common::Bag < common::DoubleTime > bag;
  74. bag.push_back(common::ExternalEvent < common::DoubleTime >(
  75. "out", common::Value(_value)));
  76. return bag;
  77. }
  78. private:
  79. double _value;
  80. };
  81. struct NormalPixelParameters
  82. {
  83. NormalPixelParameters(unsigned int n) : _neighbour_number(n)
  84. { }
  85. unsigned int _neighbour_number;
  86. };
  87. class NormalPixel :
  88. public paradevs::pdevs::Dynamics < common::DoubleTime,
  89. NormalPixelParameters >
  90. {
  91. public:
  92. NormalPixel(const std::string& name,
  93. const NormalPixelParameters& parameters) :
  94. paradevs::pdevs::Dynamics < common::DoubleTime,
  95. NormalPixelParameters >(name, parameters),
  96. _neighbour_number(parameters._neighbour_number), _value(0)
  97. { }
  98. virtual ~NormalPixel()
  99. { }
  100. virtual void dint(typename common::DoubleTime::type t)
  101. {
  102. delay();
  103. if (_phase == SEND) {
  104. _phase = WAIT;
  105. _received = 0;
  106. _last_time = t;
  107. }
  108. }
  109. virtual void dext(typename common::DoubleTime::type t,
  110. typename common::DoubleTime::type /* e */,
  111. const common::Bag < common::DoubleTime >& bag)
  112. {
  113. // std::cout << get_name() << " at "
  114. // << t << ": dext -> "
  115. // << bag.to_string() << std::endl;
  116. for (typename common::Bag < common::DoubleTime >::const_iterator it =
  117. bag.begin(); it != bag.end(); ++it) {
  118. if (it->on_port("in")) {
  119. if (_last_time == t) {
  120. std::cout << "oups !" << std::endl;
  121. }
  122. ++_received;
  123. if (_received == _neighbour_number) {
  124. _phase = SEND;
  125. }
  126. }
  127. }
  128. // std::cout << get_name() << " at "
  129. // << t << ": dext -> "
  130. // << _received << "/" << _neighbour_number
  131. // << std::endl;
  132. }
  133. virtual typename common::DoubleTime::type start(
  134. typename common::DoubleTime::type /* t */)
  135. {
  136. // std::cout << NormalPixel < SchedulerHandle >::get_name()
  137. // << " = " <<_neighbour_number << std::endl;
  138. _phase = WAIT;
  139. _received = 0;
  140. _last_time = common::DoubleTime::negative_infinity;
  141. return common::DoubleTime::infinity;
  142. }
  143. virtual typename common::DoubleTime::type ta(
  144. typename common::DoubleTime::type /* t */) const
  145. {
  146. // std::cout << NormalPixel < SchedulerHandle >::get_name() << " at "
  147. // << t << ": ta" << std::endl;
  148. if (_phase == WAIT) {
  149. return common::DoubleTime::infinity;
  150. } else {
  151. return 0;
  152. }
  153. }
  154. virtual common::Bag < common::DoubleTime > lambda(
  155. typename common::DoubleTime::type /* t */) const
  156. {
  157. // std::cout << NormalPixel < SchedulerHandle >::get_name() << " at "
  158. // << t << ": lambda" << std::endl;
  159. common::Bag < common::DoubleTime > bag;
  160. if (_phase == SEND) {
  161. bag.push_back(common::ExternalEvent < common::DoubleTime >(
  162. "out", common::Value(_value)));
  163. }
  164. return bag;
  165. }
  166. private:
  167. enum Phase { WAIT, SEND };
  168. unsigned int _neighbour_number;
  169. double _value;
  170. Phase _phase;
  171. unsigned int _received;
  172. typename common::DoubleTime::type _last_time;
  173. };
  174. } } } // namespace paradevs tests boost_graph
  175. #endif