123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include "ModuleBase.h"
- //#include <boost/algorithm/string.hpp>
- namespace mdd {
- class ModuleMerge : public ModuleBase {
- private:
- public:
- ModuleMerge();
- int addModuleInput();
- int removeModuleInput();
- std::string setInputName(const std::string& input_id, const std::string& new_name);
- bool configure(const std::string& config) override;
- std::string getConfiguration() override;
- state update() override;
- };
- ModuleMerge::ModuleMerge()
- :ModuleBase(R"JSON(
- [{
- "name":"inputs",
- "value":[2]
- }])JSON")
- {
- std::vector<double> default_val = { 1 };
- inputs.push_back(std::make_shared<Input>(this, "Value", 0, default_val));
- inputs.push_back(std::make_shared<Input>(this, "Value", 1, default_val));
- outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
- key = "ModuleMerge";
- setName("Merge");
- }
- int ModuleMerge::addModuleInput() {
- std::vector<double> default_val = { 1 };
- inputs.push_back(std::make_shared<Input>(this, "Value", inputs.size(), default_val));
- return inputs.size();
- }
- int ModuleMerge::removeModuleInput() {
- inputs.pop_back();
- return inputs.size();
- }
- std::string ModuleMerge::setInputName(const std::string& input_id, const std::string& new_name) {
- for (int id = 0; id < inputs.size(); ++id) {
- if (getInput(id)->getID() == input_id) {
- getInput(id)->setName(new_name);
- return getInput(id)->getID();
- }
- }
- return "";
- }
- bool ModuleMerge::configure(const std::string& config) {
- bool success = false;
- success = ModuleBase::configure(config);
- json config_parsed = json::parse(config);
- if (!config_parsed.is_array())
- {
- return success;
- }
- for (size_t i = 0; i < config_parsed.size(); i++)
- {
- if (config_parsed[i].contains("name"))
- {
- if (config_parsed[i]["name"].get<std::string>() == "inputs")
- {
- size_t new_length = config_parsed[i]["value"][0].get<int>();
- size_t length = inputs.size();
- if (length - new_length > 0)
- {
- inputs.erase(inputs.end() - (length - new_length), inputs.end());
- }
- else {
- for (size_t j = length; j < new_length; j++)
- {
- inputs.push_back(std::make_shared<Input>(this, "Value", j, std::vector<double> {1}));
- }
- }
- }
- }
- }
- return false;
- }
- std::string ModuleMerge::getConfiguration() {
- json ret = json::parse(ModuleBase::getConfiguration());
- for (size_t i = 0; i < ret.size(); i++)
- {
- if (ret[i].contains("name"))
- {
- if (ret[i]["name"].get<std::string>() == "inputs")
- {
- size_t length = inputs.size();
- for (size_t j = 0; j < length; j++)
- {
- json in;
- in["type"] = inputs[j]->getType();
- in["value"] = inputs[j]->getValue();
- ret[i]["value"].push_back(in);
- }
- }
- }
- }
- return ret.dump();
- }
- state ModuleMerge::update() {
- std::vector<double> ret;
- for (size_t i = 0; i < inputs.size(); i++)
- {
- for (size_t j = 0; j < getInput(i)->getValue().size(); j++)
- {
- ret.push_back(getInput(i)->getValue()[j]);
- }
- }
- return getOutput(0)->setValue(ret);
- }
- }
|