Connector.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #include "Connector.h"
  2. namespace mdd {
  3. json Connector::add(const json& args) {
  4. json ret;
  5. auto jsub = args.find("subject");
  6. auto jobj = args.find("object");
  7. if (jsub == args.end() || jobj == args.end())
  8. {
  9. return ret;
  10. }
  11. auto jsub_typ = jsub->find("type");
  12. if (jsub_typ == jsub->end())
  13. {
  14. return ret;
  15. }
  16. ret["operation"] = "add";
  17. json jargs;
  18. ProcessorBase* proc_ptr = nullptr;
  19. if (*jsub_typ == "processor")
  20. {
  21. if (proc_ptr = dynamic_cast<ProcessorBase*>(&(*_root->getModule(*jsub))))
  22. {
  23. jargs["subject"] = proc_ptr->getIdentifier();
  24. }
  25. else {
  26. json j;
  27. return j;
  28. }
  29. }
  30. auto jobj_typ = jobj->find("type");
  31. if (jobj_typ == jobj->end())
  32. {
  33. json j;
  34. return j;
  35. }
  36. if (*jobj_typ == "processor")
  37. {
  38. proc_ptr->addModule(Registration().generateProcessor((*jobj)["key"].get<std::string>()));
  39. jargs["object"] = proc_ptr->getModules().back()->dump();
  40. }
  41. else if (*jobj_typ == "module")
  42. {
  43. proc_ptr->addModule(Registration().generateModule((*jobj)["key"].get<std::string>()));
  44. jargs["object"] = proc_ptr->getModules().back()->dump();
  45. }
  46. else if(*jobj_typ == "connection")
  47. {
  48. auto jobj_in = jobj->find("input");
  49. auto jobj_out = jobj->find("output");
  50. IModule::Ptr module_in_ptr = _root->getModule(*jobj_in);
  51. IModule::Ptr module_out_ptr = _root->getModule(*jobj_out);
  52. if (module_in_ptr != nullptr && module_out_ptr != nullptr) {
  53. IInput::Ptr in_ptr = nullptr;
  54. std::string id = (*jobj_in)["key"].get<std::string>() + std::to_string((*jobj_in)["appendix"].get<int>());
  55. for (size_t i = 0; i < module_in_ptr->getNumInputs(); i++)
  56. {
  57. auto in = module_in_ptr->getInput(i);
  58. if (in->getID() == id)
  59. {
  60. in_ptr = in;
  61. break;
  62. }
  63. }
  64. IOutput::Ptr out_ptr = nullptr;
  65. id = (*jobj_out)["key"].get<std::string>() + std::to_string((*jobj_out)["appendix"].get<int>());
  66. for (size_t i = 0; i < module_out_ptr->getNumOutputs(); i++)
  67. {
  68. auto out = module_out_ptr->getOutput(i);
  69. if (out->getID() == id)
  70. {
  71. out_ptr = out;
  72. break;
  73. }
  74. }
  75. if (in_ptr != nullptr && out_ptr != nullptr)
  76. {
  77. in_ptr->connect(out_ptr);
  78. jargs["object"] = *jobj;
  79. }
  80. }
  81. }
  82. else if (*jobj_typ == "input") {
  83. IModule::Ptr module_in_ptr = _root->getModule(*jobj);
  84. if (module_in_ptr != nullptr) {
  85. IInput::Ptr in_ptr = nullptr;
  86. std::string id = (*jobj)["key"].get<std::string>() + std::to_string((*jobj)["appendix"].get<int>());
  87. for (size_t i = 0; i < module_in_ptr->getNumInputs(); i++)
  88. {
  89. auto in = module_in_ptr->getInput(i);
  90. if (in->getID() == id)
  91. {
  92. in_ptr = in;
  93. break;
  94. }
  95. }
  96. return proc_ptr->addInput(in_ptr);
  97. }
  98. }
  99. else if (*jobj_typ == "output") {
  100. IModule::Ptr module_out_ptr = _root->getModule(*jobj);
  101. if (module_out_ptr != nullptr) {
  102. IOutput::Ptr out_ptr = nullptr;
  103. std::string id = (*jobj)["key"].get<std::string>() + std::to_string((*jobj)["appendix"].get<int>());
  104. for (size_t i = 0; i < module_out_ptr->getNumOutputs(); i++)
  105. {
  106. auto out = module_out_ptr->getOutput(i);
  107. if (out->getID() == id)
  108. {
  109. out_ptr = out;
  110. break;
  111. }
  112. }
  113. return proc_ptr->addOutput(out_ptr);
  114. }
  115. }
  116. else {
  117. return ret;
  118. }
  119. ret["args"] = jargs;
  120. return ret;
  121. }
  122. json Connector::remove(const json& args) {
  123. json ret;
  124. auto jsub = args.find("subject");
  125. auto jobj = args.find("object");
  126. if (jsub == args.end() || jobj == args.end())
  127. {
  128. return ret;
  129. }
  130. auto jsub_typ = jsub->find("type");
  131. if (jsub_typ == jsub->end())
  132. {
  133. return ret;
  134. }
  135. ret["operation"] = "remove";
  136. json jargs;
  137. if (*jsub_typ == "processor")
  138. {
  139. if (ProcessorBase* proc_ptr = dynamic_cast<ProcessorBase*>(&(*_root->getModule(*jsub))))
  140. {
  141. auto module_ptr = proc_ptr->getModule(*jobj);
  142. jargs["subject"] = proc_ptr->getIdentifier();
  143. jargs["object"] = module_ptr->getIdentifier();
  144. proc_ptr->removeModule(module_ptr);
  145. }
  146. }
  147. ret["args"] = jargs;
  148. return ret;
  149. }
  150. json Connector::change(const json& args) {
  151. json ret;
  152. auto jsub = args.find("subject");
  153. auto jobj = args.find("object");
  154. if (jsub == args.end() || jobj == args.end())
  155. {
  156. return ret;
  157. }
  158. auto jsub_typ = jsub->find("type");
  159. if (jsub_typ == jsub->end())
  160. {
  161. return ret;
  162. }
  163. ret["operation"] = "change";
  164. json jargs;
  165. if (*jsub_typ == "processor")
  166. {
  167. if (*jsub == _root->getIdentifier())
  168. {
  169. jargs["subject"] = _root->getIdentifier();
  170. if (_root->configure(*jobj))
  171. {
  172. jargs["object"] = *jobj;
  173. }
  174. }
  175. else
  176. {
  177. IModule::Ptr module_ptr = _root->getModule(*jsub);
  178. if (module_ptr == nullptr) {
  179. std::cout << "[Connector]: change: Was not able to find module." << std::endl;
  180. return ret;
  181. }
  182. jargs["subject"] = module_ptr->getIdentifier();
  183. module_ptr->configure(*jobj);
  184. auto jobj_conf = jobj->find("configure");
  185. if (jobj_conf != jobj->end())
  186. {
  187. jargs["object"] = module_ptr->dump();
  188. }
  189. else {
  190. jargs["object"] = *jobj;
  191. }
  192. }
  193. }
  194. else if (*jsub_typ == "module")
  195. {
  196. IModule::Ptr module_ptr = _root->getModule(*jsub);
  197. if (module_ptr == nullptr) {
  198. std::cout << "[Connector]: change: Was not able to find module." << std::endl;
  199. return ret;
  200. }
  201. jargs["subject"] = module_ptr->getIdentifier();
  202. module_ptr->configure(*jobj);
  203. if (jobj->contains("configure"))
  204. {
  205. jargs["object"] = module_ptr->dump();
  206. }
  207. else {
  208. jargs["object"] = *jobj;
  209. }
  210. }
  211. else if (*jsub_typ == "input")
  212. {
  213. IModule::Ptr module_ptr = _root->getModule(*jsub);
  214. if (module_ptr == nullptr) {
  215. std::cout << "[Connector]: change: Was not able to find module." << std::endl;
  216. return ret;
  217. }
  218. IInput::Ptr in_ptr = module_ptr->getInput(*jsub);
  219. if (in_ptr == nullptr) {
  220. std::cout << "[Connector]: change: Was not able to find input." << std::endl;
  221. return ret;
  222. }
  223. in_ptr->configure(*jobj);
  224. jargs["subject"] = in_ptr->getIdentifier();
  225. jargs["object"] = *jobj;
  226. }
  227. else if (*jsub_typ == "output")
  228. {
  229. IModule::Ptr module_ptr = _root->getModule(*jsub);
  230. if (module_ptr == nullptr) {
  231. std::cout << "[Connector]: change: Was not able to find module." << std::endl;
  232. return ret;
  233. }
  234. IOutput::Ptr out_ptr = module_ptr->getOutput(*jsub);
  235. if (out_ptr == nullptr) {
  236. std::cout << "[Connector]: change: Was not able to find output." << std::endl;
  237. return ret;
  238. }
  239. out_ptr->configure(*jobj);
  240. jargs["subject"] = out_ptr->getIdentifier();
  241. jargs["object"] = *jobj;
  242. }
  243. else if (*jsub_typ == "optimizer") {
  244. _opt->configure(*jobj);
  245. jargs["subject"] = _opt->getIdentifier();
  246. jargs["object"] = *jobj;
  247. }
  248. else {
  249. json j;
  250. return j;
  251. }
  252. ret["args"] = jargs;
  253. return ret;
  254. }
  255. json Connector::state(const json& args) {
  256. json ret;
  257. bool found = false;
  258. //init
  259. if (args == "all")
  260. {
  261. ret = encode();
  262. }
  263. //Toolbox
  264. if (args.contains("module"))
  265. {
  266. found = true;
  267. if (args["module"] == "all")
  268. {
  269. auto modules = regi.getModules();
  270. for (size_t i = 0; i < modules.size(); i++)
  271. {
  272. json jmodule;
  273. jmodule["key"] = modules[i];
  274. jmodule["type"] = "module";
  275. ret["module"].push_back(jmodule);
  276. }
  277. }
  278. }
  279. if (args.contains("processor"))
  280. {
  281. found = true;
  282. if (args["processor"] == "all")
  283. {
  284. auto processors = regi.getProcessors();
  285. for (size_t i = 0; i < processors.size(); i++)
  286. {
  287. json jprocessor;
  288. jprocessor["key"] = processors[i];
  289. jprocessor["type"] = "processor";
  290. ret["processor"].push_back(jprocessor);
  291. }
  292. }
  293. }
  294. //
  295. auto jtype = args.find("type");
  296. if (jtype != args.end())
  297. {
  298. found = true;
  299. if (*jtype == "optimizer")
  300. {
  301. ret["info"] = _opt->getConfiguration();
  302. //ret["info"]["ID"] = _opt->getIdentifier();
  303. }
  304. else
  305. {
  306. auto module_ptr = _root->getModule(args);
  307. if (*jtype == "processor")
  308. {
  309. if (IProcessor* processor_ptr = dynamic_cast<IProcessor*>(&(*module_ptr)))
  310. {
  311. ret["info"] = processor_ptr->getConfiguration();
  312. //ret["info"]["ID"] = processor_ptr->getIdentifier();
  313. }
  314. }
  315. else if (*jtype == "module")
  316. {
  317. ret["info"] = module_ptr->getConfiguration();
  318. //ret["info"]["ID"] = module_ptr->getIdentifier();
  319. }
  320. else if (*jtype == "input")
  321. {
  322. auto ptr = module_ptr->getInput(args);
  323. ret["info"] = ptr->getConfiguration();
  324. //ret["info"]["ID"] = ptr->getIdentifier();
  325. }
  326. else if (*jtype == "output")
  327. {
  328. auto ptr = module_ptr->getOutput(args);
  329. ret["info"] = ptr->getConfiguration();
  330. //ret["info"]["ID"] = ptr->getIdentifier();
  331. }
  332. }
  333. }
  334. if (!found)
  335. {
  336. ret["error"] = "Reached end of state parser!";
  337. }
  338. //std::cout << "[Connector]: state: " << j.dump() << std::endl;
  339. json jret;
  340. jret["operation"] = "state";
  341. jret["args"] = ret;
  342. return jret;
  343. }
  344. json Connector::save(const json& args) {
  345. std::ofstream out("save.json");
  346. json jfile;
  347. jfile["processor"] = _root->dump();
  348. jfile["optimizer"] = _opt->dump();
  349. out << std::setw(4) << jfile << std::endl;
  350. json ret;
  351. ret["operation"] = "save";
  352. return ret;
  353. }
  354. json Connector::load(const json& args) {
  355. if (std::filesystem::exists("save.json")) {
  356. std::ifstream in("save.json");
  357. json jfile;
  358. in >> jfile;
  359. auto jproc = jfile.find("processor");
  360. if (jproc != jfile.end())
  361. {
  362. if (jproc->contains("key"))
  363. {
  364. _root = regi.generateProcessor((*jproc)["key"].get<std::string>());
  365. _root->load((*jproc));
  366. }
  367. }
  368. auto jopt = jfile.find("optimizer");
  369. if (jopt != jfile.end()) {
  370. if (jopt->contains("key"))
  371. {
  372. _opt = regi.generateOptimizer((*jopt)["key"].get<std::string>());
  373. _opt->connect(_root);
  374. _opt->load((*jopt));
  375. }
  376. }
  377. }
  378. else
  379. {
  380. _root = regi.generateProcessor("ProcessorStandard");
  381. json jconfig = _root->getConfiguration();
  382. jconfig["GUI"]["name"]["value"] = "root";
  383. _root->configure(jconfig);
  384. _opt = regi.generateOptimizer("OptimizerEvolutionary");
  385. _opt->connect(_root);
  386. }
  387. _opt->attachCallback(std::bind(&Connector::optimizerCallback,this,std::placeholders::_1));
  388. json ret;
  389. ret["operation"] = "state";
  390. ret["args"].push_back(encode());
  391. return ret;
  392. }
  393. json Connector::optimizerCallback(const json& callback) {
  394. auto jit = callback.find("operation");
  395. if (jit != callback.end())
  396. {
  397. if (jit->get<std::string>() == "autosave")
  398. {
  399. return save(callback);
  400. }
  401. }
  402. return _callback(callback);
  403. }
  404. Connector::Connector()
  405. {
  406. json path;
  407. load(path);
  408. }
  409. json Connector::decode(const json& request)
  410. {
  411. json ret;
  412. // check for correct structure
  413. if (!request.contains("operation") || !request.contains("args")) {
  414. ret["error"] = "Missing json member!";
  415. std::cout << "[Connector]: " << "Missing json member!" << std::endl;
  416. return ret;
  417. }
  418. auto it = _ops.find(request["operation"].get<std::string>());
  419. if (it != _ops.end())
  420. {
  421. ret = it->second(request["args"]);
  422. //std::cout << "[Connector]: " << ret.dump() << std::endl;
  423. return ret;
  424. }
  425. ret["error"] = "Operation is not supported!";
  426. return ret;
  427. }
  428. json Connector::encode()
  429. {
  430. json ret;
  431. ret["processor"] = _root->dump();
  432. ret["optimizer"]["permutations"] = _opt->getPermutations();
  433. std::cout << "[Connector]: encode: " << ret.dump() << std::endl;
  434. return ret;
  435. }
  436. void Connector::attachCallback(std::function<json(const json&)> callback)
  437. {
  438. _callback = callback;
  439. }
  440. }