devs_examples.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @file devs_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/devs_examples.hpp>
  26. #include <devs/Coordinator.hpp>
  27. namespace paradevs { namespace devs {
  28. void A::dint(const common::Time& t)
  29. {
  30. std::cout << "[ model " << get_name() << " ] dint at " << t << std::endl;
  31. if (_phase == SEND) {
  32. _phase = WAIT;
  33. }
  34. }
  35. void A::dext(const common::Time& /* e */, const common::Message& msg)
  36. {
  37. std::cout << "[ model " << get_name() << " ] dext: "
  38. << msg.get_content()
  39. << " on " << msg.get_port_name() << std::endl;
  40. _phase = SEND;
  41. }
  42. common::Time A::start()
  43. {
  44. std::cout << "[ model " << get_name() << " ] start" << std::endl;
  45. _phase = WAIT;
  46. return 0;
  47. }
  48. common::Time A::ta() const
  49. {
  50. if (_phase == WAIT) {
  51. return 1;
  52. } else {
  53. return 0;
  54. }
  55. }
  56. common::Messages A::lambda() const
  57. {
  58. std::cout << "[ model " << get_name() << " ] lambda" << std::endl;
  59. common::Messages msgs;
  60. msgs.push_back(common::Message("out", 0, true));
  61. return msgs;
  62. }
  63. void A::observation(std::ostream& /* file */) const
  64. { }
  65. void B::dint(const common::Time& t)
  66. {
  67. std::cout << "[ model " << get_name() << " ] dint at " << t
  68. << std::endl;
  69. if (_phase == SEND) {
  70. _phase = WAIT;
  71. }
  72. }
  73. void B::dext(const common::Time& /* e */, const common::Message& msg)
  74. {
  75. std::cout << "[ model " << get_name() << " ] dext: "
  76. << msg.get_content()
  77. << " on " << msg.get_port_name() << std::endl;
  78. _phase = SEND;
  79. }
  80. common::Time B::start()
  81. {
  82. std::cout << "[ model " << get_name() << " ] start" << std::endl;
  83. _phase = WAIT;
  84. return 0;
  85. }
  86. common::Time B::ta() const
  87. {
  88. if (_phase == WAIT) {
  89. return std::numeric_limits < double >::max();
  90. } else {
  91. return 0;
  92. }
  93. }
  94. common::Messages B::lambda() const
  95. {
  96. std::cout << "[ model " << get_name() << " ] lambda" << std::endl;
  97. common::Messages msgs;
  98. msgs.push_back(common::Message("out", 0, true));
  99. return msgs;
  100. }
  101. void B::observation(std::ostream& /* file */) const
  102. { }
  103. common::Model* MyBuilder::build() const
  104. {
  105. devs::Coordinator* root = new devs::Coordinator("root");
  106. devs::Coordinator* S1 = new devs::Coordinator("S1");
  107. {
  108. devs::Simulator* a = new devs::Simulator(new A("a1"));
  109. devs::Simulator* b = new devs::Simulator(new B("b1"));
  110. S1->add_child(a);
  111. S1->add_child(b);
  112. S1->add_link(common::Node("out", a), common::Node("in", b));
  113. S1->add_link(common::Node("out", b), common::Node("out", S1));
  114. }
  115. devs::Coordinator* S2 = new devs::Coordinator("S2");
  116. {
  117. devs::Simulator* a = new devs::Simulator(new A("a2"));
  118. devs::Simulator* b = new devs::Simulator(new B("b2"));
  119. S2->add_child(a);
  120. S2->add_child(b);
  121. S2->add_link(common::Node("out", a), common::Node("in", b));
  122. S2->add_link(common::Node("in", S2), common::Node("in", a));
  123. }
  124. root->add_child(S1);
  125. root->add_child(S2);
  126. root->add_link(common::Node("out", S1), common::Node("in", S2));
  127. return root;
  128. }
  129. } } // namespace paradevs devs