models.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // std::cout << t << " [" << get_name() << "] -> dint: "
  51. // << _phase << " => ";
  52. if (_phase == SEND) {
  53. _phase = WAIT;
  54. _sigma = common::DoubleTime::infinity;
  55. } else if (_phase == NEWSTATE) {
  56. if (_state && (_true_neighbour_number < 2 ||
  57. _true_neighbour_number > 3)) {
  58. _state = false;
  59. } else if (not _state && (_true_neighbour_number == 3)) {
  60. _state = true;
  61. }
  62. _phase = SEND;
  63. _sigma = 1;
  64. _true_neighbour_number = 0;
  65. _received = 0;
  66. }
  67. // std::cout << _phase << std::endl;
  68. }
  69. void dext(typename common::DoubleTime::type t,
  70. typename common::DoubleTime::type /* e */,
  71. const common::Bag < common::DoubleTime >& bag)
  72. {
  73. // std::cout << t << " [" << get_name() << "] -> dext => "
  74. // << _received << " " << _phase << std::endl;
  75. for (common::Bag < common::DoubleTime >::const_iterator it =
  76. bag.begin(); it != bag.end(); ++it) {
  77. if (it->get_content().get_content < bool >()) {
  78. ++_true_neighbour_number;
  79. }
  80. ++_received;
  81. }
  82. if (_received == _neighbour_number) {
  83. _phase = NEWSTATE;
  84. _sigma = 0;
  85. } else {
  86. _phase = WAIT;
  87. _sigma = common::DoubleTime::infinity;
  88. }
  89. // std::cout << t << " [" << get_name() << "] -> dext (AFTER) => "
  90. // << _received << " " << _phase << std::endl;
  91. }
  92. void dconf(typename common::DoubleTime::type t,
  93. typename common::DoubleTime::type e,
  94. const common::Bag < common::DoubleTime >& bag)
  95. {
  96. // std::cout << t << " [" << get_name() << "] -> dconf" << std::endl;
  97. dext(t, e, bag);
  98. }
  99. typename common::DoubleTime::type start(typename common::DoubleTime::type t)
  100. {
  101. // std::cout << t << " [" << get_name() << "] -> START" << std::endl;
  102. _phase = SEND;
  103. _sigma = 0;
  104. _state = false;
  105. _true_neighbour_number = 0;
  106. _received = 0;
  107. return 0;
  108. }
  109. typename common::DoubleTime::type ta(
  110. typename common::DoubleTime::type t) const
  111. { return _sigma; }
  112. common::Bag < common::DoubleTime > lambda(
  113. typename common::DoubleTime::type t) const
  114. {
  115. common::Bag < common::DoubleTime > bag;
  116. if (_phase == SEND) {
  117. // std::cout << t << " [" << get_name() << "] -> lambda"
  118. // << std::endl;
  119. bag.push_back(common::ExternalEvent < common::DoubleTime >(
  120. "out", common::Value(_state)));
  121. }
  122. return bag;
  123. }
  124. void observation(std::ostream& /* file */) const
  125. { }
  126. private:
  127. enum Phase { SEND, WAIT, NEWSTATE };
  128. unsigned int _neighbour_number;
  129. Phase _phase;
  130. common::DoubleTime::type _sigma;
  131. bool _state;
  132. unsigned int _received;
  133. unsigned int _true_neighbour_number;
  134. };
  135. } } } } // namespace paradevs tests multithreading lifegame
  136. #endif