AbstractAtomicModel.hpp 9.9 KB

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