AbstractAtomicModel.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file artis/kernel/AbstractModel.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
  22. #define __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
  23. #include <artis/kernel/AbstractModel.hpp>
  24. #include <artis/kernel/AbstractCoupledModel.hpp>
  25. #include <artis/kernel/Internals.hpp>
  26. #include <artis/kernel/States.hpp>
  27. #include <artis/utils/DateTime.hpp>
  28. #include <artis/utils/Exception.hpp>
  29. #include <sstream>
  30. namespace artis { namespace kernel {
  31. template < typename T, typename U, typename V >
  32. class AbstractAtomicModel : public AbstractModel < U, V >,
  33. public States < T, U, V >,
  34. public Internals < T, U, V >,
  35. public Externals < T, U, V >
  36. {
  37. typedef AbstractModel < U, V > type;
  38. public:
  39. AbstractAtomicModel(const type* parent = 0) : type(parent)
  40. {
  41. #ifdef WITH_TRACE
  42. utils::Trace < utils::DoubleTime >::trace()
  43. << utils::TraceElement < utils::DoubleTime >("KERNEL", -1,
  44. utils::KERNEL)
  45. << "NEW ATOMIC MODEL - "
  46. << typeid(T).name();
  47. utils::Trace < utils::DoubleTime >::trace().flush();
  48. #endif
  49. }
  50. virtual ~AbstractAtomicModel()
  51. { }
  52. virtual const Node < U >* atomic(unsigned int /* index */) const
  53. { return this; }
  54. virtual void after(typename U::type t)
  55. {
  56. #ifdef WITH_TRACE
  57. utils::Trace < utils::DoubleTime >::trace()
  58. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  59. utils::KERNEL)
  60. << "AFTER - "
  61. << AbstractAtomicModel < T, U, V >::path(this);
  62. utils::Trace < utils::DoubleTime >::trace().flush();
  63. for (size_t i = 0; i < Internals < T, U, V >::size(); ++i) {
  64. if (not Internals < T, U, V >::get(i).is_null()) {
  65. utils::Trace < utils::DoubleTime >::trace()
  66. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  67. utils::KERNEL)
  68. << Internals < T, U, V >::name(i) << " = "
  69. << Internals < T, U, V >::get(i).to_string(
  70. dynamic_cast < const T* >(this));
  71. utils::Trace < utils::DoubleTime >::trace().flush();
  72. }
  73. }
  74. #else
  75. (void) t;
  76. #endif
  77. }
  78. virtual void before(typename U::type t)
  79. {
  80. #ifdef WITH_TRACE
  81. utils::Trace < utils::DoubleTime >::trace()
  82. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  83. utils::KERNEL)
  84. << "BEFORE - "
  85. << AbstractAtomicModel < T, U, V >::path(this);
  86. utils::Trace < utils::DoubleTime >::trace().flush();
  87. for (size_t i = 0; i < Internals < T, U, V >::size(); ++i) {
  88. if (not Internals < T, U, V >::get(i).is_null()) {
  89. utils::Trace < utils::DoubleTime >::trace()
  90. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  91. utils::KERNEL)
  92. << Internals < T, U, V >::name(i) << " = "
  93. << Internals < T, U, V >::get(i).to_string(
  94. dynamic_cast < const T* >(this));
  95. utils::Trace < utils::DoubleTime >::trace().flush();
  96. }
  97. }
  98. #else
  99. (void) t;
  100. #endif
  101. }
  102. virtual bool check(typename U::type t) const
  103. { return Externals < T, U, V >::check(t); }
  104. virtual void compute(typename U::type t, bool update) = 0;
  105. virtual const Any& get(typename U::type t, unsigned int index) const
  106. {
  107. if (type::last_time != t) {
  108. throw utils::InvalidGet("Variable not computed");
  109. }
  110. return Internals < T, U, V >::get(index);
  111. }
  112. template < typename W >
  113. W get(typename U::type t, unsigned int index) const
  114. {
  115. if (type::last_time != t) {
  116. throw utils::InvalidGet("Variable not computed");
  117. }
  118. Any value = Internals < T, U, V >::get(index);
  119. return static_cast < const T* >(this)->*(value.get < T, W >());
  120. }
  121. virtual std::string get(const ValueType& value_type, typename U::type t,
  122. unsigned int index) const
  123. {
  124. if (type::last_time != t) {
  125. throw utils::InvalidGet("Variable not computed");
  126. }
  127. Any value = Internals < T, U, V >::get(index);
  128. switch (value_type) {
  129. case DOUBLE:
  130. {
  131. std::ostringstream ss;
  132. ss << std::setprecision(10)
  133. << static_cast < const T* >(this)->*(
  134. value.get < T, double >());
  135. return ss.str();
  136. }
  137. case INT:
  138. return std::to_string(
  139. static_cast < const T* >(this)->*(value.get < T, int >()));
  140. case BOOL:
  141. return std::to_string(
  142. static_cast < const T* >(this)->*(value.get < T, bool >()));
  143. default: return "NA";
  144. }
  145. }
  146. virtual void init(typename U::type t, const V& parameters) = 0;
  147. virtual bool is_atomic() const
  148. { return true; }
  149. bool is_computed(typename U::type t, unsigned int /* index */) const
  150. { return type::last_time == t; }
  151. bool is_stable(typename U::type t) const
  152. { return type::last_time == t; }
  153. virtual bool is_updated() const
  154. { return Externals < T, U, V >::updated; }
  155. virtual void restore(const context::State < U >& state)
  156. {
  157. Externals < T, U, V >::restore(this, state);
  158. Internals < T, U, V >::restore(this, state);
  159. States < T, U, V >::restore(this, state);
  160. type::last_time = state.last_time();
  161. }
  162. virtual void save(context::State < U >& state) const
  163. {
  164. Externals < T, U, V >::save(this, state);
  165. Internals < T, U, V >::save(this, state);
  166. States < T, U, V >::save(this, state);
  167. state.last_time(type::last_time);
  168. }
  169. virtual void stable()
  170. { Externals < T, U, V >::updated = false; }
  171. };
  172. } }
  173. #endif