models.hpp 5.9 KB

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