#pragma once /** * @file exp_sample_multi_dim.h * @brief 多维样本 高斯拟合 * @author your name (you@domain.com) * @version 0.1 * @date 2023-12-22 * * Copyright: Baosight Co. Ltd. * DO NOT COPY/USE WITHOUT PERMISSION * */ #include #include #include #include using namespace nlohmann; /** * @brief 表达式-多维样本类的实现 * @tparam dims */ class ExpSampleMultiDim : public ExpSample { public: ExpSampleMultiDim(const string& name, const mix_cc::json& rule_json, const string& ruleId, size_t dims, double padding_low, double padding_up) : ExpSample(name, rule_json, ruleId, dims, padding_low, padding_up) {} ~ExpSampleMultiDim() {} int init() override; int reload_config_xyz(); /** * @brief 基本过程,用来组成高级过程 * @tparam T * @tparam ReturnType * @return ReturnType */ template ReturnType base_proc() { try { act_triggered_ = static_cast(exp_act_->evaluate()); if (act_triggered_) { double x1, x2, x3 = 1; if (dims_ > 0) { x1 = exp_xs_[0]->evaluate(); } if (dims_ > 1) { x2 = exp_xs_[1]->evaluate(); } if (dims_ > 2) { x3 = exp_xs_[2]->evaluate(); } if ((!std::isnan(x1) && !std::isnan(x2) && !std::isnan(x2)) && (x1 != 0 || x2 != 0 || x3 != 0)) { if constexpr (is_same_v) { if (dims_ == 1) { auto alarm_info = sample_stat_->auto_detect_and_save( SamplePoint{x1}, now_time_); // !IMPORTANT@ NEED TO MODIFY LATTER if (alarm_info) { return utility::build_alarm_info( MsgLevel::ERROR, rule_id_, rule_name_, "POLYFIT", error_str_ + alarm_info.alarm_str, alarm_info.value, alarm_info.range, query_time_range_); } } if (dims_ == 2) { auto alarm_info = sample_stat_->auto_detect_and_save( SamplePoint{x1, x2}, now_time_); // !IMPORTANT@ NEED TO MODIFY LATTER if (alarm_info) { return utility::build_alarm_info( MsgLevel::ERROR, rule_id_, rule_name_, "POLYFIT", error_str_ + alarm_info.alarm_str, alarm_info.value, alarm_info.range, query_time_range_); } } if (dims_ == 3) { auto alarm_info = sample_stat_->auto_detect_and_save( SamplePoint{x1, x2, x3}, now_time_); // !IMPORTANT@ NEED TO MODIFY LATTER if (alarm_info) { return utility::build_alarm_info( MsgLevel::ERROR, rule_id_, rule_name_, "POLYFIT", error_str_ + alarm_info.alarm_str, alarm_info.value, alarm_info.range, query_time_range_); } } } else if constexpr (is_same_v) { if (dims_ == 1) { return std::make_optional(SamplePoint{x1}); } if (dims_ == 2) { return std::make_optional(SamplePoint{x1, x2}); } if (dims_ == 3) { return std::make_optional(SamplePoint{x1, x2, x3}); } } } } } catch (const std::exception& e) { std::throw_with_nested( mix_cc::Exception(-1, "calc_once error", BOOST_CURRENT_LOCATION)); } if constexpr (is_same_v) { return AlarmInfo{}; } else if constexpr (is_same_v) { return std::nullopt; } } /** * @brief 监控过程 * @return AlarmInfo */ AlarmInfo mon_proc() override { return base_proc(); } /** * @brief 定期采样过程 * @return std::optional> */ std::optional cron_proc_sample() override { return base_proc>(); } protected: std::vector> exp_xs_; ///< 样本表达式数组 std::string sample_tag_x1_str_; ///< x1 样本表达式字符串 std::string sample_tag_x2_str_; ///< x2 样本表达式字符串 std::string sample_tag_x3_str_; ///< x3 样本表达式字符串 };