123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /**
- * @file paradevs/common/observer/View.hpp
- * @author The PARADEVS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * PARADEVS - the multimodeling and simulation environment
- * This file is a part of the PARADEVS environment
- *
- * Copyright (C) 2013-2016 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef PARADEVS_COMMON_OBSERVER_VIEW_HPP
- #define PARADEVS_COMMON_OBSERVER_VIEW_HPP
- #include <paradevs/common/Model.hpp>
- #include <paradevs/common/time/DoubleTime.hpp>
- #include <paradevs/common/Value.hpp>
- #include <boost/lexical_cast.hpp>
- namespace paradevs { namespace observer {
- template < typename Time >
- class View
- {
- typedef std::vector < unsigned int > Selector;
- public:
- typedef std::vector < std::pair < double, common::Value > > Value;
- typedef std::map < std::string, Value > Values;
- View() : _model(0)
- { }
- virtual ~View()
- { }
- void attachModel(const paradevs::common::Model < Time >* m)
- { _model = m; }
- double begin() const
- {
- double t = common::DoubleTime::infinity;
- for (Values::const_iterator it = _values.begin(); it!= _values.end();
- ++it) {
- if (t > it->second.begin()->first) {
- t = it->second.begin()->first;
- }
- }
- return t;
- }
- View* clone() const
- {
- View* v = new View();
- v->_selectors = _selectors;
- for (Values::const_iterator it = _values.begin(); it!= _values.end();
- ++it) {
- v->_values[it->first] = Value();
- Value::const_iterator itp = it->second.begin();
- while (itp != it->second.end()) {
- v->_values[it->first].push_back(*itp);
- ++itp;
- }
- }
- v->_model = 0;
- return v;
- }
- double end() const
- {
- double t = 0;
- for (Values::const_iterator it = _values.begin(); it!= _values.end();
- ++it) {
- if (t < it->second.back().first) {
- t = it->second.back().first;
- }
- }
- return t;
- }
- double get(double t, const std::string& name) const
- {
- Values::const_iterator it = _values.find(name);
- if (it != _values.end()) {
- Value::const_iterator itp = it->second.begin();
- while (itp != it->second.end() and itp->first < t) {
- ++itp;
- }
- if (itp != it->second.end()) {
- // TODO: to improve
- return boost::lexical_cast < double >(itp->second);
- } else {
- return 0;
- }
- }
- return 0;
- }
- const Value& get(const std::string& name) const
- {
- Values::const_iterator it = _values.find(name);
- if (it != _values.end()) {
- return it->second;
- } else {
- assert(false);
- }
- }
- virtual void observe(double time)
- {
- for (typename Selectors::const_iterator it = _selectors.begin();
- it != _selectors.end(); ++it) {
- const common::Model < common::DoubleTime >* model = _model;
- if (it->second.size() > 1) {
- size_t i = 0;
- while (i < it->second.size() - 1 and model) {
- model = model->get_submodel(it->second[i]);
- ++i;
- }
- }
- if (model) {
- _values[it->first].push_back(
- std::make_pair(time,
- model->observe(time,
- it->second.back())));
- }
- }
- }
- void selector(const std::string& name, const Selector& chain)
- {
- _selectors[name] = chain;
- _values[name] = Value();
- }
- const Values& values() const
- { return _values;}
- private:
- typedef std::map < std::string, Selector > Selectors;
- Selectors _selectors;
- Values _values;
- const paradevs::common::Model < Time >* _model;
- };
- } }
- #endif
|