AbstractAtomicModel.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 >(
  44. true,
  45. boost::core::demangle(typeid(T).name()), // .erase(0,6),
  46. utils::DoubleTime::null,
  47. utils::CONSTRUCT);
  48. utils::Trace < utils::DoubleTime >::trace().flush();
  49. #endif
  50. }
  51. virtual ~AbstractAtomicModel()
  52. {
  53. #ifdef WITH_TRACE
  54. trace_element(true, utils::DoubleTime::null,
  55. utils::DESTRUCT);
  56. #endif
  57. }
  58. virtual const Node < U >* atomic(unsigned int /* index */) const
  59. { return this; }
  60. virtual void after(typename U::type t)
  61. {
  62. #ifdef WITH_TRACE
  63. trace_element(true, t, utils::AFTER_COMPUTE);
  64. trace_internals(true, t, utils::AFTER_COMPUTE);
  65. #else
  66. (void) t;
  67. #endif
  68. }
  69. virtual void before(typename U::type t)
  70. {
  71. #ifdef WITH_TRACE
  72. trace_element(true, t, utils::BEFORE_COMPUTE);
  73. trace_internals(true, t, utils::BEFORE_COMPUTE);
  74. trace_externals(true, t, utils::BEFORE_COMPUTE);
  75. #else
  76. (void) t;
  77. #endif
  78. }
  79. virtual bool check(typename U::type t) const
  80. { return Externals < T, U, V >::check(t); }
  81. virtual void compute(typename U::type t, bool update) = 0;
  82. virtual const Any& get(typename U::type t, unsigned int index) const
  83. {
  84. if (type::last_time != t) {
  85. throw utils::InvalidGet("Variable not computed");
  86. }
  87. return Internals < T, U, V >::get(index);
  88. }
  89. template < typename W >
  90. W get(typename U::type t, unsigned int index) const
  91. {
  92. if (type::last_time != t) {
  93. throw utils::InvalidGet("Variable not computed");
  94. }
  95. Any value = Internals < T, U, V >::get(index);
  96. return static_cast < const T* >(this)->*(value.get < T, W >());
  97. }
  98. virtual std::string get(const ValueType& value_type, typename U::type t,
  99. unsigned int index) const
  100. {
  101. if (type::last_time != t) {
  102. throw utils::InvalidGet("Variable not computed");
  103. }
  104. Any value = Internals < T, U, V >::get(index);
  105. switch (value_type) {
  106. case DOUBLE:
  107. {
  108. std::ostringstream ss;
  109. ss << std::setprecision(10)
  110. << static_cast < const T* >(this)->*(
  111. value.get < T, double >());
  112. return ss.str();
  113. }
  114. case INT:
  115. return std::to_string(
  116. static_cast < const T* >(this)->*(value.get < T, int >()));
  117. case BOOL:
  118. return std::to_string(
  119. static_cast < const T* >(this)->*(value.get < T, bool >()));
  120. default: return "NA";
  121. }
  122. }
  123. virtual void init(typename U::type t, const V& parameters) = 0;
  124. virtual bool is_atomic() const
  125. { return true; }
  126. bool is_computed(typename U::type t, unsigned int /* index */) const
  127. { return type::last_time == t; }
  128. bool is_stable(typename U::type t) const
  129. { return type::last_time == t; }
  130. virtual bool is_updated() const
  131. { return Externals < T, U, V >::updated; }
  132. virtual void restore(const context::State < U >& state)
  133. {
  134. Externals < T, U, V >::restore(this, state);
  135. Internals < T, U, V >::restore(this, state);
  136. States < T, U, V >::restore(this, state);
  137. type::last_time = state.last_time();
  138. }
  139. virtual void save(context::State < U >& state) const
  140. {
  141. Externals < T, U, V >::save(this, state);
  142. Internals < T, U, V >::save(this, state);
  143. States < T, U, V >::save(this, state);
  144. state.last_time(type::last_time);
  145. }
  146. virtual void stable()
  147. { Externals < T, U, V >::updated = false; }
  148. virtual void trace_element(typename U::type t, utils::TraceType type = utils::NONE, std::string comment = "") const
  149. {trace_element(false, t, type, comment);}
  150. virtual void trace_internals(typename U::type t, utils::TraceType type) const
  151. {trace_internals(false, t, type);}
  152. virtual void trace_externals(typename U::type t, utils::TraceType type = utils::NONE) const
  153. {trace_externals(false, t, type);}
  154. virtual void trace_model(typename U::type t, utils::TraceType type = utils::NONE) const
  155. {trace_model(false, t, type);}
  156. private:
  157. void trace_element(bool from_kernel, typename U::type t, utils::TraceType type = utils::NONE, std::string comment = "") const
  158. {
  159. utils::Trace < utils::DoubleTime >::trace()
  160. << utils::TraceElement < utils::DoubleTime >(
  161. from_kernel,
  162. AbstractAtomicModel < T, U, V >::path(this),
  163. t, type)
  164. << comment;
  165. utils::Trace < utils::DoubleTime >::trace().flush();
  166. }
  167. void trace_internals(bool from_kernel, typename U::type t, utils::TraceType type) const
  168. {
  169. for (size_t i = 0; i < Internals < T, U, V >::size(); ++i) {
  170. if (not Internals < T, U, V >::get(i).is_null()) {
  171. utils::Trace < utils::DoubleTime >::trace()
  172. << utils::TraceElement < utils::DoubleTime >(
  173. from_kernel,
  174. AbstractAtomicModel < T, U, V >::path(this),
  175. t, type)
  176. << utils::KernelInfo(
  177. Internals < T, U, V >::name(i), true,
  178. Internals < T, U, V >::get(i).to_string(
  179. dynamic_cast < const T* >(this))
  180. );
  181. utils::Trace < utils::DoubleTime >::trace().flush();
  182. }
  183. }
  184. }
  185. void trace_externals(bool from_kernel, typename U::type t, utils::TraceType type = utils::NONE) const
  186. {
  187. for (size_t i = 0; i < Externals < T, U, V >::size(); ++i) {
  188. if (not Externals < T, U, V >::get(i).is_null()) {
  189. utils::Trace < utils::DoubleTime >::trace()
  190. << utils::TraceElement < utils::DoubleTime >(
  191. from_kernel,
  192. AbstractAtomicModel < T, U, V >::path(this),
  193. t, type)
  194. << utils::KernelInfo(
  195. Externals < T, U, V >::name(i), false,
  196. Externals < T, U, V >::get(i).to_string(
  197. dynamic_cast < const T* >(this))
  198. );
  199. utils::Trace < utils::DoubleTime >::trace().flush();
  200. }
  201. }
  202. }
  203. void trace_model(bool from_kernel, typename U::type t, utils::TraceType type = utils::NONE) const
  204. {
  205. trace_element(from_kernel, t, type);
  206. trace_internals(from_kernel, t, type);
  207. trace_externals(from_kernel, t, type);
  208. }
  209. };
  210. } }
  211. #endif