models.hpp 8.9 KB

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