AbstractModel.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. trace_element(t, utils::COMPUTE);
  64. #endif
  65. compute(t, last_time == t);
  66. last_time = t;
  67. stable();
  68. }
  69. after(t);
  70. }
  71. virtual std::string path(
  72. const AbstractModel < Time, Parameters >* child) const
  73. {
  74. int index = -1;
  75. std::string p = type::parent ? type::parent->path_index(child, index) :
  76. "";
  77. if (index >= 0) {
  78. return p +
  79. "/[" + std::to_string(index) + "]" +
  80. boost::core::demangle(typeid(*child).name()).erase(0,6);
  81. } else {
  82. return (p.empty() ? "" : p + "/") +
  83. boost::core::demangle(typeid(*child).name()).erase(0,6);
  84. }
  85. }
  86. virtual std::string path_index(
  87. const AbstractModel < Time, Parameters >* child, int& index) const
  88. {
  89. (void) child;
  90. int i = -1;
  91. std::string p = type::parent ? type::parent->path_index(this, index) :
  92. "";
  93. index = -1;
  94. if (i >= 0) {
  95. return p +
  96. "/[" + std::to_string(i) + "]" +
  97. boost::core::demangle(typeid(*this).name());
  98. } else {
  99. return (p.empty() ? "" : p + "/") +
  100. boost::core::demangle(typeid(*this).name());
  101. }
  102. }
  103. virtual void restore(const context::State < Time >& state) = 0;
  104. virtual void save(context::State < Time >& state) const = 0;
  105. virtual void trace_element(typename Time::type t, utils::TraceType type) const = 0;
  106. virtual void trace_internals(typename Time::type t, utils::TraceType type) const = 0;
  107. virtual void trace_externals(typename Time::type t, utils::TraceType type) const = 0;
  108. virtual void trace_model(typename Time::type t, utils::TraceType type) const = 0;
  109. void set_parent(const AbstractModel < Time, Parameters >* p)
  110. { parent = p; }
  111. virtual void stable() = 0;
  112. protected:
  113. const AbstractModel < Time, Parameters >* parent;
  114. typename Time::type last_time;
  115. };
  116. } }
  117. #endif