models.hpp 9.7 KB

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