models.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * @file tests/fddevs/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-2022 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_FDDEVS_MODELS_HPP
  26. #define TESTS_FDDEVS_MODELS_HPP
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/fddevs/Dynamics.hpp>
  30. namespace artis {
  31. namespace tests {
  32. namespace fddevs {
  33. enum CRC_state_values
  34. {
  35. I0 = 0, I1, G, GR, WW, W, DW
  36. };
  37. class CRC : public artis::fddevs::Dynamics<common::DoubleTime, CRC, CRC_state_values>
  38. {
  39. public:
  40. enum inputs
  41. {
  42. IN_P
  43. };
  44. enum outputs
  45. {
  46. OUT_G, OUT_W
  47. };
  48. CRC(const std::string &name,
  49. const artis::fddevs::Context<common::DoubleTime, CRC, artis::common::NoParameters> &context)
  50. :
  51. artis::fddevs::Dynamics<common::DoubleTime, CRC, CRC_state_values>(name,
  52. context)
  53. {
  54. input_ports({{IN_P, "p"}});
  55. output_ports({{OUT_G, "g"},
  56. {OUT_W, "w"}});
  57. initial_state(I0);
  58. }
  59. ~CRC() override = default;
  60. void delta_tau(typename common::DoubleTime::type /* t */) override
  61. {
  62. switch (state()) {
  63. case I0: {
  64. state(I1);
  65. break;
  66. }
  67. case I1: {
  68. state(G);
  69. break;
  70. }
  71. case G: {
  72. state(G);
  73. break;
  74. }
  75. case GR: {
  76. state(WW);
  77. break;
  78. }
  79. case WW: {
  80. state(W);
  81. break;
  82. }
  83. case W: {
  84. state(DW);
  85. break;
  86. }
  87. case DW: {
  88. state(G);
  89. break;
  90. }
  91. }
  92. }
  93. void
  94. delta_x(typename common::DoubleTime::type /* t */,
  95. typename common::DoubleTime::type /* e */,
  96. const common::event::Bag<common::DoubleTime> &bag) override
  97. {
  98. assert(bag.size() == 1);
  99. if (bag.at(0).on_port(IN_P) and state() == G) {
  100. state(GR);
  101. }
  102. }
  103. common::event::Bag<common::DoubleTime>
  104. lambda(typename common::DoubleTime::type /* t */) const override
  105. {
  106. common::event::Bag<common::DoubleTime> msgs;
  107. switch (state()) {
  108. case I0:
  109. msgs.push_back(
  110. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_W, 0));
  111. break;
  112. case I1:
  113. msgs.push_back(
  114. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_G, 1));
  115. break;
  116. case G:break;
  117. case GR:
  118. msgs.push_back(
  119. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_G, 0));
  120. break;
  121. case WW:
  122. msgs.push_back(
  123. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_W, 1));
  124. break;
  125. case W:
  126. msgs.push_back(
  127. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_W, 0));
  128. break;
  129. case DW:
  130. msgs.push_back(
  131. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_G, 1));
  132. break;
  133. }
  134. return msgs;
  135. }
  136. bool rho(typename common::DoubleTime::type /* time */,
  137. const common::event::Bag<common::DoubleTime> &bag) const override
  138. {
  139. return state() == G and bag.at(0).on_port(IN_P);
  140. }
  141. typename common::DoubleTime::type
  142. tau(typename common::DoubleTime::type /* t */) const override
  143. {
  144. switch (state()) {
  145. case I0:return 0;
  146. case I1:return 0;
  147. case G:return 10;
  148. case GR:return 5;
  149. case WW:return 2;
  150. case W:return 26;
  151. case DW:return 2;
  152. }
  153. return common::DoubleTime::infinity;
  154. }
  155. };
  156. enum MXR_state_values
  157. {
  158. A00 = 0, A01, A10, A11, R11
  159. };
  160. class MXR : public artis::fddevs::Dynamics<common::DoubleTime, MXR, MXR_state_values>
  161. {
  162. public:
  163. enum inputs
  164. {
  165. IN_A, IN_B
  166. };
  167. MXR(const std::string &name,
  168. const artis::fddevs::Context<common::DoubleTime, MXR, artis::common::NoParameters> &context)
  169. :
  170. artis::fddevs::Dynamics<common::DoubleTime, MXR, MXR_state_values>(name,
  171. context)
  172. {
  173. input_ports({{IN_A, "a"},
  174. {IN_B, "b"}});
  175. initial_state(A00);
  176. }
  177. ~MXR() override = default;
  178. void delta_tau(typename common::DoubleTime::type /* t */) override
  179. {
  180. if (state() == A11) {
  181. state(R11);
  182. }
  183. }
  184. void
  185. delta_x(typename common::DoubleTime::type /* t */,
  186. typename common::DoubleTime::type /* e */,
  187. const common::event::Bag<common::DoubleTime> &bag) override
  188. {
  189. std::for_each(bag.begin(), bag.end(),
  190. [this](const artis::common::event::ExternalEvent<common::DoubleTime> &e) {
  191. int data;
  192. e.data()(data);
  193. if (e.on_port(IN_A)) {
  194. switch (state()) {
  195. case A00: {
  196. if (data == 1) {
  197. state(A01);
  198. }
  199. break;
  200. }
  201. case A01: {
  202. if (data == 0) {
  203. state(A00);
  204. }
  205. break;
  206. }
  207. case A10: {
  208. if (data == 1) {
  209. state(A11);
  210. }
  211. break;
  212. }
  213. case A11: {
  214. if (data == 0) {
  215. state(A10);
  216. }
  217. break;
  218. }
  219. case R11:break;
  220. }
  221. } else if (e.on_port(IN_B)) {
  222. switch (state()) {
  223. case A00: {
  224. if (data == 1) {
  225. state(A10);
  226. }
  227. break;
  228. }
  229. case A01: {
  230. if (data == 1) {
  231. state(A11);
  232. }
  233. break;
  234. }
  235. case A10: {
  236. if (data == 0) {
  237. state(A00);
  238. }
  239. break;
  240. }
  241. case A11: {
  242. if (data == 0) {
  243. state(A01);
  244. }
  245. break;
  246. }
  247. case R11:break;
  248. }
  249. }
  250. });
  251. }
  252. common::event::Bag<common::DoubleTime>
  253. lambda(typename common::DoubleTime::type /* t */) const override
  254. {
  255. common::event::Bag<common::DoubleTime> msgs;
  256. return msgs;
  257. }
  258. bool rho(typename common::DoubleTime::type /* time */,
  259. const common::event::Bag<common::DoubleTime> & /* bag */) const override
  260. {
  261. return false;
  262. }
  263. typename common::DoubleTime::type
  264. tau(typename common::DoubleTime::type /* t */) const override
  265. {
  266. if (state() == A11) {
  267. return 0.01;
  268. } else {
  269. return common::DoubleTime::infinity;
  270. }
  271. }
  272. };
  273. enum Beep_state_values
  274. {
  275. INIT = 0, SEND
  276. };
  277. class Beep
  278. : public artis::fddevs::Dynamics<common::DoubleTime, Beep, Beep_state_values>
  279. {
  280. public:
  281. enum outputs
  282. {
  283. OUT_P
  284. };
  285. Beep(const std::string &name,
  286. const artis::fddevs::Context<common::DoubleTime, Beep, artis::common::NoParameters> &context)
  287. :
  288. artis::fddevs::Dynamics<common::DoubleTime, Beep, Beep_state_values>(name,
  289. context)
  290. {
  291. output_ports({{OUT_P, "p"}});
  292. initial_state(INIT);
  293. }
  294. ~Beep() override = default;
  295. void delta_tau(typename common::DoubleTime::type /* t */) override
  296. {
  297. if (state() == INIT) {
  298. state(SEND);
  299. }
  300. }
  301. common::event::Bag<common::DoubleTime>
  302. lambda(typename common::DoubleTime::type /* t */) const override
  303. {
  304. common::event::Bag<common::DoubleTime> msgs;
  305. switch (state()) {
  306. case INIT:
  307. msgs.push_back(
  308. artis::common::event::ExternalEvent<common::DoubleTime>(OUT_P, 1));
  309. break;
  310. case SEND:break;
  311. }
  312. return msgs;
  313. }
  314. bool rho(typename common::DoubleTime::type /* time */,
  315. const common::event::Bag<common::DoubleTime> & /* bag */) const override
  316. {
  317. return false;
  318. }
  319. typename common::DoubleTime::type
  320. tau(typename common::DoubleTime::type /* t */) const override
  321. {
  322. switch (state()) {
  323. case INIT:return 27;
  324. case SEND:return common::DoubleTime::infinity;
  325. }
  326. return common::DoubleTime::infinity;
  327. }
  328. };
  329. }
  330. }
  331. } // namespace artis tests fddevs
  332. #endif