models.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-2022 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. input_ports({{IN, "in"}});
  58. output_ports({{OUT, "out"}});
  59. observables({{STATE, "state"}});
  60. }
  61. ~Cell() override = default;
  62. void dint(const typename common::DoubleTime::type & /* t */) override {
  63. if (_phase == SEND) {
  64. _phase = WAIT;
  65. _sigma = common::DoubleTime::infinity;
  66. } else if (_phase == NEW_STATE) {
  67. if (_state and (_true_neighbour_number < 2
  68. or _true_neighbour_number > 3)) {
  69. _state = false;
  70. } else if (not _state and (_true_neighbour_number == 3)) {
  71. _state = true;
  72. }
  73. _phase = SEND;
  74. _sigma = 1;
  75. _true_neighbour_number = 0;
  76. _received = 0;
  77. }
  78. }
  79. void dext(const typename common::DoubleTime::type & /* t */,
  80. const typename common::DoubleTime::type & /* e */,
  81. const common::event::Bag<common::DoubleTime> &bag) override {
  82. std::for_each(bag.begin(), bag.end(),
  83. [this](const common::event::ExternalEvent<common::DoubleTime> &e) {
  84. if (e.on_port(IN)) {
  85. bool data;
  86. e.data()(data);
  87. if (data) {
  88. ++_true_neighbour_number;
  89. }
  90. ++_received;
  91. }
  92. });
  93. if (_received == _neighbour_number) {
  94. _phase = NEW_STATE;
  95. _sigma = 0;
  96. } else {
  97. _phase = WAIT;
  98. _sigma = common::DoubleTime::infinity;
  99. }
  100. }
  101. void
  102. dconf(const typename common::DoubleTime::type &t,
  103. const typename common::DoubleTime::type &e,
  104. const common::event::Bag<common::DoubleTime> &bag) override {
  105. dext(t, e, bag);
  106. }
  107. void start(const typename common::DoubleTime::type &t) override {
  108. (void) t;
  109. _phase = SEND;
  110. _sigma = 0;
  111. _state = _initial_state;
  112. _true_neighbour_number = 0;
  113. _received = 0;
  114. }
  115. typename common::DoubleTime::type
  116. ta(const typename common::DoubleTime::type & /* t */) const override {
  117. return _sigma;
  118. }
  119. common::event::Bag<common::DoubleTime>
  120. lambda(const typename common::DoubleTime::type & /* t */) const override {
  121. common::event::Bag<common::DoubleTime> bag;
  122. if (_phase == SEND) {
  123. bag.push_back(
  124. artis::common::event::ExternalEvent<common::DoubleTime>(OUT, _state));
  125. }
  126. return bag;
  127. }
  128. common::event::Value observe(const common::DoubleTime::type & /* t */,
  129. unsigned int index) const override {
  130. if (index == STATE) {
  131. return _state;
  132. }
  133. return common::event::Value();
  134. }
  135. private:
  136. enum Phase {
  137. SEND, WAIT, NEW_STATE
  138. };
  139. // parameters
  140. unsigned int _neighbour_number;
  141. bool _initial_state;
  142. // state
  143. Phase _phase;
  144. common::DoubleTime::type _sigma;
  145. bool _state;
  146. unsigned int _received;
  147. unsigned int _true_neighbour_number;
  148. };
  149. }
  150. }
  151. }
  152. } // namespace artis tests multithreading lifegame
  153. #endif