AbstractModel.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_MODEL_HPP
  22. #define __ARTIS_KERNEL_ABSTRACT_MODEL_HPP
  23. #include <artis/kernel/Node.hpp>
  24. #include <artis/context/State.hpp>
  25. #include <artis/utils/DoubleTime.hpp>
  26. #include <artis/utils/Trace.hpp>
  27. #include <boost/core/demangle.hpp>
  28. #include <typeinfo>
  29. #if WIN32
  30. #include <iso646.h>
  31. #endif
  32. namespace artis { namespace kernel {
  33. template < typename Time, typename Parameters >
  34. class AbstractModel : public Node < Time >
  35. {
  36. typedef AbstractModel < Time, Parameters > type;
  37. public:
  38. AbstractModel(const AbstractModel < Time, Parameters >* parent = 0) :
  39. parent(parent), last_time(-1)
  40. { }
  41. virtual ~AbstractModel()
  42. { }
  43. virtual void after(typename Time::type t) = 0;
  44. virtual const Node < Time >* atomic(unsigned int index) const = 0;
  45. virtual void before(typename Time::type t) = 0;
  46. virtual bool check(typename Time::type /* t */) const
  47. { return true; }
  48. virtual void compute(typename Time::type t, bool update) = 0;
  49. virtual void init(typename Time::type t, const Parameters& parameters) = 0;
  50. virtual bool is_atomic() const = 0;
  51. virtual bool is_computed(typename Time::type t) const
  52. { return last_time == t; }
  53. virtual bool is_stable(typename Time::type t) const = 0;
  54. virtual bool is_computed(typename Time::type t,
  55. unsigned int index) const = 0;
  56. virtual bool is_updated() const = 0;
  57. virtual void operator()(typename Time::type t)
  58. {
  59. before(t);
  60. if (check(t) and (last_time != t or
  61. (last_time == t and is_updated()))) {
  62. #ifdef WITH_TRACE
  63. utils::Trace < utils::DoubleTime >::trace()
  64. << utils::TraceElement < utils::DoubleTime >(
  65. true,
  66. AbstractModel < Time, Parameters >::path(this),
  67. t, utils::COMPUTE);
  68. utils::Trace < utils::DoubleTime >::trace().flush();
  69. #endif
  70. compute(t, last_time == t);
  71. last_time = t;
  72. stable();
  73. }
  74. after(t);
  75. }
  76. virtual std::string path(
  77. const AbstractModel < Time, Parameters >* child) const
  78. {
  79. int index = -1;
  80. std::string p = type::parent ? type::parent->path_index(child, index) :
  81. "";
  82. if (index >= 0) {
  83. return p +
  84. "/[" + std::to_string(index) + "]" +
  85. boost::core::demangle(typeid(*child).name()); // .erase(0,6);
  86. } else {
  87. return (p.empty() ? "" : p + "/") +
  88. boost::core::demangle(typeid(*child).name()); // .erase(0,6);
  89. }
  90. }
  91. virtual std::string path_index(
  92. const AbstractModel < Time, Parameters >* child, int& index) const
  93. {
  94. (void) child;
  95. int i = -1;
  96. std::string p = type::parent ? type::parent->path_index(this, index) :
  97. "";
  98. index = -1;
  99. if (i >= 0) {
  100. return p +
  101. "/[" + std::to_string(i) + "]" +
  102. boost::core::demangle(typeid(*this).name());
  103. } else {
  104. return (p.empty() ? "" : p + "/") +
  105. boost::core::demangle(typeid(*this).name());
  106. }
  107. }
  108. virtual void restore(const context::State < Time >& state) = 0;
  109. virtual void save(context::State < Time >& state) const = 0;
  110. virtual void trace_element(typename Time::type t, utils::TraceType type,
  111. std::string comment = "") const = 0;
  112. virtual void trace_internals(typename Time::type t,
  113. utils::TraceType type) const = 0;
  114. virtual void trace_externals(typename Time::type t,
  115. utils::TraceType type) const = 0;
  116. virtual void trace_model(typename Time::type t,
  117. utils::TraceType type) const = 0;
  118. void set_parent(const AbstractModel < Time, Parameters >* p)
  119. { parent = p; }
  120. virtual void stable() = 0;
  121. protected:
  122. const AbstractModel < Time, Parameters >* parent;
  123. typename Time::type last_time;
  124. };
  125. template < typename Time, typename Parameters >
  126. using AbstractModels = std::vector < AbstractModel < Time, Parameters >* >;
  127. } }
  128. #endif