View.hpp 4.6 KB

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