View.hpp 6.1 KB

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