Examples.cpp 3.6 KB

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