AbstractModel.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <boost/core/demangle.hpp>
  27. #include <typeinfo>
  28. #if WIN32
  29. #include <iso646.h>
  30. #endif
  31. namespace artis { namespace kernel {
  32. template < typename Time, typename Parameters >
  33. class AbstractModel : public Node < Time >
  34. {
  35. typedef AbstractModel < Time, Parameters > type;
  36. public:
  37. AbstractModel(const AbstractModel < Time, Parameters >* parent = 0) :
  38. parent(parent), last_time(-1)
  39. { }
  40. virtual ~AbstractModel()
  41. { }
  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
  46. { return true; }
  47. virtual void compute(typename Time::type t, bool update) = 0;
  48. virtual void init(typename Time::type t, const Parameters& parameters) = 0;
  49. virtual bool is_atomic() const = 0;
  50. virtual bool is_computed(typename Time::type t) const
  51. { return last_time == t; }
  52. virtual bool is_stable(typename Time::type t) const = 0;
  53. virtual bool is_computed(typename Time::type t,
  54. unsigned int index) const = 0;
  55. virtual bool is_updated() const = 0;
  56. virtual void operator()(typename Time::type t)
  57. {
  58. before(t);
  59. if (check(t) and (last_time != t or
  60. (last_time == t and is_updated()))) {
  61. compute(t, last_time == t);
  62. last_time = t;
  63. stable();
  64. }
  65. after(t);
  66. }
  67. virtual std::string path(
  68. const AbstractModel < Time, Parameters >* child) const
  69. {
  70. int index = -1;
  71. std::string p = type::parent ? type::parent->path_index(child, index) :
  72. "";
  73. if (index >= 0) {
  74. return p +
  75. "/[" + std::to_string(index) + "]" +
  76. boost::core::demangle(typeid(*child).name());
  77. } else {
  78. return (p.empty() ? "" : p + "/") +
  79. boost::core::demangle(typeid(*child).name());
  80. }
  81. }
  82. virtual std::string path_index(
  83. const AbstractModel < Time, Parameters >* child, int& index) const
  84. {
  85. (void) child;
  86. int i = -1;
  87. std::string p = type::parent ? type::parent->path_index(this, index) :
  88. "";
  89. index = -1;
  90. if (i >= 0) {
  91. return p +
  92. "/[" + std::to_string(i) + "]" +
  93. boost::core::demangle(typeid(*this).name());
  94. } else {
  95. return (p.empty() ? "" : p + "/") +
  96. boost::core::demangle(typeid(*this).name());
  97. }
  98. }
  99. virtual void restore(const context::State < Time >& state) = 0;
  100. virtual void save(context::State < Time >& state) const = 0;
  101. #ifdef WITH_TRACE
  102. virtual void trace_model(typename Time::type t) const = 0;
  103. #endif
  104. void set_parent(const AbstractModel < Time, Parameters >* p)
  105. { parent = p; }
  106. virtual void stable() = 0;
  107. protected:
  108. const AbstractModel < Time, Parameters >* parent;
  109. typename Time::type last_time;
  110. };
  111. } }
  112. #endif