View.hpp 7.1 KB

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