models.hpp 6.8 KB

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