refactor: 提取 BoundChecker 上下限检测组件

从 ExpBase 提取 detect_up_down() 逻辑和哨兵值处理至独立的 BoundChecker 类。
将 DetectMode 从 struct 升级为 enum class。
This commit is contained in:
Huamonarch 2026-05-15 14:09:56 +08:00
parent c4bcb6610b
commit b9cf5f4e9e
5 changed files with 110 additions and 23 deletions

View File

@ -336,19 +336,19 @@ AlarmInfo ExpBase::mon_proc() {
SingletonTemp<EqpStat>::GetInstance().add_stat_values( SingletonTemp<EqpStat>::GetInstance().add_stat_values(
rule_id_, result_value); rule_id_, result_value);
} }
if (detect_up_down(result_value)) { if (bound_checker_.isOutOfBounds(result_value)) {
rule_stat_.alarm_value = result_value; rule_stat_.alarm_value = result_value;
std::string msg = ""; std::string msg = "";
if (fb_fsm_.isTimeMode()) { if (fb_fsm_.isTimeMode()) {
msg = error_str_ + ":" + DAA::double2str(result_value) + msg = error_str_ + ":" + DAA::double2str(result_value) +
"ms,时间范围:[0," + DAA::double2str(limit_up_) + "] ms"; "ms,时间范围:[0," + DAA::double2str(bound_checker_.limitUp()) + "] ms";
} else { } else {
msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ + msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ +
",合理区间:[" + DAA::double2strLimit(limit_down_) + "," + ",合理区间:[" + DAA::double2strLimit(bound_checker_.limitDown()) + "," +
DAA::double2strLimit(limit_up_) + "]" + unit_; DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
} }
return utility::build_alarm_info( return utility::build_alarm_info(
utility::get_msg_level(limit_down_, limit_up_, result_value), utility::get_msg_level(bound_checker_.limitDown(), bound_checker_.limitUp(), result_value),
rule_id_, rule_name_, "EXP4", msg, get_alarm_time()); rule_id_, rule_name_, "EXP4", msg, get_alarm_time());
} }
} else { } else {
@ -382,11 +382,11 @@ AlarmInfo ExpBase::mon_proc() {
// 不是动作反馈 // 不是动作反馈
else { else {
if (exp_type_ == ExpType::Bound) { if (exp_type_ == ExpType::Bound) {
if (filter_flag_ == true && this->detect_up_down(result_value)) { if (filter_flag_ == true && bound_checker_.isOutOfBounds(result_value)) {
rule_stat_.alarm_value = result_value; rule_stat_.alarm_value = result_value;
auto msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ + auto msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ +
",合理区间:[" + DAA::double2strLimit(limit_down_) + "," + ",合理区间:[" + DAA::double2strLimit(bound_checker_.limitDown()) + "," +
DAA::double2strLimit(limit_up_) + "]" + unit_; DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
logger_->Debug() << msg << endl; logger_->Debug() << msg << endl;
this->query_time_range_.set_left(query_time_range_.get_right() - this->query_time_range_.set_left(query_time_range_.get_right() -
delay_time_); delay_time_);
@ -399,7 +399,7 @@ AlarmInfo ExpBase::mon_proc() {
else if (exp_type_ == ExpType::BoundHoldTime) { else if (exp_type_ == ExpType::BoundHoldTime) {
bool is_over_up_down = bool is_over_up_down =
this->detect_up_down(result_value); bound_checker_.isOutOfBounds(result_value);
if (!filter_flag_) { if (!filter_flag_) {
/*前提条件不满足*/ /*前提条件不满足*/
act_start_time_ = this->now_time_; act_start_time_ = this->now_time_;
@ -420,8 +420,8 @@ AlarmInfo ExpBase::mon_proc() {
rule_stat_.alarm_value = result_value; rule_stat_.alarm_value = result_value;
auto msg = error_str_ + ":" + DAA::double2str(result_value) + auto msg = error_str_ + ":" + DAA::double2str(result_value) +
unit_ + ",合理区间:[" + unit_ + ",合理区间:[" +
DAA::double2strLimit(limit_down_) + "," + DAA::double2strLimit(bound_checker_.limitDown()) + "," +
DAA::double2strLimit(limit_up_) + "]" + unit_; DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
expr_engine_->printVars(); expr_engine_->printVars();
this->query_time_range_.set_left(query_time_range_.get_right() - this->query_time_range_.set_left(query_time_range_.get_right() -
delay_time_); delay_time_);
@ -430,7 +430,7 @@ AlarmInfo ExpBase::mon_proc() {
this->now_time_; this->now_time_;
act_started_ = false; act_started_ = false;
return utility::build_alarm_info( return utility::build_alarm_info(
utility::get_msg_level(limit_down_, limit_up_, result_value), utility::get_msg_level(bound_checker_.limitDown(), bound_checker_.limitUp(), result_value),
rule_id_, rule_name_, "EXP5", msg, this->get_alarm_time()); rule_id_, rule_name_, "EXP5", msg, this->get_alarm_time());
} }
} else { } else {
@ -735,10 +735,11 @@ int ExpBase::reload_config_up_down() {
(int)limit_up_ == (int)limit_down_) { (int)limit_up_ == (int)limit_down_) {
this->detect_mode_ = DetectMode::ErrorMode; this->detect_mode_ = DetectMode::ErrorMode;
} }
bound_checker_.setLimits(limit_down_, limit_up_);
///共享内存参数 ///共享内存参数
logger_->Info() << rule_name_ logger_->Info() << rule_name_
<< ",detect_mode_[0-双侧1-仅left2-仅right3-错误]:" << ",detect_mode_[0-双侧1-仅left2-仅right3-错误]:"
<< detect_mode_ << ",limit_down:" << limit_down_ << "," << static_cast<int>(bound_checker_.detectMode()) << ",limit_down:" << limit_down_ << ","
<< "limit_up:" << limit_up_ << std::endl; << "limit_up:" << limit_up_ << std::endl;
this->rule_stat_.limit_down = limit_down_; this->rule_stat_.limit_down = limit_down_;
this->rule_stat_.limit_up = limit_up_; this->rule_stat_.limit_up = limit_up_;
@ -819,6 +820,7 @@ int ExpBase::reload_ci_dist() {
} }
this->limit_down_ = dist_range.get_left(); this->limit_down_ = dist_range.get_left();
this->limit_up_ = dist_range.get_right(); this->limit_up_ = dist_range.get_right();
bound_checker_.setLimits(limit_down_, limit_up_);
this->rule_stat_.limit_down = limit_down_; this->rule_stat_.limit_down = limit_down_;
this->rule_stat_.limit_up = limit_up_; this->rule_stat_.limit_up = limit_up_;
this->rule_stat_.current_value = this->rule_stat_.current_value =

View File

@ -21,6 +21,7 @@
#include <eqpalg/utility/HoldTime.h> #include <eqpalg/utility/HoldTime.h>
#include <eqpalg/utility/StatExp.hpp> #include <eqpalg/utility/StatExp.hpp>
#include <eqpalg/utility/item2chinese.hpp> #include <eqpalg/utility/item2chinese.hpp>
#include <eqpalg/utility/bound_checker.h>
#include <eqpalg/utility/fb_state_machine.h> #include <eqpalg/utility/fb_state_machine.h>
#include <glob/SingletonTemplate.h> #include <glob/SingletonTemplate.h>
#include <iomanip> #include <iomanip>
@ -193,7 +194,9 @@ protected:
std::string sample_result_; std::string sample_result_;
int detect_mode_ = DetectMode::Default; DetectMode detect_mode_ = DetectMode::Default;
BoundChecker bound_checker_;
bool filter_flag_ = false; bool filter_flag_ = false;

View File

@ -57,15 +57,7 @@ struct DistMode {
static const int Online = 1; static const int Online = 1;
static const int Offline = 2; static const int Offline = 2;
}; };
/** // DetectMode 已提取至 eqpalg/utility/bound_checker.henum class
* @brief
*/
struct DetectMode {
static const int Default = 0;
static const int OnlyLeft = 1;
static const int OnlyRight = 2;
static const int ErrorMode = 3;
};
/** /**
* @brief * @brief
*/ */

View File

@ -0,0 +1,34 @@
// eqpalg/utility/bound_checker.cpp
#include <eqpalg/utility/bound_checker.h>
void BoundChecker::setLimits(double down, double up) {
limit_down_ = down;
limit_up_ = up;
// 哨兵值推导检测模式(与 ExpBase::reload_config_up_down 逻辑一致)
int idown = static_cast<int>(down);
int iup = static_cast<int>(up);
if (idown == -32768 && iup != idown) {
detect_mode_ = DetectMode::OnlyRight; // 仅右边界
} else if ((iup == -32768 || iup == 32768 || iup == 32767) && iup != idown) {
detect_mode_ = DetectMode::OnlyLeft; // 仅左边界
} else if (iup == -32768 && iup == idown) {
detect_mode_ = DetectMode::ErrorMode; // 配置错误
} else {
detect_mode_ = DetectMode::Default; // 双侧
}
}
bool BoundChecker::isOutOfBounds(double value) const {
switch (detect_mode_) {
case DetectMode::Default:
return value < limit_down_ || value > limit_up_;
case DetectMode::OnlyLeft:
return value < limit_down_;
case DetectMode::OnlyRight:
return value > limit_up_;
default:
return false;
}
}

View File

@ -0,0 +1,56 @@
// eqpalg/utility/bound_checker.h
#pragma once
#include <string>
/**
* @brief
*/
enum class DetectMode {
Default = 0, // 双侧检测 (value < down || value > up)
OnlyLeft = 1, // 仅检测左边界 (value < down)
OnlyRight = 2, // 仅检测右边界 (value > up)
ErrorMode = 3 // 错误模式
};
/**
* @brief
*
* ExpBase -32768/32767
*/
class BoundChecker {
public:
BoundChecker() = default;
/**
* @brief
* @param down -32768
* @param up 32767/32768
*/
void setLimits(double down, double up);
/**
* @brief
*/
void setDetectMode(DetectMode mode) { detect_mode_ = mode; }
/**
* @brief value
*/
bool isOutOfBounds(double value) const;
// 访问器
double limitDown() const { return limit_down_; }
double limitUp() const { return limit_up_; }
DetectMode detectMode() const { return detect_mode_; }
/**
* @brief
*/
bool isValid() const { return detect_mode_ != DetectMode::ErrorMode; }
private:
double limit_down_ = -32768;
double limit_up_ = -32768;
DetectMode detect_mode_ = DetectMode::Default;
};