models.hpp 6.3 KB

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