models.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @file tests/dsde/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_DSDE_MODELS_HPP
  26. #define TESTS_DSDE_MODELS_HPP
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/dsde/Coordinator.hpp>
  30. #include <artis-star/kernel/dsde/Executive.hpp>
  31. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  32. #include <tests/dsde/graph_manager.hpp>
  33. namespace artis {
  34. namespace tests {
  35. namespace dsde {
  36. class Beep :
  37. public artis::pdevs::Dynamics<common::DoubleTime, Beep>
  38. {
  39. public:
  40. enum inputs
  41. {
  42. IN
  43. };
  44. enum outputs
  45. {
  46. OUT
  47. };
  48. Beep(const std::string &name,
  49. const artis::pdevs::Context<common::DoubleTime, Beep, artis::common::NoParameters> &context)
  50. :
  51. artis::pdevs::Dynamics<common::DoubleTime, Beep>(name, context), _value(0)
  52. {
  53. input_ports({{IN, "in"}});
  54. output_ports({{OUT, "out"}});
  55. }
  56. ~Beep() override = default;
  57. void dint(const typename common::DoubleTime::type &t) override
  58. {
  59. #ifndef WITH_TRACE
  60. (void)t;
  61. #endif
  62. #ifdef WITH_TRACE
  63. common::Trace<common::DoubleTime>::trace()
  64. << common::TraceElement<common::DoubleTime>(get_name(), t,
  65. common::FormalismType::PDEVS,
  66. common::FunctionType::DELTA_INT,
  67. common::LevelType::USER);
  68. common::Trace<common::DoubleTime>::trace().flush();
  69. #endif
  70. if (_phase == SEND or _phase == INIT) {
  71. _phase = WAIT;
  72. }
  73. }
  74. void
  75. dext(const typename common::DoubleTime::type &t,
  76. const typename common::DoubleTime::type & /* e */,
  77. const common::Bag <common::DoubleTime> &bag) override
  78. {
  79. #ifndef WITH_TRACE
  80. (void)t;
  81. (void)bag;
  82. #endif
  83. #ifdef WITH_TRACE
  84. common::Trace<common::DoubleTime>::trace()
  85. << common::TraceElement<common::DoubleTime>(get_name(), t,
  86. common::FormalismType::PDEVS,
  87. common::FunctionType::DELTA_EXT,
  88. common::LevelType::USER)
  89. << "messages = " << bag.to_string();
  90. common::Trace<common::DoubleTime>::trace().flush();
  91. #endif
  92. _phase = SEND;
  93. }
  94. void dconf(const typename common::DoubleTime::type &t,
  95. const typename common::DoubleTime::type & /* e */,
  96. const common::Bag <common::DoubleTime> &bag) override
  97. {
  98. #ifndef WITH_TRACE
  99. (void)t;
  100. (void)bag;
  101. #endif
  102. #ifdef WITH_TRACE
  103. common::Trace<common::DoubleTime>::trace()
  104. << common::TraceElement<common::DoubleTime>(get_name(), t,
  105. common::FormalismType::PDEVS,
  106. common::FunctionType::DELTA_CONF,
  107. common::LevelType::USER)
  108. << "messages = " << bag.to_string();
  109. common::Trace<common::DoubleTime>::trace().flush();
  110. #endif
  111. }
  112. void
  113. start(const typename common::DoubleTime::type &t) override
  114. {
  115. #ifndef WITH_TRACE
  116. (void)t;
  117. #endif
  118. #ifdef WITH_TRACE
  119. common::Trace<common::DoubleTime>::trace()
  120. << common::TraceElement<common::DoubleTime>(get_name(), t,
  121. common::FormalismType::PDEVS,
  122. common::FunctionType::START,
  123. common::LevelType::USER);
  124. common::Trace<common::DoubleTime>::trace().flush();
  125. #endif
  126. _phase = INIT;
  127. }
  128. typename common::DoubleTime::type
  129. ta(const typename common::DoubleTime::type &t) const override
  130. {
  131. #ifndef WITH_TRACE
  132. (void)t;
  133. #endif
  134. #ifdef WITH_TRACE
  135. common::Trace<common::DoubleTime>::trace()
  136. << common::TraceElement<common::DoubleTime>(get_name(), t,
  137. common::FormalismType::PDEVS,
  138. common::FunctionType::TA,
  139. common::LevelType::USER);
  140. common::Trace<common::DoubleTime>::trace().flush();
  141. #endif
  142. if (_phase == WAIT) {
  143. return (rand() % 10) / 2. + 1;
  144. } else {
  145. return 0;
  146. }
  147. }
  148. common::Bag <common::DoubleTime>
  149. lambda(const typename common::DoubleTime::type &t) const override
  150. {
  151. #ifndef WITH_TRACE
  152. (void)t;
  153. #endif
  154. common::Bag<common::DoubleTime> bag;
  155. bag.push_back(artis::common::ExternalEvent<common::DoubleTime>(OUT, _value));
  156. #ifdef WITH_TRACE
  157. common::Trace<common::DoubleTime>::trace()
  158. << common::TraceElement<common::DoubleTime>(get_name(), t,
  159. common::FormalismType::PDEVS,
  160. common::FunctionType::LAMBDA,
  161. common::LevelType::USER)
  162. << "messages = " << bag.to_string();
  163. common::Trace<common::DoubleTime>::trace().flush();
  164. #endif
  165. return bag;
  166. }
  167. private:
  168. enum Phase
  169. {
  170. INIT, WAIT, SEND
  171. };
  172. Phase _phase;
  173. double _value;
  174. };
  175. }
  176. }
  177. } // namespace artis tests dsde
  178. #endif