|
@@ -31,6 +31,7 @@
|
|
|
#include <paradevs/common/time/DoubleTime.hpp>
|
|
|
#include <paradevs/common/Value.hpp>
|
|
|
|
|
|
+#include <boost/format.hpp>
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
namespace paradevs { namespace observer {
|
|
@@ -38,11 +39,14 @@ namespace paradevs { namespace observer {
|
|
|
template < typename Time >
|
|
|
class View
|
|
|
{
|
|
|
- typedef std::vector < unsigned int > Selector;
|
|
|
+ typedef std::vector < int > Selector;
|
|
|
|
|
|
public:
|
|
|
- typedef std::vector < std::pair < double, common::Value > > Value;
|
|
|
- typedef std::map < std::string, Value > Values;
|
|
|
+ typedef std::vector < std::pair < double, common::Value > > Values;
|
|
|
+ typedef std::map < std::string, Values > VariableValues;
|
|
|
+ typedef std::map < std::string, VariableValues > SelectorValues;
|
|
|
+
|
|
|
+ enum vars { ALL = -1 };
|
|
|
|
|
|
View() : _model(0)
|
|
|
{ }
|
|
@@ -57,12 +61,12 @@ public:
|
|
|
{
|
|
|
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;
|
|
|
- }
|
|
|
- }
|
|
|
+ // for (SelectorValues::const_iterator it = _values.begin(); it!= _values.end();
|
|
|
+ // ++it) {
|
|
|
+ // if (t > it->second.begin()->first) {
|
|
|
+ // t = it->second.begin()->first;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
return t;
|
|
|
}
|
|
|
|
|
@@ -70,19 +74,19 @@ public:
|
|
|
{
|
|
|
View* v = new View();
|
|
|
|
|
|
- v->_selectors = _selectors;
|
|
|
- for (Values::const_iterator it = _values.begin(); it!= _values.end();
|
|
|
- ++it) {
|
|
|
+ // 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();
|
|
|
+ // 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;
|
|
|
+ // while (itp != it->second.end()) {
|
|
|
+ // v->_values[it->first].push_back(*itp);
|
|
|
+ // ++itp;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // v->_model = 0;
|
|
|
return v;
|
|
|
}
|
|
|
|
|
@@ -90,28 +94,35 @@ public:
|
|
|
{
|
|
|
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;
|
|
|
- }
|
|
|
- }
|
|
|
+ // for (SelectorValues::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
|
|
|
+ double get(double t, const std::string& selector_name,
|
|
|
+ const std::string& variable_name) const
|
|
|
{
|
|
|
- Values::const_iterator it = _values.find(name);
|
|
|
+ SelectorValues::const_iterator it = _values.find(selector_name);
|
|
|
|
|
|
if (it != _values.end()) {
|
|
|
- Value::const_iterator itp = it->second.begin();
|
|
|
+ VariableValues::const_iterator itp = it->second.find(variable_name);
|
|
|
|
|
|
- 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);
|
|
|
+ Values::const_iterator itv = itp->second.begin();
|
|
|
+
|
|
|
+ while (itv != itp->second.end() and itv->first < t) {
|
|
|
+ ++itv;
|
|
|
+ }
|
|
|
+ if (itv != itp->second.end()) {
|
|
|
+ // TODO: to improve
|
|
|
+ return boost::lexical_cast < double >(itv->second);
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
} else {
|
|
|
return 0;
|
|
|
}
|
|
@@ -119,17 +130,59 @@ public:
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- const Value& get(const std::string& name) const
|
|
|
+ const Values& get(const std::string& selector_name, const std::string& variable_name) const
|
|
|
{
|
|
|
- Values::const_iterator it = _values.find(name);
|
|
|
+ SelectorValues::const_iterator it = _values.find(selector_name);
|
|
|
|
|
|
if (it != _values.end()) {
|
|
|
- return it->second;
|
|
|
+ VariableValues::const_iterator itv = it->second.find(variable_name);
|
|
|
+
|
|
|
+ if (itv != it->second.end()) {
|
|
|
+ return itv->second;
|
|
|
+ } else {
|
|
|
+ assert(false);
|
|
|
+ }
|
|
|
} else {
|
|
|
assert(false);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ void observe(double time, const common::Model < common::DoubleTime >* model,
|
|
|
+ const std::string& selector_name, unsigned int variable_index)
|
|
|
+ {
|
|
|
+ std::string path = (boost::format("%1%:%2%") % model->path() % variable_index).str() ;
|
|
|
+ VariableValues& values = _values[selector_name];
|
|
|
+
|
|
|
+ if (values.find(path) == values.end()) {
|
|
|
+ values[path] = Values();
|
|
|
+ }
|
|
|
+ values[path].push_back(
|
|
|
+ std::make_pair(time, model->observe(time, variable_index)));
|
|
|
+ }
|
|
|
+
|
|
|
+ void observe(const Selector& chain, unsigned int i,
|
|
|
+ double time, const common::Model < common::DoubleTime >* model,
|
|
|
+ const std::string& selector_name, unsigned int variable_index)
|
|
|
+ {
|
|
|
+ while (i < chain.size() - 1 and chain[i + 1] != ALL and model) {
|
|
|
+ assert(chain[i] >= 0);
|
|
|
+ model = model->get_submodel((unsigned int)chain[i]);
|
|
|
+ ++i;
|
|
|
+ }
|
|
|
+ if (chain[i + 1] == ALL) {
|
|
|
+ for (size_t model_index = 0; model_index < model->get_submodel_number(chain[i]);
|
|
|
+ ++model_index) {
|
|
|
+ assert(chain[i] >= 0);
|
|
|
+ observe(chain, i + 2, time, model->get_submodel((unsigned int)chain[i], model_index),
|
|
|
+ selector_name, variable_index);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (model) {
|
|
|
+ observe(time, model, selector_name, variable_index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
virtual void observe(double time)
|
|
|
{
|
|
|
for (typename Selectors::const_iterator it = _selectors.begin();
|
|
@@ -139,34 +192,29 @@ public:
|
|
|
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;
|
|
|
+ observe(it->second, i, time, model, it->first, it->second.back());
|
|
|
+ } else {
|
|
|
+ if (model) {
|
|
|
+ observe(time, model, it->first, it->second.back());
|
|
|
}
|
|
|
}
|
|
|
- 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();
|
|
|
+ _values[name] = VariableValues();
|
|
|
}
|
|
|
|
|
|
- const Values& values() const
|
|
|
+ const SelectorValues& values() const
|
|
|
{ return _values;}
|
|
|
|
|
|
private:
|
|
|
typedef std::map < std::string, Selector > Selectors;
|
|
|
|
|
|
Selectors _selectors;
|
|
|
- Values _values;
|
|
|
+ SelectorValues _values;
|
|
|
const paradevs::common::Model < Time >* _model;
|
|
|
};
|
|
|
|