AbstractAtomicModel.hpp 7.5 KB

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