AbstractAtomicModel.hpp 8.6 KB

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