models.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @file tests/multithreading/lifegame/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_MULTITHREADING_LIFEGAME_MODELS_HPP
  26. #define TESTS_MULTITHREADING_LIFEGAME_MODELS_HPP 1
  27. #include <paradevs/common/time/DoubleTime.hpp>
  28. #include <paradevs/kernel/pdevs/Dynamics.hpp>
  29. namespace paradevs { namespace tests {
  30. namespace multithreading { namespace lifegame {
  31. struct CellParameters
  32. {
  33. CellParameters(int n) : neighbour_number(n)
  34. { }
  35. int neighbour_number;
  36. };
  37. class Cell : public paradevs::pdevs::Dynamics < common::DoubleTime,
  38. CellParameters >
  39. {
  40. public:
  41. Cell(const std::string& name, const CellParameters& parameters) :
  42. paradevs::pdevs::Dynamics < common::DoubleTime, CellParameters >(
  43. name, parameters),
  44. _neighbour_number(parameters.neighbour_number)
  45. { }
  46. virtual ~Cell()
  47. { }
  48. void dint(typename common::DoubleTime::type t)
  49. {
  50. (void)t;
  51. // std::cout << t << " [" << get_name() << "] -> dint: "
  52. // << _phase << " => ";
  53. if (_phase == SEND) {
  54. _phase = WAIT;
  55. _sigma = common::DoubleTime::infinity;
  56. } else if (_phase == NEWSTATE) {
  57. if (_state && (_true_neighbour_number < 2 ||
  58. _true_neighbour_number > 3)) {
  59. _state = false;
  60. } else if (not _state && (_true_neighbour_number == 3)) {
  61. _state = true;
  62. }
  63. _phase = SEND;
  64. _sigma = 1;
  65. _true_neighbour_number = 0;
  66. _received = 0;
  67. }
  68. // std::cout << _phase << std::endl;
  69. }
  70. void dext(typename common::DoubleTime::type t,
  71. typename common::DoubleTime::type /* e */,
  72. const common::Bag < common::DoubleTime >& bag)
  73. {
  74. (void)t;
  75. // std::cout << t << " [" << get_name() << "] -> dext => "
  76. // << _received << " " << _phase << std::endl;
  77. for (common::Bag < common::DoubleTime >::const_iterator it =
  78. bag.begin(); it != bag.end(); ++it) {
  79. if (it->get_content().get_content < bool >()) {
  80. ++_true_neighbour_number;
  81. }
  82. ++_received;
  83. }
  84. if (_received == _neighbour_number) {
  85. _phase = NEWSTATE;
  86. _sigma = 0;
  87. } else {
  88. _phase = WAIT;
  89. _sigma = common::DoubleTime::infinity;
  90. }
  91. // std::cout << t << " [" << get_name() << "] -> dext (AFTER) => "
  92. // << _received << " " << _phase << std::endl;
  93. }
  94. void dconf(typename common::DoubleTime::type t,
  95. typename common::DoubleTime::type e,
  96. const common::Bag < common::DoubleTime >& bag)
  97. {
  98. // std::cout << t << " [" << get_name() << "] -> dconf" << std::endl;
  99. dext(t, e, bag);
  100. }
  101. typename common::DoubleTime::type start(typename common::DoubleTime::type t)
  102. {
  103. (void)t;
  104. // std::cout << t << " [" << get_name() << "] -> START - "
  105. // << _neighbour_number << std::endl;
  106. _phase = SEND;
  107. _sigma = 0;
  108. _state = false;
  109. _true_neighbour_number = 0;
  110. _received = 0;
  111. return 0;
  112. }
  113. typename common::DoubleTime::type ta(
  114. typename common::DoubleTime::type t) const
  115. {
  116. (void)t;
  117. return _sigma;
  118. }
  119. common::Bag < common::DoubleTime > lambda(
  120. typename common::DoubleTime::type t) const
  121. {
  122. (void)t;
  123. common::Bag < common::DoubleTime > bag;
  124. if (_phase == SEND) {
  125. // std::cout << t << " [" << get_name() << "] -> lambda"
  126. // << std::endl;
  127. bag.push_back(common::ExternalEvent < common::DoubleTime >(
  128. "out", common::Value(_state)));
  129. }
  130. return bag;
  131. }
  132. void observation(std::ostream& /* file */) const
  133. { }
  134. private:
  135. enum Phase { SEND, WAIT, NEWSTATE };
  136. unsigned int _neighbour_number;
  137. Phase _phase;
  138. common::DoubleTime::type _sigma;
  139. bool _state;
  140. unsigned int _received;
  141. unsigned int _true_neighbour_number;
  142. };
  143. } } } } // namespace paradevs tests multithreading lifegame
  144. #endif