mixed_tests.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * @file mixed_examples.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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. #include <common/RootCoordinator.hpp>
  26. #include <common/scheduler/VectorScheduler.hpp>
  27. #include <common/scheduler/HeapScheduler.hpp>
  28. #include <common/Time.hpp>
  29. #include <common/Trace.hpp>
  30. #include <dtss/Coordinator.hpp>
  31. #include <dtss/Dynamics.hpp>
  32. #include <dtss/GraphManager.hpp>
  33. #include <dtss/Simulator.hpp>
  34. #include <pdevs/Coordinator.hpp>
  35. #include <pdevs/Dynamics.hpp>
  36. #include <pdevs/GraphManager.hpp>
  37. #include <pdevs/Simulator.hpp>
  38. namespace paradevs {
  39. template < typename T >
  40. struct Limits
  41. {
  42. static constexpr T negative_infinity =
  43. -std::numeric_limits < T >::infinity();
  44. static constexpr T positive_infinity =
  45. std::numeric_limits < T >::infinity();
  46. static constexpr T null = 0;
  47. };
  48. typedef paradevs::common::Time < double, Limits < double > > MyTime;
  49. class A1 : public paradevs::pdevs::Dynamics < MyTime >
  50. {
  51. public:
  52. A1(const std::string& name) : paradevs::pdevs::Dynamics < MyTime >(name)
  53. { }
  54. virtual ~A1()
  55. { }
  56. void dint(typename MyTime::type t)
  57. {
  58. common::Trace < MyTime >::trace() <<
  59. common::TraceElement < MyTime >(get_name(), t,
  60. common::DELTA_INT);
  61. common::Trace < MyTime >::trace().flush();
  62. if (_phase == SEND) {
  63. _phase = WAIT;
  64. }
  65. }
  66. void dext(typename MyTime::type t, typename MyTime::type /* e */,
  67. const common::Bag < MyTime >& msgs)
  68. {
  69. common::Trace < MyTime >::trace()
  70. << common::TraceElement < MyTime >(get_name(), t,
  71. common::DELTA_EXT)
  72. << "messages = " << msgs.to_string();
  73. common::Trace < MyTime >::trace().flush();
  74. _phase = SEND;
  75. }
  76. void dconf(typename MyTime::type t, typename MyTime::type /* e */,
  77. const common::Bag < MyTime >& msgs)
  78. {
  79. common::Trace < MyTime >::trace()
  80. << common::TraceElement < MyTime >(get_name(), t,
  81. common::DELTA_CONF)
  82. << "messages = " << msgs.to_string();
  83. common::Trace < MyTime >::trace().flush();
  84. }
  85. typename MyTime::type start(typename MyTime::type t)
  86. {
  87. common::Trace < MyTime >::trace()
  88. << common::TraceElement < MyTime >(get_name(), t,
  89. common::START);
  90. common::Trace < MyTime >::trace().flush();
  91. _phase = WAIT;
  92. return 0;
  93. }
  94. typename MyTime::type ta(typename MyTime::type t) const
  95. {
  96. common::Trace < MyTime >::trace()
  97. << common::TraceElement < MyTime >(get_name(), t,
  98. common::TA);
  99. common::Trace < MyTime >::trace().flush();
  100. if (_phase == WAIT) {
  101. return 1;
  102. } else {
  103. return 0;
  104. }
  105. }
  106. common::Bag < MyTime > lambda(typename MyTime::type t) const
  107. {
  108. common::Bag < MyTime > msgs;
  109. msgs.push_back(common::ExternalEvent < MyTime >("out", 0.));
  110. common::Trace < MyTime >::trace()
  111. << common::TraceElement < MyTime >(get_name(), t,
  112. common::LAMBDA)
  113. << "messages = " << msgs.to_string();
  114. common::Trace < MyTime >::trace().flush();
  115. return msgs;
  116. }
  117. private:
  118. enum Phase { WAIT, SEND };
  119. Phase _phase;
  120. };
  121. class B1 : public paradevs::pdevs::Dynamics < MyTime >
  122. {
  123. public:
  124. B1(const std::string& name) : paradevs::pdevs::Dynamics < MyTime >(name)
  125. { }
  126. virtual ~B1()
  127. { }
  128. void dint(typename MyTime::type t)
  129. {
  130. common::Trace < MyTime >::trace()
  131. << common::TraceElement < MyTime >(get_name(), t,
  132. common::DELTA_INT);
  133. common::Trace < MyTime >::trace().flush();
  134. if (_phase == SEND) {
  135. _phase = WAIT;
  136. }
  137. }
  138. void dext(typename MyTime::type t, typename MyTime::type /* e */,
  139. const common::Bag < MyTime >& msgs)
  140. {
  141. common::Trace < MyTime >::trace()
  142. << common::TraceElement < MyTime >(get_name(), t,
  143. common::DELTA_EXT)
  144. << "messages = " << msgs.to_string();
  145. common::Trace < MyTime >::trace().flush();
  146. _phase = SEND;
  147. }
  148. void dconf(typename MyTime::type t, typename MyTime::type /* e */,
  149. const common::Bag < MyTime >& msgs)
  150. {
  151. common::Trace < MyTime >::trace()
  152. << common::TraceElement < MyTime >(get_name(), t,
  153. common::DELTA_CONF)
  154. << "messages = " << msgs.to_string();
  155. common::Trace < MyTime >::trace().flush();
  156. }
  157. typename MyTime::type start(typename MyTime::type t)
  158. {
  159. common::Trace < MyTime >::trace()
  160. << common::TraceElement < MyTime >(get_name(), t,
  161. common::START);
  162. common::Trace < MyTime >::trace().flush();
  163. _phase = WAIT;
  164. return std::numeric_limits < double >::max();
  165. }
  166. typename MyTime::type ta(typename MyTime::type t) const
  167. {
  168. common::Trace < MyTime >::trace()
  169. << common::TraceElement < MyTime >(get_name(), t,
  170. common::TA);
  171. common::Trace < MyTime >::trace().flush();
  172. if (_phase == WAIT) {
  173. return std::numeric_limits < double >::max();
  174. } else {
  175. return 0;
  176. }
  177. }
  178. common::Bag < MyTime > lambda(typename MyTime::type t) const
  179. {
  180. common::Bag < MyTime > msgs;
  181. msgs.push_back(common::ExternalEvent < MyTime >("out", t));
  182. common::Trace < MyTime >::trace()
  183. << common::TraceElement < MyTime >(get_name(), t,
  184. common::LAMBDA)
  185. << "messages = " << msgs.to_string();
  186. common::Trace < MyTime >::trace().flush();
  187. return msgs;
  188. }
  189. private:
  190. enum Phase { WAIT, SEND };
  191. Phase _phase;
  192. };
  193. class A2 : public paradevs::dtss::Dynamics < MyTime >
  194. {
  195. public:
  196. A2(const std::string& name) : paradevs::dtss::Dynamics < MyTime >(name)
  197. { }
  198. virtual ~A2()
  199. { }
  200. void transition(const common::Bag < MyTime >& x, typename MyTime::type t)
  201. {
  202. common::Trace < MyTime >::trace()
  203. << common::TraceElement < MyTime >(get_name(), t,
  204. common::DELTA_INT)
  205. << "x = " << x.to_string();
  206. common::Trace < MyTime >::trace().flush();
  207. }
  208. typename MyTime::type start(typename MyTime::type t)
  209. {
  210. common::Trace < MyTime >::trace()
  211. << common::TraceElement < MyTime >(get_name(), t,
  212. common::START);
  213. common::Trace < MyTime >::trace().flush();
  214. return 0;
  215. }
  216. common::Bag < MyTime > lambda(typename MyTime::type t) const
  217. {
  218. common::Bag < MyTime > msgs;
  219. msgs.push_back(common::ExternalEvent < MyTime >("out", 0.));
  220. common::Trace < MyTime >::trace()
  221. << common::TraceElement < MyTime >(get_name(), t,
  222. common::LAMBDA)
  223. << "messages = " << msgs.to_string();
  224. common::Trace < MyTime >::trace().flush();
  225. return msgs;
  226. }
  227. };
  228. class B2 : public paradevs::dtss::Dynamics < MyTime >
  229. {
  230. public:
  231. B2(const std::string& name) : paradevs::dtss::Dynamics < MyTime >(name)
  232. { }
  233. virtual ~B2()
  234. { }
  235. void transition(const common::Bag < MyTime >& x, typename MyTime::type t)
  236. {
  237. common::Trace < MyTime >::trace()
  238. << common::TraceElement < MyTime >(get_name(), t,
  239. common::DELTA_INT)
  240. << "x = " << x.to_string();
  241. common::Trace < MyTime >::trace().flush();
  242. }
  243. typename MyTime::type start(typename MyTime::type t)
  244. {
  245. common::Trace < MyTime >::trace()
  246. << common::TraceElement < MyTime >(get_name(), t,
  247. common::START);
  248. common::Trace < MyTime >::trace().flush();
  249. return 0;
  250. }
  251. common::Bag < MyTime > lambda(typename MyTime::type t) const
  252. {
  253. common::Bag < MyTime > msgs;
  254. msgs.push_back(common::ExternalEvent < MyTime >("out", 0.));
  255. common::Trace < MyTime >::trace()
  256. << common::TraceElement < MyTime >(get_name(), t,
  257. common::LAMBDA)
  258. << "messages = " << msgs.to_string();
  259. common::Trace < MyTime >::trace().flush();
  260. return msgs;
  261. }
  262. };
  263. class Beep : public paradevs::pdevs::Dynamics < MyTime >
  264. {
  265. public:
  266. Beep(const std::string& name) : paradevs::pdevs::Dynamics < MyTime >(name)
  267. { }
  268. virtual ~Beep()
  269. { }
  270. void dint(typename MyTime::type t)
  271. {
  272. common::Trace < MyTime >::trace() <<
  273. common::TraceElement < MyTime >(get_name(), t,
  274. common::DELTA_INT);
  275. common::Trace < MyTime >::trace().flush();
  276. if (_phase == SEND) {
  277. _phase = WAIT;
  278. }
  279. }
  280. void dext(typename MyTime::type t, typename MyTime::type /* e */,
  281. const common::Bag < MyTime >& msgs)
  282. {
  283. common::Trace < MyTime >::trace()
  284. << common::TraceElement < MyTime >(get_name(), t,
  285. common::DELTA_EXT)
  286. << "messages = " << msgs.to_string();
  287. common::Trace < MyTime >::trace().flush();
  288. _phase = SEND;
  289. }
  290. void dconf(typename MyTime::type t, typename MyTime::type /* e */,
  291. const common::Bag < MyTime >& msgs)
  292. {
  293. common::Trace < MyTime >::trace()
  294. << common::TraceElement < MyTime >(get_name(), t,
  295. common::DELTA_CONF)
  296. << "messages = " << msgs.to_string();
  297. common::Trace < MyTime >::trace().flush();
  298. }
  299. typename MyTime::type start(typename MyTime::type t)
  300. {
  301. common::Trace < MyTime >::trace()
  302. << common::TraceElement < MyTime >(get_name(), t,
  303. common::START);
  304. common::Trace < MyTime >::trace().flush();
  305. _phase = WAIT;
  306. return 0;
  307. }
  308. typename MyTime::type ta(typename MyTime::type t) const
  309. {
  310. common::Trace < MyTime >::trace()
  311. << common::TraceElement < MyTime >(get_name(), t,
  312. common::TA);
  313. common::Trace < MyTime >::trace().flush();
  314. if (_phase == WAIT) {
  315. return (rand() % 100) / 10.;
  316. } else {
  317. return 0;
  318. }
  319. }
  320. common::Bag < MyTime > lambda(typename MyTime::type t) const
  321. {
  322. common::Bag < MyTime > msgs;
  323. msgs.push_back(common::ExternalEvent < MyTime >("out", 0.));
  324. common::Trace < MyTime >::trace()
  325. << common::TraceElement < MyTime >(get_name(), t,
  326. common::LAMBDA)
  327. << "messages = " << msgs.to_string();
  328. common::Trace < MyTime >::trace().flush();
  329. return msgs;
  330. }
  331. private:
  332. enum Phase { WAIT, SEND };
  333. Phase _phase;
  334. };
  335. struct LastBagPolicy
  336. {
  337. const common::Bag < MyTime >& bag() const
  338. { return _bag; }
  339. virtual void operator()(typename MyTime::type /* t */,
  340. const common::ExternalEvent < MyTime >& event,
  341. typename MyTime::type /* tl */,
  342. typename MyTime::type /* tn */)
  343. {
  344. _bag.clear();
  345. _bag.push_back(event);
  346. }
  347. private:
  348. common::Bag < MyTime > _bag;
  349. };
  350. struct IgnorePolicy
  351. {
  352. const common::Bag < MyTime >& bag() const
  353. { return _bag; }
  354. virtual void operator()(typename MyTime::type /* t */,
  355. const common::ExternalEvent < MyTime >& /* event */,
  356. typename MyTime::type /* tl */,
  357. typename MyTime::type /* tn */)
  358. { }
  359. private:
  360. common::Bag < MyTime > _bag;
  361. };
  362. class S1GraphManager : public pdevs::GraphManager < MyTime >
  363. {
  364. public:
  365. S1GraphManager(common::Coordinator < MyTime >* coordinator) :
  366. pdevs::GraphManager < MyTime >(coordinator), a("a1"), b("b1")
  367. {
  368. add_child(&a);
  369. add_child(&b);
  370. add_link(&a, "out", &b, "in");
  371. add_link(&b, "out", coordinator, "out");
  372. }
  373. virtual ~S1GraphManager()
  374. { }
  375. private:
  376. pdevs::Simulator < MyTime, A1 > a;
  377. pdevs::Simulator < MyTime, B1 > b;
  378. };
  379. class S2GraphManager : public dtss::GraphManager < MyTime >
  380. {
  381. public:
  382. S2GraphManager(common::Coordinator < MyTime >* coordinator) :
  383. dtss::GraphManager < MyTime >(coordinator), a("a2", 20), b("b2", 20)
  384. {
  385. add_child(&a);
  386. add_child(&b);
  387. add_link(&a, "out", &b, "in");
  388. add_link(coordinator, "in", &a, "in");
  389. }
  390. virtual ~S2GraphManager()
  391. { }
  392. private:
  393. dtss::Simulator < MyTime, A2 > a;
  394. dtss::Simulator < MyTime, B2 > b;
  395. };
  396. class RootGraphManager : public pdevs::GraphManager < MyTime >
  397. {
  398. public:
  399. RootGraphManager(common::Coordinator < MyTime >* coordinator) :
  400. pdevs::GraphManager < MyTime >(coordinator),
  401. S1("S1", paradevs::pdevs::Parameters()),
  402. S2("S2", paradevs::dtss::Parameters < MyTime >(20))
  403. {
  404. add_child(&S1);
  405. add_child(&S2);
  406. add_link(&S1, "out", &S2, "in");
  407. }
  408. virtual ~RootGraphManager()
  409. { }
  410. private:
  411. pdevs::Coordinator < MyTime,
  412. paradevs::common::scheduler::HeapScheduler <
  413. MyTime >,
  414. S1GraphManager > S1;
  415. dtss::Coordinator < MyTime, LastBagPolicy, S2GraphManager > S2;
  416. };
  417. template < int size >
  418. class LinearGraphManager : public pdevs::GraphManager < MyTime >
  419. {
  420. public:
  421. LinearGraphManager(common::Coordinator < MyTime >* coordinator) :
  422. pdevs::GraphManager < MyTime >(coordinator)
  423. {
  424. for (unsigned int i = 1; i <= size; ++i) {
  425. std::ostringstream ss;
  426. ss << "a" << i;
  427. _models.push_back(new pdevs::Simulator < MyTime, Beep >(ss.str()));
  428. }
  429. for (unsigned int i = 0; i < size; ++i) {
  430. add_child(_models[i]);
  431. }
  432. for (unsigned int i = 0; i < size - 1; ++i) {
  433. add_link(_models[i], "out", _models[i + 1], "in");
  434. }
  435. }
  436. virtual ~LinearGraphManager()
  437. {
  438. for (unsigned int i = 0; i < size; ++i) {
  439. delete _models[i];
  440. }
  441. }
  442. private:
  443. std::vector < pdevs::Simulator < MyTime, Beep >* > _models;
  444. };
  445. class Linear2GraphManager : public pdevs::GraphManager < MyTime >
  446. {
  447. public:
  448. Linear2GraphManager(common::Coordinator < MyTime >* coordinator) :
  449. pdevs::GraphManager < MyTime >(coordinator)
  450. {
  451. for (unsigned int i = 1; i <= 100; ++i) {
  452. std::ostringstream ss;
  453. ss << "a" << i;
  454. _models.push_back(new pdevs::Simulator < MyTime, Beep >(ss.str()));
  455. }
  456. for (unsigned int i = 0; i < 100; ++i) {
  457. add_child(_models[i]);
  458. }
  459. for (unsigned int i = 0; i < 99; ++i) {
  460. add_link(_models[i], "out", _models[i + 1], "in");
  461. }
  462. add_link(coordinator, "in", _models[0], "in");
  463. add_link(_models[99], "out", coordinator, "out");
  464. }
  465. virtual ~Linear2GraphManager()
  466. {
  467. for (unsigned int i = 0; i < 50; ++i) {
  468. delete _models[i];
  469. }
  470. }
  471. private:
  472. std::vector < pdevs::Simulator < MyTime, Beep >* > _models;
  473. };
  474. class Root2GraphManager : public pdevs::GraphManager < MyTime >
  475. {
  476. public:
  477. Root2GraphManager(common::Coordinator < MyTime >* coordinator) :
  478. pdevs::GraphManager < MyTime >(coordinator),
  479. S1("S1", paradevs::pdevs::Parameters()),
  480. S2("S2", paradevs::pdevs::Parameters())
  481. {
  482. add_child(&S1);
  483. add_child(&S2);
  484. add_link(&S1, "out", &S2, "in");
  485. }
  486. virtual ~Root2GraphManager()
  487. { }
  488. private:
  489. pdevs::Coordinator < MyTime,
  490. paradevs::common::scheduler::HeapScheduler <
  491. MyTime >,
  492. Linear2GraphManager > S1;
  493. pdevs::Coordinator < MyTime,
  494. paradevs::common::scheduler::HeapScheduler <
  495. MyTime >,
  496. Linear2GraphManager > S2;
  497. };
  498. class Root3GraphManager : public pdevs::GraphManager < MyTime >
  499. {
  500. public:
  501. Root3GraphManager(common::Coordinator < MyTime >* coordinator) :
  502. pdevs::GraphManager < MyTime >(coordinator),
  503. S1("S1", paradevs::pdevs::Parameters()),
  504. S2("S2", paradevs::pdevs::Parameters())
  505. {
  506. add_child(&S1);
  507. add_child(&S2);
  508. add_link(&S1, "out", &S2, "in");
  509. }
  510. virtual ~Root3GraphManager()
  511. { }
  512. private:
  513. pdevs::Coordinator < MyTime,
  514. paradevs::common::scheduler::VectorScheduler <
  515. MyTime >,
  516. Linear2GraphManager > S1;
  517. pdevs::Coordinator < MyTime,
  518. paradevs::common::scheduler::VectorScheduler <
  519. MyTime >,
  520. Linear2GraphManager > S2;
  521. };
  522. } // namespace paradevs