refactor: ExpBase 表达式管理迁移到 ExpressionEngine

This commit is contained in:
Huamonarch 2026-05-15 12:59:11 +08:00
parent 96ca4d02bb
commit 7c2fe7f7bb
2 changed files with 55 additions and 493 deletions

View File

@ -16,7 +16,6 @@ extern ProcessType glob_process_type;
ExpBase::ExpBase(const string &name, const mix_cc::json &rule_json,
const string &ruleId, size_t exp_type)
: AlgBase(name, rule_json, ruleId), exp_type_(exp_type) {
is_exp_alg_ = true;
logger_.reset(
new LOG("ExpBase-" + std::to_string(exp_type) + ":" + rule_name_,
AUTO_CATCH_PID));
@ -29,14 +28,13 @@ int ExpBase::init() {
int ret = 0;
try {
ret += AlgBase::init(); /*1.tag点2.执行周期*/
fun_vars_init();
this->con_monitor_.setThreshold(100);
// 重新载入数据源配置信息
ret += this->reload_config_data_source(); /*3.数据源*/
// 在载入数据源信息完成后载入表达式配置之前必须刷新变量把变量信息初始化到mm_vars内
if (glob_process_type == ProcessType::kMon ||
glob_process_type == ProcessType::kTask) {
ret += this->first_fill_mm_vars(); /*4.数据项*/
ret += expr_engine_->firstFill(data_source_, now_time_, query_time_range_); /*4.数据项*/
}
// 必须在刷新变量后,才可以初始化表达式
@ -110,9 +108,9 @@ AlarmInfo ExpBase::exec_mon() {
// 并判断是否报警
case DataSource::MEMORY: {
try {
refresh_exp_vars_mem();
expr_engine_->refreshFromMemory(now_time_, query_time_range_);
} catch (const std::exception &e) {
logger_->Error() << rule_id_ << ":refresh_exp_vars_mem()异常!"
logger_->Error() << rule_id_ << ":refreshFromMemory()异常!"
<< e.what() << ",location:" << BOOST_CURRENT_LOCATION
<< endl;
return out_alarm;
@ -130,7 +128,7 @@ AlarmInfo ExpBase::exec_mon() {
refresh_ihd_cache();
// 然后刷以此把缓存中的数据取出
for (auto i = 0; i < queried_data_.rows(); i++) {
refresh_exp_vars_ihd(i);
expr_engine_->refreshFromIhdRow(i, queried_data_, queried_time_, now_time_, query_time_range_);
if (!out_alarm.alarmed) {
auto tmp = mon_proc();
if (tmp.alarmed) {
@ -234,7 +232,7 @@ std::vector<AlarmInfo> ExpBase::exec_task(mix_cc::time_range_t time_range) {
refresh_ihd_cache();
}
for (auto i = 0; i < queried_data_.rows(); i++) {
refresh_exp_vars_ihd(i);
expr_engine_->refreshFromIhdRow(i, queried_data_, queried_time_, now_time_, query_time_range_);
}
// 对每个ihdb 查询周期的数据,进行处理
for (auto now_time = time_range.get_left();
@ -248,7 +246,7 @@ std::vector<AlarmInfo> ExpBase::exec_task(mix_cc::time_range_t time_range) {
refresh_ihd_cache();
}
for (auto i = 0; i < queried_data_.rows(); i++) {
refresh_exp_vars_ihd(i);
expr_engine_->refreshFromIhdRow(i, queried_data_, queried_time_, now_time_, query_time_range_);
auto tmp = mon_proc();
if (tmp.alarmed) {
out_alarms.push_back(tmp);
@ -274,23 +272,18 @@ AlarmInfo ExpBase::mon_proc() {
// 监控基本过程
double result_value;
filter_flag_ = false;
auto_fun_vars_reset();
expr_engine_->autoResetFunVars();
try {
// 获得是否满足前提条件表达式
result_value = exp_act_->evaluate();
result_value = expr_engine_->evaluate("act");
if (exp_type_ == ExpType::Bound || exp_type_ == ExpType::BoundHoldTime) {
if (exp_feedback_ != nullptr) {
try {
/*有数据筛选*/
try {
if (exp_feedback_->evaluate()) {
filter_flag_ = true;
}
} catch (...) {
logger_->Debug() << "ruleid:" << rule_id_ << ",exp_feedback_ ERROR!"
<< endl;
}
} else {
filter_flag_ = expr_engine_->evaluateBool("feedback");
} catch (...) {
/*无数据筛选*/
logger_->Debug() << "ruleid:" << rule_id_ << ",feedback evaluate ERROR!"
<< endl;
filter_flag_ = true;
}
if (is_learning_ && filter_flag_ == true) {
@ -307,7 +300,7 @@ AlarmInfo ExpBase::mon_proc() {
if (act_start_done()) {
if (rule_id_ == string(CMemVar::Const()->printRuleid)) {
logger_->Debug() << "act_start_done!" << std::endl;
print_exp_vars();
expr_engine_->printVars();
}
return AlarmInfo{};
}
@ -317,9 +310,9 @@ AlarmInfo ExpBase::mon_proc() {
logger_->Debug()
<< "act_not_hold!---fun_vars_.refresh_fun_vars---start"
<< std::endl;
print_exp_vars();
expr_engine_->printVars();
}
is_fun_vars_need_reset_ = true;
expr_engine_->markFunVarsNeedReset();
return AlarmInfo{};
}
if (act_done()) {
@ -327,12 +320,12 @@ AlarmInfo ExpBase::mon_proc() {
this->query_time_range_.set_left(
query_time_range_.get_right() -
milliseconds(int(mm_vars["time"])));
result_value = exp_result_->evaluate();
is_fun_vars_need_reset_ = true;
result_value = expr_engine_->evaluate("result");
expr_engine_->markFunVarsNeedReset();
this->rule_stat_.limit_down = limit_down_;
this->rule_stat_.limit_up = limit_up_;
rule_stat_.current_value = result_value;
print_exp_vars();
expr_engine_->printVars();
logger_->Debug() << " action end:"
<< mix_cc::mix_time_t(query_time_range_.get_right())
.to_formatted_time()
@ -380,7 +373,7 @@ AlarmInfo ExpBase::mon_proc() {
}
} else if (act_timeout()) {
is_fun_vars_need_reset_ = true;
expr_engine_->markFunVarsNeedReset();
return this->get_timeout_alarm();
return AlarmInfo{};
}
@ -396,7 +389,7 @@ AlarmInfo ExpBase::mon_proc() {
logger_->Debug() << msg << endl;
this->query_time_range_.set_left(query_time_range_.get_right() -
delay_time_);
is_fun_vars_need_reset_ = true;
expr_engine_->markFunVarsNeedReset();
return utility::build_alarm_info(MsgLevel::ERROR, rule_id_,
rule_name_, "EXP2", msg,
this->get_alarm_time());
@ -428,10 +421,10 @@ AlarmInfo ExpBase::mon_proc() {
unit_ + ",合理区间:[" +
DAA::double2strLimit(limit_down_) + "," +
DAA::double2strLimit(limit_up_) + "]" + unit_;
print_exp_vars();
expr_engine_->printVars();
this->query_time_range_.set_left(query_time_range_.get_right() -
delay_time_);
is_fun_vars_need_reset_ = true;
expr_engine_->markFunVarsNeedReset();
act_start_time_ =
this->now_time_;
act_started_ = false;
@ -448,12 +441,12 @@ AlarmInfo ExpBase::mon_proc() {
else {
if (act_triggered_) {
rule_stat_.alarm_value = act_triggered_;
print_exp_vars();
expr_engine_->printVars();
auto msg = rule_name_ + " " + error_str_;
logger_->Debug() << msg << endl;
this->query_time_range_.set_left(query_time_range_.get_right() -
delay_time_);
is_fun_vars_need_reset_ = true;
expr_engine_->markFunVarsNeedReset();
return utility::build_alarm_info(MsgLevel::ERROR, rule_id_,
rule_name_, "EXP1", msg,
this->get_alarm_time());
@ -534,7 +527,7 @@ bool ExpBase::act_start_done() {
<< endl;
return true;
} else if (!act_started_ && !act_triggered_) {
is_fun_vars_need_reset_ = true;
expr_engine_->markFunVarsNeedReset();
}
return false;
}
@ -633,7 +626,7 @@ bool ExpBase::act_done() {
}
}
/*-----------根据最新变量计算feedback结果-----------*/
feedback_triggered_ = static_cast<bool>(exp_feedback_->evaluate());
feedback_triggered_ = expr_engine_->evaluateBool("feedback");
/*-----------根据最新变量计算feedback结果-----------*/
// 如果动作处于开始状态,且反馈模式触发
@ -730,32 +723,10 @@ int ExpBase::reload_config_exp_feedback() {
.at("value")
.get<std::string>());
exp_str_ = get_macro_replaced_exp(tmp_exp);
res += init_hold_exp_str(exp_str_);
auto fun_res = fun_vars_.add_exp_str(exp_str_, &mm_vars);
res += fun_res.first ? 0 : -1;
exp_str_ = fun_res.second;
if (rule_id_ == "28c1d627-1a8a-426b-9070-eea3c906c53c") {
logger_->Debug() << exp_str_ << ",fun_res first:" << fun_res.first
<< ",second:" << fun_res.second << std::endl;
}
auto messy_code = exp_messy_code_check(exp_str_);
if (messy_code == -1) {
this->error_code_list_.push_back(
{ErrorType::EnCodeError, ErrorLocation::FBExp});
res += messy_code;
}
// 构建feedback表达式
if (exp_feedback_ == nullptr && exp_str_ != "") {
try {
exp_feedback_ =
std::make_unique<mix_cc::matheval::Expression>(exp_str_, &mm_vars);
logger_->Debug() << exp_str_ << ":" << exp_feedback_->evaluate()
<< endl;
} catch (const std::exception &e) {
logger_->Error() << exp_str_ << "计算错误:" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
if (exp_str_ != "") {
int reg_ret = expr_engine_->registerExpression("feedback", exp_str_);
if (reg_ret != 0) {
this->error_code_list_.push_back(
{ErrorType::CalError, ErrorLocation::FBExp});
return -1;
@ -769,29 +740,13 @@ int ExpBase::reload_config_exp_feedback() {
auto tmp_exp =
rule_json_.at("function").at("result").at("value").get<std::string>();
exp_str_ = get_macro_replaced_exp(tmp_exp);
res += init_hold_exp_str(exp_str_);
auto fun_res = fun_vars_.add_exp_str(exp_str_, &mm_vars);
res += fun_res.first ? 0 : -1;
exp_str_ = fun_res.second;
auto messy_code = exp_messy_code_check(exp_str_);
if (messy_code == -1) {
this->error_code_list_.push_back(
{ErrorType::EnCodeError, ErrorLocation::ResultExp});
res += messy_code;
}
// 如果结果表达式没有初始化,则初始化之后再次使用
if (exp_result_ == nullptr && exp_str_ != "") {
try {
exp_result_ =
std::make_unique<mix_cc::matheval::Expression>(exp_str_, &mm_vars);
logger_->Debug() << exp_str_ << ":" << exp_result_->evaluate() << endl;
} catch (const std::exception &e) {
logger_->Error() << exp_result_ << "计算错误:" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
if (exp_str_ != "") {
int reg_ret = expr_engine_->registerExpression("result", exp_str_);
if (reg_ret != 0) {
this->error_code_list_.push_back(
{ErrorType::CalError, ErrorLocation::ResultExp});
res += reg_ret;
}
}
@ -805,31 +760,6 @@ int ExpBase::reload_config_exp_feedback() {
return res;
}
// 刷新共享内存所对应的变量
int ExpBase::refresh_exp_vars_mem() {
refresh_now_time();
for (unsigned int i = 0; i < m_tags.size(); i++) {
// s[n] 表示tag[n]在动作开始时刻的起始值
// p[n] 表示tag[n]在上一个动作周期的数值
double current =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[i]];
mm_vars[var_cache_.p_keys[i]] = mm_vars[var_cache_.tag_keys[i]];
mm_vars[var_cache_.tag_keys[i]] = current;
for (size_t j = pv_num_ - 1; j > 0; j--) {
mm_vars[var_cache_.pv_keys[i][j]] = mm_vars[var_cache_.pv_keys[i][j - 1]];
}
mm_vars[var_cache_.pv_keys[i][0]] = current;
}
mm_vars["now"] =
duration_cast<milliseconds>(now_time_.time_since_epoch()).count();
// 设置right的时间
this->fun_vars_.refresh_fun_vars(false, &mm_vars);
this->query_time_range_.set_right(now_time_);
refresh_hold_var();
return 0;
}
// 重新载入数据源配置
int ExpBase::reload_config_data_source() {
int res = 0;
@ -856,167 +786,6 @@ int ExpBase::reload_config_data_source() {
return res;
}
// 刷新对应ihd cache内第row行的数据变量
int ExpBase::refresh_exp_vars_ihd(int row) {
// modify 20260107
// 检查矩阵大小和索引有效性
if (queried_data_.rows() == 0 || queried_data_.cols() == 0) {
logger_->Error() << "refresh_exp_vars_ihd: queried_data_ 为空!" << endl;
return -1;
}
if (row < 0 || row >= queried_data_.rows()) {
logger_->Error() << "refresh_exp_vars_ihd: row索引 " << row
<< " 超出范围 [0, " << queried_data_.rows() - 1 << "]"
<< endl;
return -1;
}
if (static_cast<int>(m_tags.size()) > queried_data_.cols()) {
logger_->Error() << "refresh_exp_vars_ihd: m_tags.size() " << m_tags.size()
<< " 超出列数 " << queried_data_.cols() << endl;
return -1;
}
// i <= row 原本查询时间对应的是[t1,t2)
// 则queried_time_内部的时间也一定是[t1,t2)
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)] = queried_data_(row, 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"] = mix_cc::mix_time_t(queried_time_[row]).to_milliseconds();
// right一定小于t2
this->query_time_range_.set_right(queried_time_[row]);
// 当前时间 == 上次执行的结束时间
this->now_time_ = queried_time_[row];
}
this->fun_vars_.refresh_fun_vars(false, &mm_vars);
refresh_hold_var();
return 0;
}
// 程序启动时刻的,首次刷新变量数据
// 防止程序因为p1 pv1类似的变量导致程序的变量差值信息错误
int ExpBase::first_fill_mm_vars() {
/*-------动作反馈标记-重置------*/
logger_->Debug() << "-first_fill_mm_vars()-----mm_vars,size:"
<< mm_vars.size() << ",mm_vars..empty():" << mm_vars.empty()
<< std::endl;
this->act_started_ = false;
this->act_triggered_ = false;
this->feedback_triggered_ = false;
/*-------动作反馈标记-重置------*/
if (data_source_ == DataSource::MEMORY) {
for (unsigned int i = 0; i < m_tags.size(); i++) {
auto pv_str = "pv" + std::to_string(i + 1);
if (this->rule_id_ == "fba8df61-8a16-4a0e-80de-6e25aaf3d037") {
logger_->Debug() << "m_tags[" << i << "]:" << m_tags[i]
<< ",m_tags.size():" << m_tags.size() << "-----0------"
<< std::endl;
}
double value =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[i]];
if (this->rule_id_ == "fba8df61-8a16-4a0e-80de-6e25aaf3d037") {
logger_->Debug() << "m_tags[" << i << "]:" << m_tags[i]
<< ",m_tags.size():" << m_tags.size() << "-----1------"
<< ",value:" << value
<< "mm_vars.size:" << mm_vars.size() << std::endl;
}
if (this->rule_id_ == "fba8df61-8a16-4a0e-80de-6e25aaf3d037") {
logger_->Debug() << "s" + std::to_string(i + 1) << ",m_tags[" << i
<< "]:" << m_tags[i]
<< ",m_tags.size():" << m_tags.size() << "-----2-----"
<< std::endl;
}
mm_vars["s" + std::to_string(i + 1)] = value;
mm_vars["p" + std::to_string(i + 1)] = value;
mm_vars["tag" + std::to_string(i + 1)] = value;
mm_vars["mv2_tag" + std::to_string(i + 1)] = 0;
mm_vars["mv2_p" + std::to_string(i + 1)] = 0;
mm_vars["up_tag" + std::to_string(i + 1)] = 0;
mm_vars["dw_tag" + std::to_string(i + 1)] = 0;
mm_vars["mx_tag" + std::to_string(i + 1)] =
mm_vars["tag" + std::to_string(i + 1)];
mm_vars["mi_tag" + std::to_string(i + 1)] =
mm_vars["tag" + 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;
if (this->exp_type_ == -1) {
logger_->Debug() << "m_tags[" << i << "]:" << m_tags[i]
<< ",m_tags.size():" << m_tags.size() << "-----3-----"
<< "mm_vars.size:" << mm_vars.size() << std::endl;
}
}
mm_vars["stime"] = 0;
mm_vars["now"] = 0;
mm_vars["etime"] = 0;
mm_vars["time"] = 0;
} else if (data_source_ == DataSource::IHDB) {
this->refresh_now_time();
query_time_range_.set_left(now_time_ - 10s);
query_time_range_.set_right(now_time_ - 5s);
this->refresh_ihd_cache();
if (queried_data_.rows() == 0) {
logger_->Debug() << "time:"
<< mix_cc::mix_time_t(this->query_time_range_.get_left())
.to_formatted_time()
<< "~"
<< mix_cc::mix_time_t(
this->query_time_range_.get_right())
.to_formatted_time()
<< ",diff_time:"
<< std::chrono::duration_cast<milliseconds>(
query_time_range_.get_right() -
query_time_range_.get_left())
.count()
<< endl;
logger_->Error() << "first_fill_mm_vars(),IHDB查询异常未查到数据"
<< endl;
this->error_code_list_.push_back(
{ErrorType::Empty, ErrorLocation::DataValue});
return -1;
}
for (unsigned int i = 0; i < m_tags.size(); i++) {
auto pv_str = "pv" + std::to_string(i + 1);
auto rows = queried_data_.rows() - 1;
auto tmp_val = queried_data_(rows, i);
mm_vars["s" + std::to_string(i + 1)] = tmp_val;
mm_vars["p" + std::to_string(i + 1)] = tmp_val;
mm_vars["tag" + std::to_string(i + 1)] = tmp_val;
mm_vars["mv2_tag" + std::to_string(i + 1)] = 0;
mm_vars["mv2_p" + std::to_string(i + 1)] = 0;
mm_vars["up_tag" + std::to_string(i + 1)] = 0;
mm_vars["dw_tag" + std::to_string(i + 1)] = 0;
mm_vars["mx_tag" + std::to_string(i + 1)] =
mm_vars["tag" + std::to_string(i + 1)];
mm_vars["mi_tag" + std::to_string(i + 1)] =
mm_vars["tag" + std::to_string(i + 1)];
mm_vars[pv_str + "_0"] = tmp_val;
mm_vars[pv_str + "_1"] = tmp_val;
mm_vars[pv_str + "_2"] = tmp_val;
mm_vars[pv_str + "_3"] = tmp_val;
mm_vars[pv_str + "_4"] = tmp_val;
mm_vars[pv_str + "_5"] = tmp_val;
mm_vars["stime"] = 0;
mm_vars["now"] = 0;
mm_vars["etime"] = 0;
mm_vars["time"] = 0;
}
}
return 0;
}
int ExpBase::reload_config_exp_act() {
// 根据key对不同版本的算法都进行取值
int res = 0;
@ -1026,17 +795,7 @@ int ExpBase::reload_config_exp_act() {
.at("value")
.get<std::string>();
exp_str_ = get_macro_replaced_exp(tmp_exp);
res += init_hold_exp_str(exp_str_);
auto fun_res = fun_vars_.add_exp_str(exp_str_, &mm_vars);
res += fun_res.first ? 0 : -1;
exp_str_ = fun_res.second;
feedback_mode_ = true;
auto messy_code = exp_messy_code_check(exp_str_);
if (messy_code == -1) {
this->error_code_list_.push_back(
{ErrorType::EnCodeError, ErrorLocation::ActExp});
res += messy_code;
}
} else if (rule_json_.at("function").contains("result")) {
if (rule_json_.at("function").contains("filter_exp")) {
@ -1045,27 +804,10 @@ int ExpBase::reload_config_exp_act() {
.at("value")
.get<std::string>();
exp_str_ = get_macro_replaced_exp(tmp_exp);
res += init_hold_exp_str(exp_str_);
auto fun_res = fun_vars_.add_exp_str(exp_str_, &mm_vars);
res += fun_res.first ? 0 : -1;
exp_str_ = fun_res.second;
feedback_mode_ = false;
auto messy_code = exp_messy_code_check(exp_str_);
if (messy_code == -1) {
this->error_code_list_.push_back(
{ErrorType::EnCodeError, ErrorLocation::ActExp});
res += messy_code;
}
if (exp_feedback_ == nullptr && exp_str_ != "") {
try {
exp_feedback_ = std::make_unique<mix_cc::matheval::Expression>(
exp_str_, &mm_vars);
logger_->Debug() << "filter_exp exp_feedback_:" << exp_str_ << "="
<< exp_feedback_->evaluate() << endl;
} catch (const std::exception &e) {
logger_->Error() << "filter_exp exp_feedback_:" << exp_str_
<< "计算出错:" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
if (exp_str_ != "") {
int reg_ret = expr_engine_->registerExpression("feedback", exp_str_);
if (reg_ret != 0) {
this->error_code_list_.push_back(
{ErrorType::CalError, ErrorLocation::FBExp});
return -1;
@ -1082,30 +824,13 @@ int ExpBase::reload_config_exp_act() {
auto tmp_exp =
rule_json_.at("function").at("result").at("value").get<std::string>();
exp_str_ = get_macro_replaced_exp(tmp_exp);
res += init_hold_exp_str(exp_str_);
auto fun_res = fun_vars_.add_exp_str(exp_str_, &mm_vars);
res += fun_res.first ? 0 : -1;
exp_str_ = fun_res.second;
feedback_mode_ = false;
auto messy_code = exp_messy_code_check(exp_str_);
if (messy_code == -1) {
this->error_code_list_.push_back(
{ErrorType::EnCodeError, ErrorLocation::ActExp});
res += messy_code;
}
}
if (exp_act_ == nullptr && exp_str_ != "") {
try {
exp_act_ =
std::make_unique<mix_cc::matheval::Expression>(exp_str_, &mm_vars);
logger_->Debug() << "exp_act:" << exp_str_ << "=" << exp_act_->evaluate()
<< endl;
} catch (const std::exception &e) {
logger_->Error() << "exp_act:" << exp_str_ << "计算出错:" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
if (exp_str_ != "") {
int reg_ret = expr_engine_->registerExpression("act", exp_str_);
if (reg_ret != 0) {
this->error_code_list_.push_back(
{ErrorType::CalError, ErrorLocation::ActExp});
return -1;
@ -1207,20 +932,10 @@ int ExpBase::reload_config_up_down_hold_time() {
return 0;
}
// 打印表达式信息
void ExpBase::print_exp_vars(const string &expstr) {
std::map<std::string, double>::iterator it;
logger_->Debug() << "exp:" << expstr << " parameter:";
for (it = mm_vars.begin(); it != mm_vars.end(); it++) {
logger_->Debug() << " " << it->first << ":" << it->second;
}
logger_->Debug() << endl;
}
void ExpBase::set_last_alarm_time(TimePoint time_point) {
this->refresh_counts_ = 0;
if (this->is_usable_) {
this->first_fill_mm_vars();
expr_engine_->firstFill(data_source_, now_time_, query_time_range_);
}
AlgBase::set_last_alarm_time(time_point);
}
@ -1228,68 +943,11 @@ void ExpBase::set_last_alarm_time(TimePoint time_point) {
void ExpBase::set_usable(bool usable) {
this->refresh_counts_ = 0;
if (this->is_usable_) {
this->first_fill_mm_vars();
expr_engine_->firstFill(data_source_, now_time_, query_time_range_);
}
AlgBase::set_usable(usable);
}
int ExpBase::refresh_hold_var() {
if (hold_times_.empty()) {
return 0;
} else {
for (map<std::string, std::unique_ptr<HoldTime>>::iterator iter =
hold_times_.begin();
iter != hold_times_.end(); iter++) {
mm_vars[iter->first] =
double(iter->second->update_value(mm_vars[iter->second->tagi]));
}
return 0;
}
}
int ExpBase::init_hold_exp_str(const std::string &exp_str) {
int res = 0;
try {
auto hold_sub_strs = HoldTime::find_substr(exp_str, "_HE");
bool flag;
double timeM;
std::string tagi;
std::string var_name;
for (auto sub_str : hold_sub_strs) {
std::tie(flag, timeM, tagi, var_name) = HoldTime::find_hold(sub_str);
if (flag) {
if (hold_times_.find(var_name) == hold_times_.end()) {
hold_times_.emplace(std::make_pair(
var_name, std::make_unique<HoldTime>(timeM, tagi, var_name)));
}
} else {
if (timeM) {
logger_->Error() << "hold结构结构不正确," << tagi << std::endl;
return -1;
}
}
}
refresh_hold_var();
} catch (const std::exception &e) {
logger_->Debug() << "ExpBase::init_hold_exp_str:" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
return -1;
}
return res;
}
int ExpBase::exp_messy_code_check(const std::string &exp_str) {
for (char e_char : exp_str) {
if (e_char > 127 || e_char < 0) {
this->logger_->Error() << "表达式存在中文字符或乱码 tmp_exp:" << exp_str
<< " :" << e_char << ":" << int(e_char) << endl;
return -1;
}
}
return 0;
}
int ExpBase::reload_ci_dist() {
if (this->exp_type_ != ExpType::Bound &&
this->exp_type_ != ExpType::CondBound &&
@ -1355,7 +1013,7 @@ void ExpBase::task_mon_pro() {
logger_->Debug() << rule_name_ << ",ihd size:" << queried_data_.size()
<< endl;
for (auto i = 0; i < queried_data_.rows(); i++) {
refresh_exp_vars_ihd(i);
expr_engine_->refreshFromIhdRow(i, queried_data_, queried_time_, now_time_, query_time_range_);
if (this->refresh_counts_ < 3) {
this->refresh_counts_++;
} else {
@ -1388,7 +1046,7 @@ TaskReturnType ExpBase::task_base_proc() {
double result_value = 0;
try {
// 获得是否满足前提条件表达式
result_value = exp_act_->evaluate();
result_value = expr_engine_->evaluate("act");
act_triggered_ = static_cast<bool>(result_value);
// 检测是否是表达式-反馈模式
if (feedback_mode_) {
@ -1397,8 +1055,8 @@ TaskReturnType ExpBase::task_base_proc() {
return task_return_data;
}
if (act_done()) {
result_value = exp_result_->evaluate();
print_exp_vars();
result_value = expr_engine_->evaluate("result");
expr_engine_->printVars();
// 并且数据合法
if (!std::isnan(result_value)) {
task_return_data.is_valid = true;
@ -1411,14 +1069,14 @@ TaskReturnType ExpBase::task_base_proc() {
}
// 表达式-样本,无需反馈
else if (!std::isnan(result_value)) {
if (exp_feedback_ != nullptr) {
if (exp_feedback_->evaluate()) {
try {
if (expr_engine_->evaluateBool("feedback")) {
task_return_data.is_valid = true;
task_return_data.value = result_value;
logger_->Debug() << "实时值:" << result_value << endl;
print_exp_vars();
expr_engine_->printVars();
}
} else {
} catch (...) {
task_return_data.is_valid = true;
task_return_data.value = result_value;
}
@ -1496,8 +1154,8 @@ bool ExpBase::detect_up_down(const double &value) {
bool ExpBase::task_prr(int row) {
if (this->prr_ == 1) {
exp_mpdule_ptr_->update(queried_data_, queried_time_, row);
bool prr_result = (bool)exp_mpdule_ptr_->get_value("pre_result");
expr_engine_->refreshFromIhdRow(row, queried_data_, queried_time_, now_time_, query_time_range_);
bool prr_result = expr_engine_->evaluateBool("pre_result");
return prr_result;
}
return true;
@ -1542,23 +1200,11 @@ void ExpBase::save_rule_norm_data() {
}
}
void ExpBase::fun_vars_init() {
string prr_exp = this->exp_mpdule_ptr_->get_exp_str("pre_result");
if (prr_exp == "") {
return;
}
auto fun_res = fun_vars_.add_exp_str(prr_exp, &mm_vars);
}
bool ExpBase::get_prr() {
if (this->prr_ == 1) {
exp_mpdule_ptr_->update();
bool prr_result = (bool)exp_mpdule_ptr_->get_value("pre_result");
// 变量刷新已由 exec_mon() 中的 refreshFromMemory 完成,此处只需求值
bool prr_result = expr_engine_->evaluateBool("pre_result");
this->now_prr_ = prr_result;
if (rule_id_ == string(CMemVar::Const()->printRuleid)) {
logger_->Debug() << "ruleid:" << this->rule_id_
<< ",rulename:" << this->rule_name_
<< " get_prr():" << prr_result << std::endl;
}
if (!this->now_prr_) {
/*-------动作反馈标记-重置------*/
@ -1571,19 +1217,3 @@ bool ExpBase::get_prr() {
now_prr_ = true;
return true;
}
void ExpBase::auto_fun_vars_reset() {
if (is_fun_vars_need_reset_) {
if (rule_id_ == string(CMemVar::Const()->printRuleid)) {
logger_->Debug() << "auto_fun_vars_reset,重置fun_vars_状态!--start"
<< std::endl;
print_exp_vars();
}
this->fun_vars_.refresh_fun_vars(true, &mm_vars);
if (rule_id_ == string(CMemVar::Const()->printRuleid)) {
logger_->Debug() << "auto_fun_vars_reset,重置fun_vars_状态!--end"
<< std::endl;
print_exp_vars();
}
}
is_fun_vars_need_reset_ = false;
}

View File

@ -137,37 +137,6 @@ private:
*/
int reload_config_exp_feedback();
protected:
/**
* @brief
* @return int
*/
int refresh_exp_vars_mem();
/**
* @brief ihyperDB查询到的数据载入到表达式变量中
* @param row
* @return int
*/
int refresh_exp_vars_ihd(int row);
/**
* @brief map
* @return int
*/
int first_fill_mm_vars();
/**
* @brief hold变量hold变量有hold(n,T)
* hold(n,T),ntag的序号T
* @return int
*/
int refresh_hold_var();
protected:
/**
* @brief
* @param expstr My Param doc
*/
void print_exp_vars(const string &expstr = "");
protected:
string exp_str_;
@ -176,20 +145,6 @@ protected:
bool feedback_done_ = false;
string unit_;
/**
* @brief
* KeepT , ///< 保持时间
* KeepC, ///<出现次数
* RiseEdge, ///<上升沿出现次数
* Detect ///<为真检测次数
*/
StatExp::FunVars fun_vars_;
std::unique_ptr<mix_cc::matheval::Expression> exp_act_;
std::unique_ptr<mix_cc::matheval::Expression> exp_feedback_;
std::unique_ptr<mix_cc::matheval::Expression> exp_result_;
protected:
const size_t
@ -247,8 +202,6 @@ protected:
bool filter_flag_ = false;
bool is_fun_vars_need_reset_ = false;
protected:
/**
@ -296,18 +249,6 @@ protected:
*/
AlarmInfo get_timeout_alarm();
/**
* @brief hold(n,T)hold变量map初始化
* @param exp_str My Param doc
* @return int
*/
int init_hold_exp_str(const std::string &exp_str);
/**
* @brief
* @param exp_str My Param doc
* @return int
*/
int exp_messy_code_check(const std::string &exp_str);
/**
* @brief
* @return int
@ -355,14 +296,5 @@ protected:
*/
bool task_prr(int row);
/**
* @brief fun_vars_
* fun_vars_替换
*/
void fun_vars_init();
/**
* @brief
* is_fun_vars_need_reset_
*/
void auto_fun_vars_reset();
};