models.hpp 4.8 KB

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