models.hpp 5.7 KB

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