View.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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-2022 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/event/Value.hpp"
  30. #include <boost/format.hpp>
  31. #include <boost/lexical_cast.hpp>
  32. namespace artis::common::observer {
  33. template<typename Time>
  34. class View {
  35. typedef std::vector<int> Selector;
  36. public:
  37. typedef std::vector<std::pair<double, common::event::Value>> Values;
  38. typedef std::map<std::string, Values> VariableValues;
  39. typedef std::map<std::string, VariableValues> SelectorValues;
  40. template<typename W>
  41. using ValueVector = std::vector<std::pair<double, W>>;
  42. enum vars {
  43. ALL = -1
  44. };
  45. View()
  46. : _model(0) {}
  47. virtual ~View() {}
  48. void attachModel(const artis::common::Model<Time> *m) { _model = m; }
  49. View *clone() const {
  50. View *v = new View();
  51. // v->_selectors = _selectors;
  52. // for (Values::const_iterator it = _values.begin(); it!= _values.end();
  53. // ++it) {
  54. // v->_values[it->first] = Value();
  55. // Value::const_iterator itp = it->second.begin();
  56. // while (itp != it->second.end()) {
  57. // v->_values[it->first].push_back(*itp);
  58. // ++itp;
  59. // }
  60. // }
  61. // v->_model = 0;
  62. return v;
  63. }
  64. const Values &get(const std::string &selector_name, const std::string &variable_name) const {
  65. SelectorValues::const_iterator it = _values.find(selector_name);
  66. if (it != _values.end()) {
  67. VariableValues::const_iterator itv = it->second.find(variable_name);
  68. if (itv != it->second.end()) {
  69. return itv->second;
  70. } else {
  71. assert(false);
  72. }
  73. } else {
  74. throw std::range_error("selector name not found");
  75. }
  76. }
  77. template<typename W>
  78. ValueVector<W> get(const std::string &selector_name, const std::string &variable_name) const {
  79. SelectorValues::const_iterator it = _values.find(selector_name);
  80. if (it != _values.end()) {
  81. VariableValues::const_iterator itv = it->second.find(variable_name);
  82. if (itv != it->second.end()) {
  83. const Values &values = itv->second;
  84. ValueVector<W> result;
  85. for (const auto &value: values) {
  86. W data;
  87. value.second(data);
  88. result.push_back(std::make_pair(value.first, data));
  89. }
  90. return result;
  91. } else {
  92. assert(false);
  93. }
  94. } else {
  95. throw std::range_error("selector name not found");
  96. }
  97. }
  98. const Values &get(const std::string &selector_name) const {
  99. SelectorValues::const_iterator it = _values.find(selector_name);
  100. if (it != _values.end()) {
  101. assert(it->second.size() == 1);
  102. return it->second.begin()->second;
  103. } else {
  104. throw std::range_error("selector name not found");
  105. }
  106. }
  107. template<typename W>
  108. ValueVector<W> get(const std::string &selector_name) const {
  109. SelectorValues::const_iterator it = _values.find(selector_name);
  110. if (it != _values.end()) {
  111. assert(it->second.size() == 1);
  112. const Values &values = it->second.begin()->second;
  113. ValueVector<W> result;
  114. for (const auto &value: values) {
  115. W data;
  116. value.second(data);
  117. result.push_back(std::make_pair(value.first, data));
  118. }
  119. return result;
  120. } else {
  121. throw std::range_error("selector name not found");
  122. }
  123. }
  124. void observe(double time, const common::Model<common::DoubleTime> *model,
  125. const std::string &selector_name, unsigned int variable_index) {
  126. std::string path = (boost::format("%1%:%2%") % model->path() %
  127. model->observable_name(variable_index)).str();
  128. VariableValues &values = _values[selector_name];
  129. if (values.find(path) == values.end()) {
  130. values[path] = Values();
  131. }
  132. values[path].push_back(
  133. std::make_pair(time, model->observe(time, variable_index)));
  134. }
  135. void observe(const Selector &chain, unsigned int i,
  136. double time, const common::Model<common::DoubleTime> *model,
  137. const std::string &selector_name, unsigned int variable_index) {
  138. assert(model != nullptr);
  139. while (i < chain.size() - 1 and chain[i + 1] != ALL and model) {
  140. assert(chain[i] >= 0);
  141. model = model->get_sub_model((unsigned int) chain[i]);
  142. ++i;
  143. }
  144. if (i < chain.size() - 1 and chain[i + 1] == ALL) {
  145. assert(model != nullptr);
  146. for (size_t model_index = 0;
  147. model_index < model->get_sub_model_number(chain[i]);
  148. ++model_index) {
  149. assert(chain[i] >= 0);
  150. observe(chain, i + 2, time,
  151. model->get_sub_model((unsigned int) chain[i], model_index),
  152. selector_name, variable_index);
  153. }
  154. } else {
  155. if (model) {
  156. observe(time, model, selector_name, variable_index);
  157. }
  158. }
  159. }
  160. virtual void observe(double time, bool trim) {
  161. for (typename Selectors::const_iterator it = _selectors.begin();
  162. it != _selectors.end(); ++it) {
  163. const common::Model<common::DoubleTime> *model = _model;
  164. if (it->second.size() > 1) {
  165. size_t i = 0;
  166. observe(it->second, i, time, model, it->first, it->second.back());
  167. } else {
  168. if (model) {
  169. observe(time, model, it->first, it->second.back());
  170. }
  171. }
  172. }
  173. if (trim) {
  174. bool ok = true;
  175. auto it = _values.begin();
  176. while (it != _values.end() and ok) {
  177. const auto &v = it->second.begin()->second;
  178. if (v.back().second.is_null()) {
  179. ++it;
  180. } else {
  181. if (v.size() > 1) {
  182. if (v.back().second == v[v.size() - 2].second) {
  183. ++it;
  184. } else {
  185. ok = false;
  186. }
  187. } else {
  188. ok = false;
  189. }
  190. }
  191. }
  192. if (ok) {
  193. for (auto &v: _values) {
  194. v.second.begin()->second.pop_back();
  195. }
  196. }
  197. }
  198. }
  199. void selector(const std::string &name, const Selector &chain) {
  200. _selectors[name] = chain;
  201. }
  202. const SelectorValues &values() const { return _values; }
  203. private:
  204. typedef std::map<std::string, Selector> Selectors;
  205. Selectors _selectors;
  206. SelectorValues _values;
  207. const artis::common::Model<Time> *_model;
  208. };
  209. }
  210. #endif