pdevs_tests.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file examples.cpp
  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 <tests/pdevs_tests.hpp>
  26. #include <pdevs/Coordinator.hpp>
  27. #include <pdevs/RootCoordinator.hpp>
  28. #define CATCH_CONFIG_MAIN
  29. #include "catch.hpp"
  30. namespace paradevs { namespace pdevs {
  31. void A::dint(const common::Time& t)
  32. {
  33. std::cout << "[ model " << get_name() << " ] dint at " << t << std::endl;
  34. if (_phase == SEND) {
  35. _phase = WAIT;
  36. }
  37. }
  38. void A::dext(const common::Time& /* e */, const common::Messages& msgs)
  39. {
  40. std::cout << "[ model " << get_name() << " ] dext: "
  41. << msgs.to_string() << std::endl;
  42. _phase = SEND;
  43. }
  44. common::Time A::start()
  45. {
  46. std::cout << "[ model " << get_name() << " ] start" << std::endl;
  47. _phase = WAIT;
  48. return 0;
  49. }
  50. common::Time A::ta() const
  51. {
  52. if (_phase == WAIT) {
  53. return 1;
  54. } else {
  55. return 0;
  56. }
  57. }
  58. common::Messages A::lambda() const
  59. {
  60. std::cout << "[ model " << get_name() << " ] lambda" << std::endl;
  61. common::Messages msgs;
  62. msgs.push_back(common::Message("out", 0, true));
  63. return msgs;
  64. }
  65. void A::observation(std::ostream& /* file */) const
  66. { }
  67. void B::dint(const common::Time& t)
  68. {
  69. std::cout << "[ model " << get_name() << " ] dint at " << t
  70. << std::endl;
  71. if (_phase == SEND) {
  72. _phase = WAIT;
  73. }
  74. }
  75. void B::dext(const common::Time& /* e */, const common::Messages& msgs)
  76. {
  77. std::cout << "[ model " << get_name() << " ] dext: "
  78. << msgs.to_string() << std::endl;
  79. _phase = SEND;
  80. }
  81. common::Time B::start()
  82. {
  83. std::cout << "[ model " << get_name() << " ] start" << std::endl;
  84. _phase = WAIT;
  85. return 0;
  86. }
  87. common::Time B::ta() const
  88. {
  89. if (_phase == WAIT) {
  90. return std::numeric_limits < double >::max();
  91. } else {
  92. return 0;
  93. }
  94. }
  95. common::Messages B::lambda() const
  96. {
  97. std::cout << "[ model " << get_name() << " ] lambda" << std::endl;
  98. common::Messages msgs;
  99. msgs.push_back(common::Message("out", 0, true));
  100. return msgs;
  101. }
  102. void B::observation(std::ostream& /* file */) const
  103. { }
  104. common::Model* HierachicalBuilder::build() const
  105. {
  106. pdevs::Coordinator* root = new pdevs::Coordinator("root");
  107. pdevs::Coordinator* S1 = new pdevs::Coordinator("S1");
  108. {
  109. pdevs::Simulator* a = new pdevs::Simulator(new A("a1"));
  110. pdevs::Simulator* b = new pdevs::Simulator(new B("b1"));
  111. S1->add_child(a);
  112. S1->add_child(b);
  113. S1->add_link(common::Node("out", a), common::Node("in", b));
  114. S1->add_link(common::Node("out", b), common::Node("out", S1));
  115. }
  116. pdevs::Coordinator* S2 = new pdevs::Coordinator("S2");
  117. {
  118. pdevs::Simulator* a = new pdevs::Simulator(new A("a2"));
  119. pdevs::Simulator* b = new pdevs::Simulator(new B("b2"));
  120. S2->add_child(a);
  121. S2->add_child(b);
  122. S2->add_link(common::Node("out", a), common::Node("in", b));
  123. S2->add_link(common::Node("in", S2), common::Node("in", a));
  124. }
  125. root->add_child(S1);
  126. root->add_child(S2);
  127. root->add_link(common::Node("out", S1), common::Node("in", S2));
  128. return root;
  129. }
  130. common::Model* OnlyOneBuilder::build() const
  131. {
  132. pdevs::Coordinator* root = new pdevs::Coordinator("root");
  133. pdevs::Simulator* a = new pdevs::Simulator(new A("a"));
  134. root->add_child(a);
  135. return root;
  136. }
  137. common::Model* FlatBuilder::build() const
  138. {
  139. pdevs::Coordinator* root = new pdevs::Coordinator("root");
  140. pdevs::Simulator* a1 = new pdevs::Simulator(new A("a1"));
  141. pdevs::Simulator* b1 = new pdevs::Simulator(new B("b1"));
  142. pdevs::Simulator* a2 = new pdevs::Simulator(new A("a2"));
  143. pdevs::Simulator* b2 = new pdevs::Simulator(new B("b2"));
  144. root->add_child(a1);
  145. root->add_child(b1);
  146. root->add_child(a2);
  147. root->add_child(b2);
  148. root->add_link(common::Node("out", a1), common::Node("in", b1));
  149. root->add_link(common::Node("out", b1), common::Node("in", a2));
  150. root->add_link(common::Node("out", a2), common::Node("in", b2));
  151. return root;
  152. }
  153. } } // namespace paradevs pdevs
  154. // TEST_CASE("pdevs/only_one", "run")
  155. // {
  156. // paradevs::pdevs::OnlyOneBuilder builder;
  157. // paradevs::pdevs::RootCoordinator rc(0, 10, builder);
  158. // rc.run();
  159. // REQUIRE(true);
  160. // }
  161. TEST_CASE("pdevs/flat", "run")
  162. {
  163. paradevs::pdevs::FlatBuilder builder;
  164. paradevs::pdevs::RootCoordinator rc(0, 10, builder);
  165. rc.run();
  166. REQUIRE(true);
  167. }
  168. // TEST_CASE("pdevs/hierachical", "run")
  169. // {
  170. // paradevs::pdevs::HierachicalBuilder builder;
  171. // paradevs::pdevs::RootCoordinator rc(0, 10, builder);
  172. // rc.run();
  173. // REQUIRE(true);
  174. // }