ModuleBase.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "ModuleBase.h"
  2. #include <algorithm>
  3. namespace mdd {
  4. size_t ModuleBase::getNumInputs(){
  5. return inputs.size();
  6. }
  7. size_t ModuleBase::getNumOutputs() {
  8. return outputs.size();
  9. }
  10. std::shared_ptr<IInput> ModuleBase::getInput(size_t index) {
  11. if (index <= inputs.size())
  12. {
  13. return inputs[index];
  14. }
  15. return nullptr;
  16. }
  17. std::shared_ptr<IInput> ModuleBase::getInput(const json& jid) {
  18. std::string sid = jid["key"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
  19. for(auto& input : inputs){
  20. if (auto in_ptr = input)
  21. {
  22. if (in_ptr->getID() == sid) {
  23. return in_ptr;
  24. }
  25. }
  26. }
  27. return nullptr;
  28. }
  29. std::shared_ptr<IOutput> ModuleBase::getOutput(size_t index) {
  30. if (index <= outputs.size())
  31. {
  32. return outputs[index];
  33. }
  34. return nullptr;
  35. }
  36. std::shared_ptr<IOutput> ModuleBase::getOutput(const json& jid) {
  37. std::string sid = jid["key"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
  38. for (auto& output : outputs) {
  39. if (auto out_ptr = output)
  40. {
  41. if (out_ptr->getID() == sid) {
  42. return out_ptr;
  43. }
  44. }
  45. }
  46. return nullptr;
  47. }
  48. std::vector<std::shared_ptr<IInput>> ModuleBase::getOptimizableInputs() {
  49. std::vector<std::shared_ptr<IInput>> ret;
  50. for (size_t i = 0; i < inputs.size(); i++)
  51. {
  52. if (auto in_ptr = inputs[i])
  53. {
  54. if (in_ptr->isOptimizable())
  55. {
  56. ret.push_back(in_ptr);
  57. }
  58. }
  59. }
  60. return ret;
  61. }
  62. std::vector<std::shared_ptr<IOutput>> ModuleBase::getOptimizableOutputs() {
  63. std::vector<std::shared_ptr<IOutput>> ret;
  64. for (size_t i = 0; i < outputs.size(); i++)
  65. {
  66. if (auto out_ptr = outputs[i])
  67. {
  68. if (out_ptr->isOptimizable())
  69. {
  70. ret.push_back(out_ptr);
  71. }
  72. }
  73. }
  74. return ret;
  75. }
  76. ModuleBase::ModuleBase(const std::string& base_config)
  77. {
  78. json jparse = json::parse(base_config);
  79. if (jparse.contains("configure"))
  80. {
  81. _base_config = jparse;
  82. }
  83. else {
  84. _base_config["configure"] = jparse;
  85. }
  86. }
  87. bool ModuleBase::configure(const json& config) {
  88. json jconfig = config;
  89. bool res = true;
  90. auto jit = jconfig.find("GUI");
  91. if (jit != jconfig.end())
  92. {
  93. _base_config["GUI"].merge_patch(jit.value());
  94. }
  95. jit = jconfig.find("configure");
  96. if (jit != jconfig.end()) {
  97. res = configureChild(jit.value());
  98. }
  99. return res;
  100. }
  101. bool configureThis(const json& config) {
  102. return true;
  103. }
  104. const json& ModuleBase::getConfiguration()
  105. {
  106. return _base_config;
  107. }
  108. std::string ModuleBase::getType() {
  109. return type;
  110. }
  111. void ModuleBase::setParent(std::shared_ptr<IModule> parent) {
  112. _parent = parent;
  113. }
  114. std::vector<std::string> ModuleBase::getParentID() {
  115. std::vector<std::string> ret;
  116. if (auto parent_ptr = _parent.lock()) {
  117. ret = parent_ptr->getParentID();
  118. ret.push_back(parent_ptr->getID());
  119. }
  120. return ret;
  121. }
  122. std::string ModuleBase::getGeneratorKey() {
  123. return key;
  124. }
  125. std::string ModuleBase::setAppendix(int appendix) {
  126. _appendix = appendix;
  127. return getID();
  128. }
  129. int ModuleBase::getAppendix() {
  130. return _appendix;
  131. }
  132. std::string ModuleBase::getID() {
  133. return key + std::to_string(_appendix);
  134. }
  135. void ModuleBase::load(const json& j)
  136. {
  137. configure(j);
  138. auto it = j.find("inputs");
  139. if (it!=j.end())
  140. {
  141. const size_t start_counter = inputs.size();
  142. const json& jin = it.value();
  143. std::vector<double> default_val = { 1 };
  144. for (size_t i = start_counter; i < jin.size(); i++)
  145. {
  146. inputs.push_back(std::make_shared<Input>("Value", 0, default_val));
  147. inputs.back()->load(jin[i]);
  148. }
  149. for (size_t i = 0; i < jin.size(); i++)
  150. {
  151. inputs[i]->setParent(shared_from_this());
  152. inputs[i]->configure(jin[i]);
  153. }
  154. }
  155. it = j.find("outputs");
  156. if (it != j.end())
  157. {
  158. const size_t start_counter = outputs.size();
  159. const json& jout = it.value();
  160. for (size_t i = start_counter; i < j["outputs"].size(); i++)
  161. {
  162. outputs.push_back(std::make_shared<Output>( "Value", 0));
  163. outputs.back()->load(jout[i]);
  164. }
  165. for (size_t i = 0; i < jout.size(); i++)
  166. {
  167. outputs[i]->setParent(shared_from_this());
  168. inputs[i]->configure(jout[i]);
  169. }
  170. }
  171. }
  172. json ModuleBase::dump()
  173. {
  174. json jdump;
  175. jdump = getConfiguration();
  176. jdump["ID"] = getIdentifier();
  177. jdump["type"] = type;
  178. jdump["key"] = key;
  179. jdump["inputs"].clear();
  180. jdump["outputs"].clear();
  181. for (auto& in : inputs)
  182. {
  183. in->setParent(shared_from_this());
  184. jdump["inputs"].push_back(in->dump());
  185. }
  186. for (auto& out : outputs)
  187. {
  188. out->setParent(shared_from_this());
  189. jdump["outputs"].push_back(out->dump());
  190. }
  191. return jdump;
  192. }
  193. void ModuleBase::disconnect() {
  194. for (size_t i = 0; i < inputs.size(); i++)
  195. {
  196. if (auto in_ptr = inputs[i])
  197. {
  198. in_ptr->disconnect();
  199. }
  200. }
  201. for (size_t i = 0; i < outputs.size(); i++)
  202. {
  203. if (auto out_ptr = outputs[i])
  204. {
  205. out_ptr->disconnect();
  206. }
  207. }
  208. }
  209. json ModuleBase::getIdentifier() {
  210. json jID;
  211. jID["key"] = key;
  212. jID["appendix"] = getAppendix();
  213. jID["type"] = type;
  214. jID["prefix"] = getParentID();
  215. return jID;
  216. }
  217. }