common_observe.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file tests/common_observe.cpp
  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. #include <artis-star/common/RootCoordinator.hpp>
  26. #include <artis-star/common/context/Context.hpp>
  27. #include <artis-star/common/observer/Observer.hpp>
  28. #include <artis-star/common/observer/View.hpp>
  29. #include <artis-star/common/time/DoubleTime.hpp>
  30. #include <artis-star/kernel/pdevs/Coordinator.hpp>
  31. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  32. #include <artis-star/kernel/pdevs/GraphManager.hpp>
  33. #include <artis-star/kernel/pdevs/Simulator.hpp>
  34. #define BOOST_TEST_MODULE Common_Observe_Tests
  35. #include <boost/test/unit_test.hpp>
  36. /*************************************************
  37. * Tests
  38. *************************************************/
  39. template<typename M>
  40. class OnlyOneGraphManager :
  41. public artis::pdevs::GraphManager<artis::common::DoubleTime> {
  42. public:
  43. enum sub_models {
  44. OneM
  45. };
  46. OnlyOneGraphManager(artis::common::Coordinator<artis::common::DoubleTime> *coordinator,
  47. const artis::common::NoParameters &parameters,
  48. const artis::common::NoParameters &graph_parameters)
  49. :
  50. artis::pdevs::GraphManager<artis::common::DoubleTime>(coordinator, parameters, graph_parameters),
  51. model("a", parameters) {
  52. this->add_child(OneM, &model);
  53. }
  54. ~OnlyOneGraphManager() override = default;
  55. private:
  56. artis::pdevs::Simulator<artis::common::DoubleTime, M> model;
  57. };
  58. class MyDynamics : public artis::pdevs::Dynamics<artis::common::DoubleTime, MyDynamics> {
  59. public:
  60. struct vars {
  61. enum values {
  62. VALUE = 0
  63. };
  64. };
  65. MyDynamics(const std::string &name,
  66. const artis::pdevs::Context<artis::common::DoubleTime, MyDynamics, artis::common::NoParameters> &context)
  67. : artis::pdevs::Dynamics<artis::common::DoubleTime, MyDynamics, artis::common::NoParameters>(name, context),
  68. _counter(1) {
  69. observables({{vars::VALUE, "value"}});
  70. }
  71. void dint(const artis::common::DoubleTime::type & /* t */) override {
  72. if (_counter < 100) { ++_counter; } else { _counter = 1; }
  73. }
  74. void start(const artis::common::DoubleTime::type & /* t */) override {
  75. _counter = 1;
  76. }
  77. artis::common::DoubleTime::type ta(const artis::common::DoubleTime::type & /* t */) const override {
  78. return 1;
  79. }
  80. artis::common::event::Value observe(const artis::common::DoubleTime::type & /* t */,
  81. unsigned int index) const override {
  82. if (index == vars::VALUE) {
  83. return (unsigned int) _counter;
  84. }
  85. return {};
  86. }
  87. private:
  88. unsigned int _counter;
  89. };
  90. BOOST_AUTO_TEST_CASE(Common_Observe_TestCase_1)
  91. {
  92. artis::pdevs::Simulator<artis::common::DoubleTime, MyDynamics> model("A", {});
  93. artis::common::observer::View<artis::common::DoubleTime> view;
  94. artis::common::event::Value ref{(unsigned int) 1};
  95. view.attachModel(&model);
  96. view.selector("value", {MyDynamics::vars::VALUE});
  97. view.observe(0, true);
  98. BOOST_REQUIRE_EQUAL(view.get("value").back().second, ref);
  99. }
  100. BOOST_AUTO_TEST_CASE(Common_Observe_TestCase_2)
  101. {
  102. artis::pdevs::Simulator<artis::common::DoubleTime, MyDynamics> model("A", {});
  103. auto view = new artis::common::observer::View<artis::common::DoubleTime>();
  104. artis::common::observer::Observer<artis::common::DoubleTime> observer(&model);
  105. artis::common::event::Value ref{(unsigned int) 1};
  106. view->selector("value", {MyDynamics::vars::VALUE});
  107. observer.attachView("global", view);
  108. observer.init();
  109. observer.observe(0, 1, true);
  110. BOOST_REQUIRE_EQUAL(observer.views().at("global")->get("value").back().second, ref);
  111. BOOST_REQUIRE_EQUAL(observer.view("global").get("value").back().second, ref);
  112. }
  113. BOOST_AUTO_TEST_CASE(Common_Observe_TestCase_3)
  114. {
  115. artis::pdevs::Simulator<artis::common::DoubleTime, MyDynamics> model("A", {});
  116. auto view = new artis::common::observer::View<artis::common::DoubleTime>();
  117. artis::common::observer::Observer<artis::common::DoubleTime> observer(&model);
  118. artis::common::event::Value ref{(unsigned int) 1};
  119. view->selector("value", {OnlyOneGraphManager<MyDynamics>::OneM, MyDynamics::vars::VALUE});
  120. artis::common::context::Context<artis::common::DoubleTime> context(0, 200);
  121. artis::common::RootCoordinator<artis::common::DoubleTime, artis::pdevs::Coordinator<
  122. artis::common::DoubleTime, OnlyOneGraphManager<MyDynamics>,
  123. artis::common::NoParameters, artis::common::NoParameters>> rc(context, "root", artis::common::NoParameters(),
  124. artis::common::NoParameters());
  125. rc.attachView("global", view);
  126. rc.run(context);
  127. BOOST_REQUIRE_EQUAL(rc.observer().view("global").get("value").back().second, ref);
  128. }
  129. BOOST_AUTO_TEST_CASE(Common_Observe_TestCase_4)
  130. {
  131. artis::pdevs::Simulator<artis::common::DoubleTime, MyDynamics> model("A", {});
  132. auto view = new artis::common::observer::View<artis::common::DoubleTime>();
  133. artis::common::observer::Observer<artis::common::DoubleTime> observer(&model);
  134. artis::common::event::Value ref1{(unsigned int) 11};
  135. artis::common::event::Value ref2{(unsigned int) 21};
  136. view->selector("value", {OnlyOneGraphManager<MyDynamics>::OneM, MyDynamics::vars::VALUE});
  137. artis::common::context::Context<artis::common::DoubleTime> context(0, 200);
  138. artis::common::RootCoordinator<artis::common::DoubleTime, artis::pdevs::Coordinator<
  139. artis::common::DoubleTime, OnlyOneGraphManager<MyDynamics>,
  140. artis::common::NoParameters, artis::common::NoParameters>> rc(context, "root", artis::common::NoParameters(),
  141. artis::common::NoParameters());
  142. rc.attachView("global", view);
  143. rc.switch_to_timed_observer(0.1);
  144. rc.start(context);
  145. rc.run(10);
  146. BOOST_REQUIRE_EQUAL(rc.observer().view("global").get("value").back().second, ref1);
  147. BOOST_REQUIRE_EQUAL(rc.observer().view("global").get("value").size(), 100);
  148. rc.run(10);
  149. BOOST_REQUIRE_EQUAL(rc.observer().view("global").get("value").back().second, ref2);
  150. BOOST_REQUIRE_EQUAL(rc.observer().view("global").get("value").size(), 100);
  151. }