models.hpp 7.4 KB

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