#include #include #include ExpModule::ExpModule(std::map &vars_ref, vector &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("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(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 queried_data, std::vector 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 queried_data, std::vector 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++) { // s[n] 表示tag[n]在动作开始时刻的起始值 // p[n] 表示tag[n]在上一个动作周期的数值 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 + "_10"] = mm_vars[pv_str + "_9"]; // mm_vars[pv_str + "_9"] = mm_vars[pv_str + "_8"]; // mm_vars[pv_str + "_8"] = mm_vars[pv_str + "_7"]; // mm_vars[pv_str + "_7"] = mm_vars[pv_str + "_6"]; // mm_vars[pv_str + "_6"] = mm_vars[pv_str + "_5"]; 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++) { // s[n] 表示tag[n]在动作开始时刻的起始值 // p[n] 表示tag[n]在上一个动作周期的数值 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::GetInstance()[m_tags[i]]; auto pv_str = "pv" + std::to_string(i + 1); // mm_vars[pv_str + "_10"] = mm_vars[pv_str + "_9"]; // mm_vars[pv_str + "_9"] = mm_vars[pv_str + "_8"]; // mm_vars[pv_str + "_8"] = mm_vars[pv_str + "_7"]; // mm_vars[pv_str + "_7"] = mm_vars[pv_str + "_6"]; // mm_vars[pv_str + "_6"] = mm_vars[pv_str + "_5"]; 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(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::GetInstance()[m_tags[i]]; // p[n] 表示tag[n]在上一个动作周期的数值 mm_vars["p" + std::to_string(i + 1)] = value; mm_vars["tag" + std::to_string(i + 1)] = value; mm_vars["now"] = duration_cast(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; // mm_vars[pv_str + "_6"] = value; // mm_vars[pv_str + "_7"] = value; // mm_vars[pv_str + "_8"] = value; // mm_vars[pv_str + "_9"] = value; // mm_vars[pv_str + "_10"] = 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); }