View.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file common/observer/View.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef ARTIS_COMMON_OBSERVER_VIEW_HPP
  26. #define ARTIS_COMMON_OBSERVER_VIEW_HPP
  27. #include <artis-star/common/Model.hpp>
  28. #include <artis-star/common/time/DoubleTime.hpp>
  29. #include <artis-star/common/Value.hpp>
  30. #include <boost/format.hpp>
  31. #include <boost/lexical_cast.hpp>
  32. namespace artis {
  33. namespace observer {
  34. template<typename Time>
  35. class View {
  36. typedef std::vector<int> Selector;
  37. public:
  38. typedef std::vector<std::pair<double, common::Value> > Values;
  39. typedef std::map<std::string, Values> VariableValues;
  40. typedef std::map<std::string, VariableValues> SelectorValues;
  41. enum vars {
  42. ALL = -1
  43. };
  44. View()
  45. : _model(0) {}
  46. virtual ~View() {}
  47. void attachModel(const artis::common::Model<Time> *m) { _model = m; }
  48. View *clone() const {
  49. View *v = new View();
  50. // v->_selectors = _selectors;
  51. // for (Values::const_iterator it = _values.begin(); it!= _values.end();
  52. // ++it) {
  53. // v->_values[it->first] = Value();
  54. // Value::const_iterator itp = it->second.begin();
  55. // while (itp != it->second.end()) {
  56. // v->_values[it->first].push_back(*itp);
  57. // ++itp;
  58. // }
  59. // }
  60. // v->_model = 0;
  61. return v;
  62. }
  63. // double
  64. // get(double t, const std::string& selector_name, const std::string& variable_name) const
  65. // {
  66. // SelectorValues::const_iterator it = _values.find(selector_name);
  67. //
  68. // if (it != _values.end()) {
  69. // VariableValues::const_iterator itp = it->second.find(variable_name);
  70. //
  71. // if (itp != it->second.end()) {
  72. // Values::const_iterator itv = itp->second.begin();
  73. //
  74. // while (itv != itp->second.end() and itv->first < t) {
  75. // ++itv;
  76. // }
  77. // if (itv != itp->second.end()) {
  78. // // TODO: to improve
  79. // return boost::lexical_cast<double>(itv->second);
  80. // } else {
  81. // return 0;
  82. // }
  83. // } else {
  84. // return 0;
  85. // }
  86. // }
  87. // return 0;
  88. // }
  89. const Values &
  90. get(const std::string &selector_name, const std::string &variable_name) const {
  91. SelectorValues::const_iterator it = _values.find(selector_name);
  92. if (it != _values.end()) {
  93. VariableValues::const_iterator itv = it->second.find(variable_name);
  94. if (itv != it->second.end()) {
  95. return itv->second;
  96. } else {
  97. assert(false);
  98. }
  99. } else {
  100. assert(false);
  101. }
  102. }
  103. const Values &get(const std::string &selector_name) const {
  104. SelectorValues::const_iterator it = _values.find(selector_name);
  105. if (it != _values.end()) {
  106. assert(it->second.size() == 1);
  107. return it->second.begin()->second;
  108. } else {
  109. assert(false);
  110. }
  111. }
  112. void observe(double time, const common::Model<common::DoubleTime> *model,
  113. const std::string &selector_name, unsigned int variable_index) {
  114. std::string path = (boost::format("%1%:%2%") % model->path() %
  115. model->observable_name(variable_index)).str();
  116. VariableValues &values = _values[selector_name];
  117. if (values.find(path) == values.end()) {
  118. values[path] = Values();
  119. }
  120. values[path].push_back(
  121. std::make_pair(time, model->observe(time, variable_index)));
  122. }
  123. void observe(const Selector &chain, unsigned int i,
  124. double time, const common::Model<common::DoubleTime> *model,
  125. const std::string &selector_name, unsigned int variable_index) {
  126. assert(model != nullptr);
  127. while (i < chain.size() - 1 and chain[i + 1] != ALL and model) {
  128. assert(chain[i] >= 0);
  129. model = model->get_submodel((unsigned int) chain[i]);
  130. ++i;
  131. }
  132. if (i < chain.size() - 1 and chain[i + 1] == ALL) {
  133. assert(model != nullptr);
  134. for (size_t model_index = 0;
  135. model_index < model->get_submodel_number(chain[i]);
  136. ++model_index) {
  137. assert(chain[i] >= 0);
  138. observe(chain, i + 2, time,
  139. model->get_submodel((unsigned int) chain[i],
  140. model_index),
  141. selector_name, variable_index);
  142. }
  143. } else {
  144. if (model) {
  145. observe(time, model, selector_name, variable_index);
  146. }
  147. }
  148. }
  149. virtual void observe(double time) {
  150. for (typename Selectors::const_iterator it = _selectors.begin();
  151. it != _selectors.end(); ++it) {
  152. const common::Model<common::DoubleTime> *model = _model;
  153. if (it->second.size() > 1) {
  154. size_t i = 0;
  155. observe(it->second, i, time, model, it->first,
  156. it->second.back());
  157. } else {
  158. if (model) {
  159. observe(time, model, it->first, it->second.back());
  160. }
  161. }
  162. }
  163. }
  164. void selector(const std::string &name, const Selector &chain) {
  165. _selectors[name] = chain;
  166. }
  167. const SelectorValues &values() const { return _values; }
  168. private:
  169. typedef std::map<std::string, Selector> Selectors;
  170. Selectors _selectors;
  171. SelectorValues _values;
  172. const artis::common::Model<Time> *_model;
  173. };
  174. }
  175. }
  176. #endif