models.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @file tests/multithreading/lifegame/models.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.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
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  29. namespace artis {
  30. namespace tests {
  31. namespace multithreading {
  32. namespace lifegame {
  33. struct CellParameters {
  34. std::map<std::string, int> neighbour_numbers;
  35. std::map<std::string, bool> initial_states;
  36. };
  37. class Cell
  38. : public artis::pdevs::Dynamics<common::DoubleTime, Cell, CellParameters> {
  39. public:
  40. enum inputs {
  41. IN
  42. };
  43. enum outputs {
  44. OUT
  45. };
  46. enum states {
  47. STATE
  48. };
  49. Cell(const std::string& name,
  50. const artis::pdevs::Context<common::DoubleTime, Cell, CellParameters>& context)
  51. :
  52. artis::pdevs::Dynamics<common::DoubleTime, Cell, CellParameters>(name,
  53. context),
  54. _neighbour_number(
  55. context.parameters().neighbour_numbers.find(name)->second),
  56. _initial_state(context.parameters().initial_states.find(name)->second)
  57. {
  58. input_ports({{IN, "in"}});
  59. output_ports({{OUT, "out"}});
  60. observables({{STATE, "state"}});
  61. }
  62. ~Cell() override = default;
  63. void dint(const typename common::DoubleTime::type& /* t */) override
  64. {
  65. if (_phase == SEND) {
  66. _phase = WAIT;
  67. _sigma = common::DoubleTime::infinity;
  68. } else if (_phase == NEW_STATE) {
  69. if (_state and (_true_neighbour_number < 2
  70. or _true_neighbour_number > 3)) {
  71. _state = false;
  72. } else if (not _state and (_true_neighbour_number == 3)) {
  73. _state = true;
  74. }
  75. _phase = SEND;
  76. _sigma = 1;
  77. _true_neighbour_number = 0;
  78. _received = 0;
  79. }
  80. }
  81. void dext(const typename common::DoubleTime::type& /* t */,
  82. const typename common::DoubleTime::type& /* e */,
  83. const common::Bag<common::DoubleTime>& bag) override
  84. {
  85. std::for_each(bag.begin(), bag.end(),
  86. [this](const common::ExternalEvent<common::DoubleTime>& e) {
  87. if (e.on_port(IN)) {
  88. bool data;
  89. e.data()(data);
  90. if (data) {
  91. ++_true_neighbour_number;
  92. }
  93. ++_received;
  94. }
  95. });
  96. if (_received == _neighbour_number) {
  97. _phase = NEW_STATE;
  98. _sigma = 0;
  99. } else {
  100. _phase = WAIT;
  101. _sigma = common::DoubleTime::infinity;
  102. }
  103. }
  104. void
  105. dconf(const typename common::DoubleTime::type& t,
  106. const typename common::DoubleTime::type& e,
  107. const common::Bag<common::DoubleTime>& bag) override
  108. {
  109. dext(t, e, bag);
  110. }
  111. void start(const typename common::DoubleTime::type& t) override
  112. {
  113. (void) t;
  114. _phase = SEND;
  115. _sigma = 0;
  116. _state = _initial_state;
  117. _true_neighbour_number = 0;
  118. _received = 0;
  119. }
  120. typename common::DoubleTime::type
  121. ta(const typename common::DoubleTime::type& /* t */) const override
  122. {
  123. return _sigma;
  124. }
  125. common::Bag<common::DoubleTime>
  126. lambda(const typename common::DoubleTime::type& /* t */) const override
  127. {
  128. common::Bag<common::DoubleTime> bag;
  129. if (_phase == SEND) {
  130. bag.push_back(
  131. artis::common::ExternalEvent<common::DoubleTime>(OUT, _state));
  132. }
  133. return bag;
  134. }
  135. common::Value observe(const common::DoubleTime::type& /* t */,
  136. unsigned int index) const override
  137. {
  138. if (index == STATE) {
  139. return _state;
  140. }
  141. return common::Value();
  142. }
  143. private:
  144. enum Phase {
  145. SEND, WAIT, NEW_STATE
  146. };
  147. // parameters
  148. unsigned int _neighbour_number;
  149. bool _initial_state;
  150. // state
  151. Phase _phase;
  152. common::DoubleTime::type _sigma;
  153. bool _state;
  154. unsigned int _received;
  155. unsigned int _true_neighbour_number;
  156. };
  157. }
  158. }
  159. }
  160. } // namespace artis tests multithreading lifegame
  161. #endif