View.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @file artis/observer/View.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_OBSERVER_VIEW_HPP
  22. #define ARTIS_OBSERVER_VIEW_HPP
  23. #include <artis/kernel/AbstractModel.hpp>
  24. #include <artis/utils/DoubleTime.hpp>
  25. #include <boost/lexical_cast.hpp>
  26. namespace artis {
  27. namespace observer {
  28. template<typename U, typename V>
  29. class View {
  30. typedef std::vector<unsigned int> Selector;
  31. public:
  32. typedef std::vector<std::pair<double, std::string> > Value;
  33. typedef std::map<std::string, Value> Values;
  34. View()
  35. :_model(0) { }
  36. virtual ~View() { }
  37. void attachModel(const artis::kernel::AbstractModel<U, V>* m) { _model = m; }
  38. double begin() const
  39. {
  40. double t = utils::DoubleTime::infinity;
  41. for (Values::const_iterator it = _values.begin(); it != _values.end();
  42. ++it) {
  43. if (t > it->second.begin()->first) {
  44. t = it->second.begin()->first;
  45. }
  46. }
  47. return t;
  48. }
  49. View* clone() const
  50. {
  51. View* v = new View();
  52. v->_selectors = _selectors;
  53. for (Values::const_iterator it = _values.begin(); it != _values.end();
  54. ++it) {
  55. v->_values[it->first] = Value();
  56. Value::const_iterator itp = it->second.begin();
  57. while (itp != it->second.end()) {
  58. v->_values[it->first].push_back(*itp);
  59. ++itp;
  60. }
  61. }
  62. v->_model = 0;
  63. return v;
  64. }
  65. double end() const
  66. {
  67. double t = 0;
  68. for (Values::const_iterator it = _values.begin(); it != _values.end();
  69. ++it) {
  70. if (t < it->second.back().first) {
  71. t = it->second.back().first;
  72. }
  73. }
  74. return t;
  75. }
  76. double get(double t, const std::string& name) const
  77. {
  78. Values::const_iterator it = _values.find(name);
  79. if (it != _values.end()) {
  80. Value::const_iterator itp = it->second.begin();
  81. while (itp != it->second.end() and itp->first < t) {
  82. ++itp;
  83. }
  84. if (itp != it->second.end()) {
  85. // TODO: to improve
  86. return boost::lexical_cast<double>(itp->second);
  87. } else {
  88. return 0;
  89. }
  90. }
  91. return 0;
  92. }
  93. const Value& get(const std::string& name) const
  94. {
  95. Values::const_iterator it = _values.find(name);
  96. if (it != _values.end()) {
  97. return it->second;
  98. } else {
  99. assert(false);
  100. }
  101. }
  102. virtual void observe(double time)
  103. {
  104. for (typename Selectors::const_iterator it = _selectors.begin();
  105. it != _selectors.end(); ++it) {
  106. const kernel::Node<utils::DoubleTime>* model = _model;
  107. if (it->second.second.size() > 1) {
  108. size_t i = 0;
  109. while (i < it->second.second.size() - 1 and model) {
  110. model = model->get_submodel(it->second.second[i]);
  111. ++i;
  112. }
  113. }
  114. if (model) {
  115. _values[it->first].push_back(
  116. std::make_pair(time,
  117. model->get(it->second.first, time,
  118. it->second.second.back())));
  119. }
  120. }
  121. }
  122. void selector(const std::string& name, kernel::ValueTypeID value_type,
  123. const Selector& chain)
  124. {
  125. _selectors[name] = std::make_pair(value_type, chain);
  126. _values[name] = Value();
  127. }
  128. const Values& values() const { return _values; }
  129. private:
  130. typedef std::map<std::string, std::pair<kernel::ValueTypeID,
  131. Selector> > Selectors;
  132. Selectors _selectors;
  133. Values _values;
  134. const artis::kernel::AbstractModel<U, V>* _model;
  135. };
  136. }
  137. }
  138. #endif