AbstractModel.hpp 5.5 KB

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