models.hpp 7.1 KB

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