models.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @file tests/pdevs/models.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. #ifndef TESTS_PDEVS_MODELS_HPP
  26. #define TESTS_PDEVS_MODELS_HPP 1
  27. #include <common/Time.hpp>
  28. #include <common/Trace.hpp>
  29. #include <pdevs/Dynamics.hpp>
  30. #include <limits>
  31. namespace paradevs { namespace tests { namespace pdevs {
  32. template < typename T >
  33. struct Limits
  34. {
  35. static constexpr T negative_infinity =
  36. -std::numeric_limits < T >::infinity();
  37. static constexpr T positive_infinity =
  38. std::numeric_limits < T >::infinity();
  39. static constexpr T null = 0;
  40. };
  41. typedef paradevs::common::Time < double, Limits < double > > MyTime;
  42. class A : public paradevs::pdevs::Dynamics < MyTime >
  43. {
  44. public:
  45. A(const std::string& name, const common::NoParameters& parameters) :
  46. paradevs::pdevs::Dynamics < MyTime >(name, parameters)
  47. { }
  48. virtual ~A()
  49. { }
  50. void dint(typename MyTime::type t)
  51. {
  52. common::Trace < MyTime >::trace()
  53. << common::TraceElement < MyTime >(get_name(), t,
  54. common::DELTA_INT);
  55. common::Trace < MyTime >::trace().flush();
  56. if (_phase == SEND) {
  57. _phase = WAIT;
  58. }
  59. }
  60. void dext(typename MyTime::type t, typename MyTime::type /* e */,
  61. const common::Bag < MyTime >& msgs)
  62. {
  63. common::Trace < MyTime >::trace()
  64. << common::TraceElement < MyTime >(get_name(), t,
  65. common::DELTA_EXT)
  66. << "messages = " << msgs.to_string();
  67. common::Trace < MyTime >::trace().flush();
  68. _phase = SEND;
  69. }
  70. void dconf(typename MyTime::type t, typename MyTime::type /* e */,
  71. const common::Bag < MyTime >& msgs)
  72. {
  73. common::Trace < MyTime >::trace()
  74. << common::TraceElement < MyTime >(get_name(), t,
  75. common::DELTA_CONF)
  76. << "messages = " << msgs.to_string();
  77. common::Trace < MyTime >::trace().flush();
  78. }
  79. typename MyTime::type start(typename MyTime::type t)
  80. {
  81. common::Trace < MyTime >::trace()
  82. << common::TraceElement < MyTime >(get_name(), t,
  83. common::START);
  84. common::Trace < MyTime >::trace().flush();
  85. _phase = WAIT;
  86. return 0;
  87. }
  88. typename MyTime::type ta(typename MyTime::type t) const
  89. {
  90. common::Trace < MyTime >::trace()
  91. << common::TraceElement < MyTime >(get_name(), t,
  92. common::TA);
  93. common::Trace < MyTime >::trace().flush();
  94. if (_phase == WAIT) {
  95. return 1;
  96. } else {
  97. return 0;
  98. }
  99. }
  100. common::Bag < MyTime > lambda(typename MyTime::type t) const
  101. {
  102. common::Bag < MyTime > msgs;
  103. msgs.push_back(common::ExternalEvent < MyTime >("out", 0.));
  104. common::Trace < MyTime >::trace()
  105. << common::TraceElement < MyTime >(get_name(), t,
  106. common::LAMBDA)
  107. << "messages = " << msgs.to_string();
  108. common::Trace < MyTime >::trace().flush();
  109. return msgs;
  110. }
  111. void observation(std::ostream& /* file */) const
  112. { }
  113. private:
  114. enum Phase { WAIT, SEND };
  115. Phase _phase;
  116. };
  117. class B : public paradevs::pdevs::Dynamics < MyTime >
  118. {
  119. public:
  120. B(const std::string& name, const common::NoParameters& parameters) :
  121. paradevs::pdevs::Dynamics < MyTime >(name, parameters)
  122. { }
  123. virtual ~B()
  124. { }
  125. void dint(typename MyTime::type t)
  126. {
  127. common::Trace < MyTime >::trace()
  128. << common::TraceElement < MyTime >(get_name(), t,
  129. common::DELTA_INT);
  130. common::Trace < MyTime >::trace().flush();
  131. if (_phase == SEND) {
  132. _phase = WAIT;
  133. }
  134. }
  135. void dext(typename MyTime::type t, typename MyTime::type /* e */,
  136. const common::Bag < MyTime >& msgs)
  137. {
  138. common::Trace < MyTime >::trace()
  139. << common::TraceElement < MyTime >(get_name(), t,
  140. common::DELTA_EXT)
  141. << "messages = " << msgs.to_string();
  142. common::Trace < MyTime >::trace().flush();
  143. _phase = SEND;
  144. }
  145. void dconf(typename MyTime::type t, typename MyTime::type /* e */,
  146. const common::Bag < MyTime >& msgs)
  147. {
  148. common::Trace < MyTime >::trace()
  149. << common::TraceElement < MyTime >(get_name(), t,
  150. common::DELTA_CONF)
  151. << "messages = " << msgs.to_string();
  152. common::Trace < MyTime >::trace().flush();
  153. }
  154. typename MyTime::type start(typename MyTime::type t)
  155. {
  156. common::Trace < MyTime >::trace()
  157. << common::TraceElement < MyTime >(get_name(), t,
  158. common::START);
  159. common::Trace < MyTime >::trace().flush();
  160. _phase = WAIT;
  161. return std::numeric_limits < double >::max();
  162. }
  163. typename MyTime::type ta(typename MyTime::type t) const
  164. {
  165. common::Trace < MyTime >::trace()
  166. << common::TraceElement < MyTime >(get_name(), t,
  167. common::TA);
  168. common::Trace < MyTime >::trace().flush();
  169. if (_phase == WAIT) {
  170. return std::numeric_limits < double >::max();
  171. } else {
  172. return 0;
  173. }
  174. }
  175. common::Bag < MyTime > lambda(typename MyTime::type t) const
  176. {
  177. common::Bag < MyTime > msgs;
  178. msgs.push_back(common::ExternalEvent < MyTime >("out", 0.));
  179. common::Trace < MyTime >::trace()
  180. << common::TraceElement < MyTime >(get_name(), t,
  181. common::LAMBDA)
  182. << "messages = " << msgs.to_string();
  183. common::Trace < MyTime >::trace().flush();
  184. return msgs;
  185. }
  186. void observation(std::ostream& /* file */) const
  187. { }
  188. private:
  189. enum Phase { WAIT, SEND };
  190. Phase _phase;
  191. };
  192. } } } // namespace paradevs tests pdevs
  193. #endif