ModuleMerge.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "ModuleBase.h"
  2. //#include <boost/algorithm/string.hpp>
  3. namespace mdd {
  4. class ModuleMerge : public ModuleBase {
  5. private:
  6. public:
  7. ModuleMerge();
  8. int addModuleInput();
  9. int removeModuleInput();
  10. std::string setInputName(const std::string& input_id, const std::string& new_name);
  11. bool configure(const std::string& config) override;
  12. std::string getConfiguration() override;
  13. state update() override;
  14. };
  15. ModuleMerge::ModuleMerge()
  16. :ModuleBase(R"JSON(
  17. [{
  18. "name":"inputs",
  19. "value":[2]
  20. }])JSON")
  21. {
  22. std::vector<double> default_val = { 1 };
  23. inputs.push_back(std::make_shared<Input>(this, "Value", 0, default_val));
  24. inputs.push_back(std::make_shared<Input>(this, "Value", 1, default_val));
  25. outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
  26. key = "ModuleMerge";
  27. setName("Merge");
  28. }
  29. int ModuleMerge::addModuleInput() {
  30. std::vector<double> default_val = { 1 };
  31. inputs.push_back(std::make_shared<Input>(this, "Value", inputs.size(), default_val));
  32. return inputs.size();
  33. }
  34. int ModuleMerge::removeModuleInput() {
  35. inputs.pop_back();
  36. return inputs.size();
  37. }
  38. std::string ModuleMerge::setInputName(const std::string& input_id, const std::string& new_name) {
  39. for (int id = 0; id < inputs.size(); ++id) {
  40. if (getInput(id)->getID() == input_id) {
  41. getInput(id)->setName(new_name);
  42. return getInput(id)->getID();
  43. }
  44. }
  45. return "";
  46. }
  47. bool ModuleMerge::configure(const std::string& config) {
  48. bool success = false;
  49. success = ModuleBase::configure(config);
  50. json config_parsed = json::parse(config);
  51. if (!config_parsed.is_array())
  52. {
  53. return success;
  54. }
  55. for (size_t i = 0; i < config_parsed.size(); i++)
  56. {
  57. if (config_parsed[i].contains("name"))
  58. {
  59. if (config_parsed[i]["name"].get<std::string>() == "inputs")
  60. {
  61. size_t new_length = config_parsed[i]["value"][0].get<int>();
  62. size_t length = inputs.size();
  63. if (length - new_length > 0)
  64. {
  65. inputs.erase(inputs.end() - (length - new_length), inputs.end());
  66. }
  67. else {
  68. for (size_t j = length; j < new_length; j++)
  69. {
  70. inputs.push_back(std::make_shared<Input>(this, "Value", j, std::vector<double> {1}));
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. std::string ModuleMerge::getConfiguration() {
  79. json ret = json::parse(ModuleBase::getConfiguration());
  80. for (size_t i = 0; i < ret.size(); i++)
  81. {
  82. if (ret[i].contains("name"))
  83. {
  84. if (ret[i]["name"].get<std::string>() == "inputs")
  85. {
  86. size_t length = inputs.size();
  87. for (size_t j = 0; j < length; j++)
  88. {
  89. json in;
  90. in["type"] = inputs[j]->getType();
  91. in["value"] = inputs[j]->getValue();
  92. ret[i]["value"].push_back(in);
  93. }
  94. }
  95. }
  96. }
  97. return ret.dump();
  98. }
  99. state ModuleMerge::update() {
  100. std::vector<double> ret;
  101. for (size_t i = 0; i < inputs.size(); i++)
  102. {
  103. for (size_t j = 0; j < getInput(i)->getValue().size(); j++)
  104. {
  105. ret.push_back(getInput(i)->getValue()[j]);
  106. }
  107. }
  108. return getOutput(0)->setValue(ret);
  109. }
  110. }