// // Created by fly on 2022/4/21. // #ifndef FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP #define FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP #include "algorithm_wrap.hpp" #include #include #include #include #include class simulation_manager { public: void add_algorithm(std::string const &name, std::shared_ptr algo) { if (!algorithms_.contains(name)) { algorithms_.emplace(name, std::make_shared(algo)); } } void remove_algorithm(std::string const &name) { algorithms_.erase(name); } void set_algorithm_enable(std::string const &name, bool flag) { if (algorithms_.contains(name)) { algorithms_[name]->set_enable(flag); } } bool algorithm_enable(std::string const &name) { if (algorithms_.contains(name)) { return algorithms_[name]->enable(); } return false; } void set_algorithm_duration_period(std::string const &name, int duration_period) { if (algorithms_.contains(name)) { algorithms_[name]->set_duration_period(duration_period); } } int algorithm_duration_period(std::string const &name) { if (algorithms_.contains(name)) { return algorithms_[name]->duration_period(); } return -1; } void set_algorithm_config(std::string const &name, nlohmann::json const &config) { if (!algorithms_.contains(name)) { spdlog::debug("Create {} Algorithm Object", name); if ("periodic_interference" == name) { algorithms_[name] = std::make_shared(std::make_shared()); } else if ("step" == name) { algorithms_[name] = std::make_shared(std::make_shared()); } else if ("white_noise" == name) { algorithms_[name] = std::make_shared(std::make_shared()); } else if ("temperature_drift" == name) { algorithms_[name] = std::make_shared(std::make_shared()); } else if ("pulse" == name) { algorithms_[name] = std::make_shared(std::make_shared()); } } if (algorithms_.contains(name)) { algorithms_[name]->set_config(config); } } nlohmann::json algorithm_config(std::string const &name) { if (algorithms_.contains(name)) { return algorithms_[name]->config(); } return {}; } nlohmann::json config() { nlohmann::json j; for (auto iter = algorithms_.begin(); iter != algorithms_.end(); ++iter) { j[iter->first] = iter->second->config(); } return j; } void set_config(nlohmann::json const &c) { for (auto iter = c.begin(); iter != c.end(); ++iter) { set_algorithm_config(iter.key(), iter.value()); } } double eval(double value) { for (auto &iter: algorithms_) { value = iter.second->eval(value); } return value; } private: std::map> algorithms_; }; #endif //FAULT_SIMULATION_ALGORITHM_SIMULATION_MANAGER_HPP