models.hpp 13 KB

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