View.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file paradevs/common/observer/View.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2016 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 PARADEVS_COMMON_OBSERVER_VIEW_HPP
  26. #define PARADEVS_COMMON_OBSERVER_VIEW_HPP
  27. #include <paradevs/common/Model.hpp>
  28. #include <paradevs/common/time/DoubleTime.hpp>
  29. #include <paradevs/common/Value.hpp>
  30. #include <boost/format.hpp>
  31. #include <boost/lexical_cast.hpp>
  32. namespace paradevs { 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 paradevs::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, const std::string& variable_name) const
  110. {
  111. SelectorValues::const_iterator it = _values.find(selector_name);
  112. if (it != _values.end()) {
  113. VariableValues::const_iterator itv = it->second.find(variable_name);
  114. if (itv != it->second.end()) {
  115. return itv->second;
  116. } else {
  117. assert(false);
  118. }
  119. } else {
  120. assert(false);
  121. }
  122. }
  123. void observe(double time, const common::Model < common::DoubleTime >* model,
  124. const std::string& selector_name, unsigned int variable_index)
  125. {
  126. std::string path = (boost::format("%1%:%2%") % model->path() % variable_index).str() ;
  127. VariableValues& values = _values[selector_name];
  128. if (values.find(path) == values.end()) {
  129. values[path] = Values();
  130. }
  131. values[path].push_back(
  132. std::make_pair(time, model->observe(time, variable_index)));
  133. }
  134. void observe(const Selector& chain, unsigned int i,
  135. double time, const common::Model < common::DoubleTime >* model,
  136. const std::string& selector_name, unsigned int variable_index)
  137. {
  138. while (i < chain.size() - 1 and chain[i + 1] != ALL and model) {
  139. assert(chain[i] >= 0);
  140. model = model->get_submodel((unsigned int)chain[i]);
  141. ++i;
  142. }
  143. if (chain[i + 1] == ALL) {
  144. for (size_t model_index = 0; model_index < model->get_submodel_number(chain[i]);
  145. ++model_index) {
  146. assert(chain[i] >= 0);
  147. observe(chain, i + 2, time, model->get_submodel((unsigned int)chain[i], model_index),
  148. selector_name, variable_index);
  149. }
  150. } else {
  151. if (model) {
  152. observe(time, model, selector_name, variable_index);
  153. }
  154. }
  155. }
  156. virtual void observe(double time)
  157. {
  158. for (typename Selectors::const_iterator it = _selectors.begin();
  159. it != _selectors.end(); ++it) {
  160. const common::Model < common::DoubleTime >* model = _model;
  161. if (it->second.size() > 1) {
  162. size_t i = 0;
  163. observe(it->second, i, time, model, it->first, it->second.back());
  164. } else {
  165. if (model) {
  166. observe(time, model, it->first, it->second.back());
  167. }
  168. }
  169. }
  170. }
  171. void selector(const std::string& name, const Selector& chain)
  172. {
  173. _selectors[name] = chain;
  174. _values[name] = VariableValues();
  175. }
  176. const SelectorValues& values() const
  177. { return _values;}
  178. private:
  179. typedef std::map < std::string, Selector > Selectors;
  180. Selectors _selectors;
  181. SelectorValues _values;
  182. const paradevs::common::Model < Time >* _model;
  183. };
  184. } }
  185. #endif