models.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/DoubleTime.hpp>
  28. #include <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. }
  95. }
  96. virtual void dext(typename common::DoubleTime::type t,
  97. typename common::DoubleTime::type /* e */,
  98. const common::Bag < common::DoubleTime,
  99. SchedulerHandle >& bag)
  100. {
  101. std::cout << NormalPixel < SchedulerHandle >::get_name() << " at "
  102. << t << ": dext -> "
  103. << bag.to_string() << std::endl;
  104. for (typename common::Bag < common::DoubleTime,
  105. SchedulerHandle >::const_iterator it =
  106. bag.begin(); it != bag.end(); ++it) {
  107. if (it->on_port("in")) {
  108. ++_received;
  109. if (_received == _neighbour_number) {
  110. _phase = SEND;
  111. }
  112. }
  113. }
  114. std::cout << NormalPixel < SchedulerHandle >::get_name() << " at "
  115. << t << ": dext -> "
  116. << _received << "/" << _neighbour_number
  117. << std::endl;
  118. }
  119. virtual typename common::DoubleTime::type start(
  120. typename common::DoubleTime::type /* t */)
  121. {
  122. _phase = WAIT;
  123. _received = 0;
  124. return common::DoubleTime::infinity;
  125. }
  126. virtual typename common::DoubleTime::type ta(
  127. typename common::DoubleTime::type t) const
  128. {
  129. std::cout << NormalPixel < SchedulerHandle >::get_name() << " at "
  130. << t << ": ta" << std::endl;
  131. if (_phase == WAIT) {
  132. return common::DoubleTime::infinity;
  133. } else {
  134. return 0;
  135. }
  136. }
  137. virtual common::Bag < common::DoubleTime, SchedulerHandle > lambda(
  138. typename common::DoubleTime::type t) const
  139. {
  140. std::cout << NormalPixel < SchedulerHandle >::get_name() << " at "
  141. << t << ": lambda" << std::endl;
  142. common::Bag < common::DoubleTime, SchedulerHandle > bag;
  143. if (_phase == SEND) {
  144. bag.push_back(common::ExternalEvent < common::DoubleTime,
  145. SchedulerHandle >("out", 0.));
  146. }
  147. return bag;
  148. }
  149. private:
  150. enum Phase { WAIT, SEND };
  151. unsigned int _neighbour_number;
  152. Phase _phase;
  153. unsigned int _received;
  154. };
  155. enum DynamicsType {
  156. TOP_PIXEL = 0, NORMAL_PIXEL
  157. };
  158. } } } // namespace paradevs tests boost_graph
  159. #endif