eis/eqpalg/utility/ExpModule.cc

139 lines
4.7 KiB
C++
Raw Normal View History

#include <eqpalg/gb_item_memory.h>
#include <eqpalg/utility/ExpModule.h>
#include <glob/SingletonTemplate.h>
ExpModule::ExpModule(std::map<std::string, double> &vars_ref,
vector<string> &m_tags, bool &is_exp_alg_ref)
: mm_vars(vars_ref), m_tags(m_tags), is_exp_alg_(is_exp_alg_ref) {
logger_ = std::make_unique<LOG>("ExpModule");
init();
}
ExpModule::~ExpModule() {}
int ExpModule::add_exp(string exp_name, string exp_str) {
if (exp_str_.find(exp_name) == exp_str_.end()) {
exp_str_[exp_name] = exp_str;
fun_vars_.add_exp_str(exp_str, &mm_vars);
exp_ptr_[exp_name] = std::make_unique<MExp>(exp_str, &mm_vars);
try {
exp_result_[exp_name] = exp_ptr_[exp_name]->evaluate();
} catch (const std::exception &e) {
logger_->Error() << "add_exp:" << e.what() << std::endl;
}
}
}
int ExpModule::update() {
if (is_exp_alg_ != true) {
this->refresh_exp_vars_mem();
this->fun_vars_.refresh_fun_vars(false, &mm_vars);
}
for (auto item : this->exp_str_) {
exp_result_[item.first] = exp_ptr_[item.first]->evaluate();
}
}
int ExpModule::update(
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> queried_data,
std::vector<TimePoint> queried_time, int row) {
if (is_exp_alg_ != true) {
this->refresh_exp_ihd_mem(queried_data, queried_time, row);
this->fun_vars_.refresh_fun_vars(false, &mm_vars);
}
for (auto item : this->exp_str_) {
exp_result_[item.first] = exp_ptr_[item.first]->evaluate();
}
}
void ExpModule::refresh_exp_ihd_mem(
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> queried_data,
std::vector<TimePoint> queried_time, int row) {
if (queried_data.rows() == 0 || queried_data.cols() == 0) {
logger_->Error() << "refresh_exp_ihd_mem: queried_data is empty"
<< std::endl;
return;
}
for (unsigned int i = 0; i < m_tags.size(); i++) {
if (row >= queried_data.rows() || i >= (unsigned int)queried_data.cols()) {
continue;
}
mm_vars["p" + std::to_string(i + 1)] =
mm_vars["tag" + std::to_string(i + 1)];
mm_vars["tag" + std::to_string(i + 1)] = queried_data(row, i);
mm_vars["now"] = mix_cc::mix_time_t(queried_time[row]).to_milliseconds();
auto pv_str = "pv" + std::to_string(i + 1);
mm_vars[pv_str + "_5"] = mm_vars[pv_str + "_4"];
mm_vars[pv_str + "_4"] = mm_vars[pv_str + "_3"];
mm_vars[pv_str + "_3"] = mm_vars[pv_str + "_2"];
mm_vars[pv_str + "_2"] = mm_vars[pv_str + "_1"];
mm_vars[pv_str + "_1"] = mm_vars[pv_str + "_0"];
mm_vars[pv_str + "_0"] = mm_vars["tag" + std::to_string(i + 1)];
}
}
void ExpModule::refresh_exp_vars_mem() {
this->now_time_ = std::chrono::system_clock::now();
for (unsigned int i = 0; i < m_tags.size(); i++) {
mm_vars["p" + std::to_string(i + 1)] =
mm_vars["tag" + std::to_string(i + 1)];
mm_vars["tag" + std::to_string(i + 1)] =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[i]];
auto pv_str = "pv" + std::to_string(i + 1);
mm_vars[pv_str + "_5"] = mm_vars[pv_str + "_4"];
mm_vars[pv_str + "_4"] = mm_vars[pv_str + "_3"];
mm_vars[pv_str + "_3"] = mm_vars[pv_str + "_2"];
mm_vars[pv_str + "_2"] = mm_vars[pv_str + "_1"];
mm_vars[pv_str + "_1"] = mm_vars[pv_str + "_0"];
mm_vars[pv_str + "_0"] = mm_vars["tag" + std::to_string(i + 1)];
}
mm_vars["now"] =
duration_cast<milliseconds>(now_time_.time_since_epoch()).count();
}
void ExpModule::init() {
this->now_time_ = std::chrono::system_clock::now();
for (unsigned int i = 0; i < m_tags.size(); i++) {
double value =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[i]];
mm_vars["p" + std::to_string(i + 1)] = value;
mm_vars["tag" + std::to_string(i + 1)] = value;
mm_vars["now"] =
duration_cast<milliseconds>(now_time_.time_since_epoch()).count();
auto pv_str = "pv" + std::to_string(i + 1);
mm_vars[pv_str + "_0"] = value;
mm_vars[pv_str + "_1"] = value;
mm_vars[pv_str + "_2"] = value;
mm_vars[pv_str + "_3"] = value;
mm_vars[pv_str + "_4"] = value;
mm_vars[pv_str + "_5"] = value;
}
}
double ExpModule::get_value(string exp_name) {
try {
return exp_result_[exp_name];
} catch (const std::exception &e) {
logger_->Error() << "get_value:" << e.what() << std::endl;
return 0;
}
}
void ExpModule::print_value() {
logger_->Debug() << "mm_vars:";
for (auto item : mm_vars) {
logger_->Debug() << " " << item.first << ":" << item.second;
}
logger_->Debug() << std::endl;
}
string ExpModule::get_exp_str(string exp_name) {
if (exp_str_.find(exp_name) == exp_str_.end()) {
return "";
}
return exp_str_[exp_name];
}
void ExpModule::fun_reset() { fun_vars_.refresh_fun_vars(true, &mm_vars); }