View.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @file artis-star/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. double begin() const
  49. {
  50. double t = common::DoubleTime::infinity;
  51. // for (SelectorValues::const_iterator it = _values.begin(); it!= _values.end();
  52. // ++it) {
  53. // if (t > it->second.begin()->first) {
  54. // t = it->second.begin()->first;
  55. // }
  56. // }
  57. return t;
  58. }
  59. View* clone() const
  60. {
  61. View* v = new View();
  62. // v->_selectors = _selectors;
  63. // for (Values::const_iterator it = _values.begin(); it!= _values.end();
  64. // ++it) {
  65. // v->_values[it->first] = Value();
  66. // Value::const_iterator itp = it->second.begin();
  67. // while (itp != it->second.end()) {
  68. // v->_values[it->first].push_back(*itp);
  69. // ++itp;
  70. // }
  71. // }
  72. // v->_model = 0;
  73. return v;
  74. }
  75. double end() const
  76. {
  77. double t = 0;
  78. // for (SelectorValues::const_iterator it = _values.begin(); it!= _values.end();
  79. // ++it) {
  80. // if (t < it->second.back().first) {
  81. // t = it->second.back().first;
  82. // }
  83. // }
  84. return t;
  85. }
  86. double get(double t, const std::string& selector_name, const std::string& variable_name) const
  87. {
  88. SelectorValues::const_iterator it = _values.find(selector_name);
  89. if (it != _values.end()) {
  90. VariableValues::const_iterator itp = it->second.find(variable_name);
  91. if (itp != it->second.end()) {
  92. Values::const_iterator itv = itp->second.begin();
  93. while (itv != itp->second.end() and itv->first < t) {
  94. ++itv;
  95. }
  96. if (itv != itp->second.end()) {
  97. // TODO: to improve
  98. return boost::lexical_cast<double>(itv->second);
  99. } else {
  100. return 0;
  101. }
  102. } else {
  103. return 0;
  104. }
  105. }
  106. return 0;
  107. }
  108. const Values& get(const std::string& selector_name, const std::string& variable_name) const
  109. {
  110. SelectorValues::const_iterator it = _values.find(selector_name);
  111. if (it != _values.end()) {
  112. VariableValues::const_iterator itv = it->second.find(variable_name);
  113. if (itv != it->second.end()) {
  114. return itv->second;
  115. } else {
  116. assert(false);
  117. }
  118. } else {
  119. assert(false);
  120. }
  121. }
  122. const Values& get(const std::string& selector_name) const
  123. {
  124. SelectorValues::const_iterator it = _values.find(selector_name);
  125. if (it != _values.end()) {
  126. assert(it->second.size() == 1);
  127. return it->second.begin()->second;
  128. } else {
  129. assert(false);
  130. }
  131. }
  132. void observe(double time, const common::Model<common::DoubleTime>* model,
  133. const std::string& selector_name, unsigned int variable_index)
  134. {
  135. std::string path = (boost::format("%1%:%2%") % model->path() %
  136. model->observable_name(variable_index)).str();
  137. VariableValues& values = _values[selector_name];
  138. if (values.find(path) == values.end()) {
  139. values[path] = Values();
  140. }
  141. values[path].push_back(
  142. std::make_pair(time, model->observe(time, variable_index)));
  143. }
  144. void observe(const Selector& chain, unsigned int i,
  145. double time, const common::Model<common::DoubleTime>* model,
  146. const std::string& selector_name, unsigned int variable_index)
  147. {
  148. assert(model != nullptr)
  149. while (i < chain.size() - 1 and chain[i + 1] != ALL and model) {
  150. assert(chain[i] >= 0);
  151. model = model->get_submodel((unsigned int) chain[i]);
  152. ++i;
  153. }
  154. if (i < chain.size() - 1 and chain[i + 1] == ALL) {
  155. assert(model != nullptr)
  156. for (size_t model_index = 0;
  157. model_index < model->get_submodel_number(chain[i]);
  158. ++model_index) {
  159. assert(chain[i] >= 0);
  160. observe(chain, i + 2, time,
  161. model->get_submodel((unsigned int) chain[i],
  162. model_index),
  163. selector_name, variable_index);
  164. }
  165. } else {
  166. if (model) {
  167. observe(time, model, selector_name, variable_index);
  168. }
  169. }
  170. }
  171. virtual void observe(double time)
  172. {
  173. for (typename Selectors::const_iterator it = _selectors.begin();
  174. it != _selectors.end(); ++it) {
  175. const common::Model<common::DoubleTime>* model = _model;
  176. if (it->second.size() > 1) {
  177. size_t i = 0;
  178. observe(it->second, i, time, model, it->first,
  179. it->second.back());
  180. } else {
  181. if (model) {
  182. observe(time, model, it->first, it->second.back());
  183. }
  184. }
  185. }
  186. }
  187. void selector(const std::string& name, const Selector& chain)
  188. {
  189. _selectors[name] = chain;
  190. }
  191. const SelectorValues& values() const { return _values; }
  192. private:
  193. typedef std::map<std::string, Selector> Selectors;
  194. Selectors _selectors;
  195. SelectorValues _values;
  196. const artis::common::Model<Time>* _model;
  197. };
  198. }
  199. }
  200. #endif