models.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @file tests/devs/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_DEVS_MODELS_HPP
  26. #define TESTS_DEVS_MODELS_HPP
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/devs/Dynamics.hpp>
  30. #include <chrono>
  31. #include <iostream>
  32. namespace artis {
  33. namespace tests {
  34. namespace devs {
  35. struct data {
  36. double x;
  37. double y;
  38. data()
  39. : x(0), y(0) {}
  40. data(double _x, double _y)
  41. : x(_x), y(_y) {}
  42. };
  43. class A : public artis::devs::Dynamics<common::DoubleTime, A> {
  44. public:
  45. A(const std::string &name,
  46. const artis::devs::Context<common::DoubleTime, A, artis::common::NoParameters> &context)
  47. :
  48. artis::devs::Dynamics<common::DoubleTime, A>(name, context) {
  49. }
  50. ~A() override = default;
  51. void dint(typename common::DoubleTime::type t) override {
  52. #ifndef WITH_TRACE
  53. (void)t;
  54. #else
  55. common::Trace<common::DoubleTime>::trace()
  56. << common::TraceElement<common::DoubleTime>(get_name(), t,
  57. common::FormalismType::DEVS,
  58. common::FunctionType::DELTA_INT,
  59. common::LevelType::USER);
  60. common::Trace<common::DoubleTime>::trace().flush();
  61. #endif
  62. if (_phase == WAIT) {
  63. ++_value.x;
  64. --_value.y;
  65. _phase = SEND;
  66. } else if (_phase == SEND) {
  67. _phase = WAIT;
  68. }
  69. }
  70. void
  71. dext(typename common::DoubleTime::type t, typename common::DoubleTime::type /* e */,
  72. const common::ExternalEvent<common::DoubleTime> &msg) override {
  73. #ifndef WITH_TRACE
  74. (void)t;
  75. (void)msgs;
  76. #else
  77. common::Trace<common::DoubleTime>::trace()
  78. << common::TraceElement<common::DoubleTime>(get_name(), t,
  79. common::FormalismType::DEVS,
  80. common::FunctionType::DELTA_EXT,
  81. common::LevelType::USER)
  82. << "event = " << msg.to_string();
  83. common::Trace<common::DoubleTime>::trace().flush();
  84. #endif
  85. _phase = SEND;
  86. }
  87. void start(typename common::DoubleTime::type t) override {
  88. #ifndef WITH_TRACE
  89. (void)t;
  90. #else
  91. common::Trace<common::DoubleTime>::trace()
  92. << common::TraceElement<common::DoubleTime>(get_name(), t,
  93. common::FormalismType::DEVS,
  94. common::FunctionType::START,
  95. common::LevelType::USER);
  96. common::Trace<common::DoubleTime>::trace().flush();
  97. #endif
  98. _phase = SEND;
  99. }
  100. typename common::DoubleTime::type
  101. ta(typename common::DoubleTime::type t) const override {
  102. #ifndef WITH_TRACE
  103. (void)t;
  104. #else
  105. common::Trace<common::DoubleTime>::trace()
  106. << common::TraceElement<common::DoubleTime>(get_name(), t,
  107. common::FormalismType::DEVS,
  108. common::FunctionType::TA,
  109. common::LevelType::USER);
  110. common::Trace<common::DoubleTime>::trace().flush();
  111. #endif
  112. if (_phase == WAIT) {
  113. return 1;
  114. } else {
  115. return 0;
  116. }
  117. }
  118. common::ExternalEvent<common::DoubleTime>
  119. lambda(typename common::DoubleTime::type t) const override {
  120. #ifndef WITH_TRACE
  121. (void)t;
  122. #endif
  123. if (_phase == SEND) {
  124. artis::common::ExternalEvent<common::DoubleTime> msg(_value);
  125. #ifdef WITH_TRACE
  126. common::Trace<common::DoubleTime>::trace()
  127. << common::TraceElement<common::DoubleTime>(get_name(), t,
  128. common::FormalismType::DEVS,
  129. common::FunctionType::LAMBDA,
  130. common::LevelType::USER)
  131. << "event = " << msg.to_string();
  132. common::Trace<common::DoubleTime>::trace().flush();
  133. #endif
  134. return msg;
  135. } else {
  136. #ifdef WITH_TRACE
  137. common::Trace<common::DoubleTime>::trace()
  138. << common::TraceElement<common::DoubleTime>(get_name(), t,
  139. common::FormalismType::DEVS,
  140. common::FunctionType::LAMBDA,
  141. common::LevelType::USER)
  142. << "no event";
  143. common::Trace<common::DoubleTime>::trace().flush();
  144. #endif
  145. return artis::common::ExternalEvent<common::DoubleTime>::Void;
  146. }
  147. }
  148. private:
  149. enum Phase {
  150. WAIT, SEND
  151. };
  152. Phase _phase;
  153. data _value;
  154. };
  155. class B : public artis::devs::Dynamics<common::DoubleTime, B> {
  156. public:
  157. B(const std::string &name,
  158. const artis::devs::Context<common::DoubleTime, B, artis::common::NoParameters> &context)
  159. :
  160. artis::devs::Dynamics<common::DoubleTime, B>(name, context),
  161. _value(0) {
  162. }
  163. ~B() override = default;
  164. void dint(typename common::DoubleTime::type t) override {
  165. #ifndef WITH_TRACE
  166. (void)t;
  167. #else
  168. common::Trace<common::DoubleTime>::trace()
  169. << common::TraceElement<common::DoubleTime>(get_name(), t,
  170. common::FormalismType::DEVS,
  171. common::FunctionType::DELTA_INT,
  172. common::LevelType::USER);
  173. common::Trace<common::DoubleTime>::trace().flush();
  174. #endif
  175. if (_phase == SEND) {
  176. _phase = WAIT;
  177. }
  178. }
  179. void
  180. dext(typename common::DoubleTime::type t, typename common::DoubleTime::type /* e */,
  181. const common::ExternalEvent<common::DoubleTime> &msg) override {
  182. #ifndef WITH_TRACE
  183. (void)t;
  184. (void)msgs;
  185. #else
  186. common::Trace<common::DoubleTime>::trace()
  187. << common::TraceElement<common::DoubleTime>(get_name(), t,
  188. common::FormalismType::DEVS,
  189. common::FunctionType::DELTA_EXT,
  190. common::LevelType::USER)
  191. << "event = " << msg.to_string();
  192. common::Trace<common::DoubleTime>::trace().flush();
  193. #endif
  194. _phase = SEND;
  195. }
  196. void start(typename common::DoubleTime::type t) override {
  197. #ifndef WITH_TRACE
  198. (void)t;
  199. #else
  200. common::Trace<common::DoubleTime>::trace()
  201. << common::TraceElement<common::DoubleTime>(get_name(), t,
  202. common::FormalismType::DEVS,
  203. common::FunctionType::START,
  204. common::LevelType::USER);
  205. common::Trace<common::DoubleTime>::trace().flush();
  206. #endif
  207. _phase = WAIT;
  208. }
  209. typename common::DoubleTime::type ta(
  210. typename common::DoubleTime::type t) const override {
  211. #ifndef WITH_TRACE
  212. (void)t;
  213. #else
  214. common::Trace<common::DoubleTime>::trace()
  215. << common::TraceElement<common::DoubleTime>(get_name(), t,
  216. common::FormalismType::DEVS,
  217. common::FunctionType::TA,
  218. common::LevelType::USER);
  219. common::Trace<common::DoubleTime>::trace().flush();
  220. #endif
  221. if (_phase == WAIT) {
  222. return common::DoubleTime::infinity;
  223. } else {
  224. return 0;
  225. }
  226. }
  227. common::ExternalEvent<common::DoubleTime> lambda(
  228. typename common::DoubleTime::type t) const override {
  229. #ifndef WITH_TRACE
  230. (void)t;
  231. #endif
  232. if (_phase == SEND) {
  233. artis::common::ExternalEvent<common::DoubleTime> msg(_value);
  234. #ifdef WITH_TRACE
  235. common::Trace<common::DoubleTime>::trace()
  236. << common::TraceElement<common::DoubleTime>(get_name(), t,
  237. common::FormalismType::DEVS,
  238. common::FunctionType::LAMBDA,
  239. common::LevelType::USER)
  240. << "event = " << msg.to_string();
  241. common::Trace<common::DoubleTime>::trace().flush();
  242. #endif
  243. return msg;
  244. } else {
  245. #ifdef WITH_TRACE
  246. common::Trace<common::DoubleTime>::trace()
  247. << common::TraceElement<common::DoubleTime>(get_name(), t,
  248. common::FormalismType::DEVS,
  249. common::FunctionType::LAMBDA,
  250. common::LevelType::USER)
  251. << "no event";
  252. common::Trace<common::DoubleTime>::trace().flush();
  253. #endif
  254. return artis::common::ExternalEvent<common::DoubleTime>::Void;
  255. }
  256. }
  257. private:
  258. enum Phase {
  259. WAIT, SEND
  260. };
  261. Phase _phase;
  262. double _value;
  263. };
  264. }
  265. }
  266. } // namespace artis tests devs
  267. #endif