models.hpp 4.8 KB

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