AbstractAtomicModel.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. virtual ~AbstractAtomicModel()
  42. { }
  43. virtual const Node < U >* atomic(unsigned int /* index */) const
  44. { return this; }
  45. virtual void after(typename U::type t)
  46. {
  47. #ifdef WITH_TRACE
  48. utils::Trace < utils::DoubleTime >::trace()
  49. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  50. utils::KERNEL)
  51. << "AFTER - "
  52. << AbstractAtomicModel < T, U, V >::path(this);
  53. utils::Trace < utils::DoubleTime >::trace().flush();
  54. for (size_t i = 0; i < Internals < T, U, V >::size(); ++i) {
  55. if (not Internals < T, U, V >::get(i).is_null()) {
  56. utils::Trace < utils::DoubleTime >::trace()
  57. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  58. utils::KERNEL)
  59. << Internals < T, U, V >::name(i) << " = "
  60. << Internals < T, U, V >::get(i).to_string(
  61. dynamic_cast < const T* >(this));
  62. utils::Trace < utils::DoubleTime >::trace().flush();
  63. }
  64. }
  65. #else
  66. (void) t;
  67. #endif
  68. }
  69. virtual void before(typename U::type t)
  70. {
  71. #ifdef WITH_TRACE
  72. utils::Trace < utils::DoubleTime >::trace()
  73. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  74. utils::KERNEL)
  75. << "BEFORE - "
  76. << AbstractAtomicModel < T, U, V >::path(this);
  77. utils::Trace < utils::DoubleTime >::trace().flush();
  78. for (size_t i = 0; i < Internals < T, U, V >::size(); ++i) {
  79. if (not Internals < T, U, V >::get(i).is_null()) {
  80. utils::Trace < utils::DoubleTime >::trace()
  81. << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
  82. utils::KERNEL)
  83. << Internals < T, U, V >::name(i) << " = "
  84. << Internals < T, U, V >::get(i).to_string(
  85. dynamic_cast < const T* >(this));
  86. utils::Trace < utils::DoubleTime >::trace().flush();
  87. }
  88. }
  89. #else
  90. (void) t;
  91. #endif
  92. }
  93. virtual bool check(typename U::type t) const
  94. { return Externals < T, U, V >::check(t); }
  95. virtual void compute(typename U::type t, bool update) = 0;
  96. virtual const Any& 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. return Internals < T, U, V >::get(index);
  102. }
  103. template < typename W >
  104. W get(typename U::type t, 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. return static_cast < const T* >(this)->*(value.get < T, W >());
  111. }
  112. virtual std::string get(const ValueTypeID& value_type, typename U::type t,
  113. 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. switch (value_type) {
  120. case DOUBLE:
  121. {
  122. std::ostringstream ss;
  123. ss << std::setprecision(10)
  124. << static_cast < const T* >(this)->*(
  125. value.get < T, double >());
  126. return ss.str();
  127. }
  128. case INT:
  129. return std::to_string(
  130. static_cast < const T* >(this)->*(value.get < T, int >()));
  131. case BOOL:
  132. return std::to_string(
  133. static_cast < const T* >(this)->*(value.get < T, bool >()));
  134. default: return "NA";
  135. }
  136. }
  137. virtual void init(typename U::type t, const V& parameters) = 0;
  138. virtual bool is_atomic() const
  139. { return true; }
  140. bool is_computed(typename U::type t, unsigned int /* index */) const
  141. { return type::last_time == t; }
  142. bool is_stable(typename U::type t) const
  143. { return type::last_time == t; }
  144. virtual bool is_updated() const
  145. { return Externals < T, U, V >::updated; }
  146. virtual void restore(const context::State < U >& state)
  147. {
  148. Externals < T, U, V >::restore(this, state);
  149. Internals < T, U, V >::restore(this, state);
  150. States < T, U, V >::restore(this, state);
  151. type::last_time = state.last_time();
  152. }
  153. virtual void save(context::State < U >& state) const
  154. {
  155. Externals < T, U, V >::save(this, state);
  156. Internals < T, U, V >::save(this, state);
  157. States < T, U, V >::save(this, state);
  158. state.last_time(type::last_time);
  159. }
  160. virtual void stable()
  161. { Externals < T, U, V >::updated = false; }
  162. };
  163. } }
  164. #endif