models.hpp 12 KB

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