View.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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-2018 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 { namespace observer {
  33. template < typename Time >
  34. class View
  35. {
  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 { ALL = -1 };
  42. View() : _model(0)
  43. { }
  44. virtual ~View()
  45. { }
  46. void attachModel(const artis::common::Model < Time >* m)
  47. { _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,
  87. const std::string& variable_name) const
  88. {
  89. SelectorValues::const_iterator it = _values.find(selector_name);
  90. if (it != _values.end()) {
  91. VariableValues::const_iterator itp = it->second.find(variable_name);
  92. if (itp != it->second.end()) {
  93. Values::const_iterator itv = itp->second.begin();
  94. while (itv != itp->second.end() and itv->first < t) {
  95. ++itv;
  96. }
  97. if (itv != itp->second.end()) {
  98. // TODO: to improve
  99. return boost::lexical_cast < double >(itv->second);
  100. } else {
  101. return 0;
  102. }
  103. } else {
  104. return 0;
  105. }
  106. }
  107. return 0;
  108. }
  109. const Values& get(const std::string& selector_name,
  110. const std::string& variable_name) const
  111. {
  112. SelectorValues::const_iterator it = _values.find(selector_name);
  113. if (it != _values.end()) {
  114. VariableValues::const_iterator itv = it->second.find(variable_name);
  115. if (itv != it->second.end()) {
  116. return itv->second;
  117. } else {
  118. assert(false);
  119. }
  120. } else {
  121. assert(false);
  122. }
  123. }
  124. void observe(double time, const common::Model < common::DoubleTime >* model,
  125. const std::string& selector_name, unsigned int variable_index)
  126. {
  127. std::string path = (boost::format("%1%:%2%") % model->path() %
  128. variable_index).str() ;
  129. VariableValues& values = _values[selector_name];
  130. if (values.find(path) == values.end()) {
  131. values[path] = Values();
  132. }
  133. values[path].push_back(
  134. std::make_pair(time, model->observe(time, variable_index)));
  135. }
  136. void observe(const Selector& chain, unsigned int i,
  137. double time, const common::Model < common::DoubleTime >* model,
  138. const std::string& selector_name, unsigned int variable_index)
  139. {
  140. while (i < chain.size() - 1 and chain[i + 1] != ALL and model) {
  141. assert(chain[i] >= 0);
  142. model = model->get_submodel((unsigned int)chain[i]);
  143. ++i;
  144. }
  145. if (chain[i + 1] == ALL) {
  146. for (size_t model_index = 0;
  147. model_index < model->get_submodel_number(chain[i]);
  148. ++model_index) {
  149. assert(chain[i] >= 0);
  150. observe(chain, i + 2, time,
  151. model->get_submodel((unsigned int)chain[i],
  152. model_index),
  153. selector_name, variable_index);
  154. }
  155. } else {
  156. if (model) {
  157. observe(time, model, selector_name, variable_index);
  158. }
  159. }
  160. }
  161. virtual void observe(double time)
  162. {
  163. for (typename Selectors::const_iterator it = _selectors.begin();
  164. it != _selectors.end(); ++it) {
  165. const common::Model < common::DoubleTime >* model = _model;
  166. if (it->second.size() > 1) {
  167. size_t i = 0;
  168. observe(it->second, i, time, model, it->first,
  169. it->second.back());
  170. } else {
  171. if (model) {
  172. observe(time, model, it->first, it->second.back());
  173. }
  174. }
  175. }
  176. }
  177. void selector(const std::string& name, const Selector& chain)
  178. {
  179. _selectors[name] = chain;
  180. _values[name] = VariableValues();
  181. }
  182. const SelectorValues& values() const
  183. { return _values;}
  184. private:
  185. typedef std::map < std::string, Selector > Selectors;
  186. Selectors _selectors;
  187. SelectorValues _values;
  188. const artis::common::Model < Time >* _model;
  189. };
  190. } }
  191. #endif