View.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/lexical_cast.hpp>
  31. namespace paradevs { namespace observer {
  32. template < typename Time >
  33. class View
  34. {
  35. typedef std::vector < unsigned int > Selector;
  36. public:
  37. typedef std::vector < std::pair < double, common::Value > > Value;
  38. typedef std::map < std::string, Value > Values;
  39. View() : _model(0)
  40. { }
  41. virtual ~View()
  42. { }
  43. void attachModel(const paradevs::common::Model < Time >* m)
  44. { _model = m; }
  45. double begin() const
  46. {
  47. double t = common::DoubleTime::infinity;
  48. for (Values::const_iterator it = _values.begin(); it!= _values.end();
  49. ++it) {
  50. if (t > it->second.begin()->first) {
  51. t = it->second.begin()->first;
  52. }
  53. }
  54. return t;
  55. }
  56. View* clone() const
  57. {
  58. View* v = new View();
  59. v->_selectors = _selectors;
  60. for (Values::const_iterator it = _values.begin(); it!= _values.end();
  61. ++it) {
  62. v->_values[it->first] = Value();
  63. Value::const_iterator itp = it->second.begin();
  64. while (itp != it->second.end()) {
  65. v->_values[it->first].push_back(*itp);
  66. ++itp;
  67. }
  68. }
  69. v->_model = 0;
  70. return v;
  71. }
  72. double end() const
  73. {
  74. double t = 0;
  75. for (Values::const_iterator it = _values.begin(); it!= _values.end();
  76. ++it) {
  77. if (t < it->second.back().first) {
  78. t = it->second.back().first;
  79. }
  80. }
  81. return t;
  82. }
  83. double get(double t, const std::string& name) const
  84. {
  85. Values::const_iterator it = _values.find(name);
  86. if (it != _values.end()) {
  87. Value::const_iterator itp = it->second.begin();
  88. while (itp != it->second.end() and itp->first < t) {
  89. ++itp;
  90. }
  91. if (itp != it->second.end()) {
  92. // TODO: to improve
  93. return boost::lexical_cast < double >(itp->second);
  94. } else {
  95. return 0;
  96. }
  97. }
  98. return 0;
  99. }
  100. const Value& get(const std::string& name) const
  101. {
  102. Values::const_iterator it = _values.find(name);
  103. if (it != _values.end()) {
  104. return it->second;
  105. } else {
  106. assert(false);
  107. }
  108. }
  109. virtual void observe(double time)
  110. {
  111. for (typename Selectors::const_iterator it = _selectors.begin();
  112. it != _selectors.end(); ++it) {
  113. const common::Model < common::DoubleTime >* model = _model;
  114. if (it->second.size() > 1) {
  115. size_t i = 0;
  116. while (i < it->second.size() - 1 and model) {
  117. model = model->get_submodel(it->second[i]);
  118. ++i;
  119. }
  120. }
  121. if (model) {
  122. _values[it->first].push_back(
  123. std::make_pair(time,
  124. model->observe(time,
  125. it->second.back())));
  126. }
  127. }
  128. }
  129. void selector(const std::string& name, const Selector& chain)
  130. {
  131. _selectors[name] = chain;
  132. _values[name] = Value();
  133. }
  134. const Values& values() const
  135. { return _values;}
  136. private:
  137. typedef std::map < std::string, Selector > Selectors;
  138. Selectors _selectors;
  139. Values _values;
  140. const paradevs::common::Model < Time >* _model;
  141. };
  142. } }
  143. #endif