AbstractModel.hpp 3.9 KB

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