|
@@ -1,64 +1,55 @@
|
|
|
#include "Connector.h"
|
|
|
namespace mdd {
|
|
|
-
|
|
|
- bool Connector::contains_subj_obj_structure(const json& args) {
|
|
|
- if (args.contains("object") && args.contains("subject"))
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
json Connector::add(const json& args) {
|
|
|
json ret;
|
|
|
- if (!contains_subj_obj_structure(args))
|
|
|
+ auto jsub = args.find("subject");
|
|
|
+ auto jobj = args.find("object");
|
|
|
+ if (jsub == args.end() || jobj == args.end())
|
|
|
{
|
|
|
return ret;
|
|
|
}
|
|
|
- json sub = args["subject"];
|
|
|
- if (!sub.contains("type"))
|
|
|
+
|
|
|
+ auto jsub_typ = jsub->find("type");
|
|
|
+ if (jsub_typ == jsub->end())
|
|
|
{
|
|
|
return ret;
|
|
|
}
|
|
|
ret["operation"] = "add";
|
|
|
json jargs;
|
|
|
- if (sub["type"] == "processor")
|
|
|
+ if (*jsub_typ == "processor")
|
|
|
{
|
|
|
jargs["subject"] = _root->getIdentifier();
|
|
|
}
|
|
|
- else if(sub["type"] == "module")
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
else {
|
|
|
json j;
|
|
|
return j;
|
|
|
}
|
|
|
|
|
|
- json obj = args["object"];
|
|
|
- if (!obj.contains("type"))
|
|
|
+ auto jobj_typ = jobj->find("type");
|
|
|
+ if (jobj_typ == jobj->end())
|
|
|
{
|
|
|
json j;
|
|
|
return j;
|
|
|
}
|
|
|
- if (obj["type"] == "processor")
|
|
|
+
|
|
|
+ if (*jobj_typ == "processor")
|
|
|
{
|
|
|
|
|
|
}
|
|
|
- else if (obj["type"] == "module")
|
|
|
+ else if (*jobj_typ == "module")
|
|
|
{
|
|
|
- _root->addModule(Registration().generateModule(obj["key"].get<std::string>()));
|
|
|
+ _root->addModule(Registration().generateModule((*jobj_typ)["key"].get<std::string>()));
|
|
|
jargs["object"] = _root->getModules().back()->dump();
|
|
|
}
|
|
|
- else if (obj["type"] == "connection")
|
|
|
+ else if(*jobj_typ == "connection")
|
|
|
{
|
|
|
- IModule::Ptr module_in_ptr = _root->getModule(obj["input"]["prefix"].back().get<std::string>());
|
|
|
- IModule::Ptr module_out_ptr = _root->getModule(obj["output"]["prefix"].back().get<std::string>());
|
|
|
+ auto jobj_in = jobj->find("input");
|
|
|
+ auto jobj_out = jobj->find("output");
|
|
|
+ IModule::Ptr module_in_ptr = _root->getModule(*jobj_in);
|
|
|
+ IModule::Ptr module_out_ptr = _root->getModule(*jobj_out);
|
|
|
if (module_in_ptr != nullptr && module_out_ptr != nullptr) {
|
|
|
IInput::Ptr in_ptr = nullptr;
|
|
|
- std::string id = obj["input"]["name"].get<std::string>() + std::to_string(obj["input"]["appendix"].get<int>());
|
|
|
+ std::string id = (*jobj_in)["name"].get<std::string>() + std::to_string((*jobj_in)["appendix"].get<int>());
|
|
|
for (size_t i = 0; i < module_in_ptr->getNumInputs(); i++)
|
|
|
{
|
|
|
auto in = module_in_ptr->getInput(i);
|
|
@@ -70,7 +61,7 @@ namespace mdd {
|
|
|
}
|
|
|
|
|
|
IOutput::Ptr out_ptr = nullptr;
|
|
|
- id = obj["output"]["name"].get<std::string>() + std::to_string(obj["output"]["appendix"].get<int>());
|
|
|
+ id = (*jobj_out)["name"].get<std::string>() + std::to_string((*jobj_out)["appendix"].get<int>());
|
|
|
for (size_t i = 0; i < module_out_ptr->getNumOutputs(); i++)
|
|
|
{
|
|
|
auto out = module_out_ptr->getOutput(i);
|
|
@@ -84,21 +75,9 @@ namespace mdd {
|
|
|
if (in_ptr != nullptr && out_ptr != nullptr)
|
|
|
{
|
|
|
in_ptr->connect(out_ptr);
|
|
|
- jargs["object"] = obj;
|
|
|
+ jargs["object"] = *jobj;
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- else if (obj["type"] == "input")
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
- else if (obj["type"] == "output")
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
- else if (obj["type"] == "configuration")
|
|
|
- {
|
|
|
-
|
|
|
}
|
|
|
else {
|
|
|
return ret;
|
|
@@ -109,25 +88,25 @@ namespace mdd {
|
|
|
|
|
|
json Connector::remove(const json& args) {
|
|
|
json ret;
|
|
|
- if (!contains_subj_obj_structure(args))
|
|
|
+ auto jsub = args.find("subject");
|
|
|
+ auto jobj = args.find("object");
|
|
|
+ if (jsub == args.end() || jobj == args.end())
|
|
|
{
|
|
|
return ret;
|
|
|
}
|
|
|
- json sub = args["subject"];
|
|
|
- if (!sub.contains("type"))
|
|
|
+ auto jsub_typ = jsub->find("type");
|
|
|
+ if (jsub_typ == jsub->end())
|
|
|
{
|
|
|
return ret;
|
|
|
}
|
|
|
- json obj = args["object"];
|
|
|
ret["operation"] = "remove";
|
|
|
json jargs;
|
|
|
- if (sub["type"] == "processor")
|
|
|
+ if (*jsub_typ == "processor")
|
|
|
{
|
|
|
- if (sub == _root->getIdentifier())
|
|
|
+ if (*jsub == _root->getIdentifier())
|
|
|
{
|
|
|
jargs["subject"] = _root->getIdentifier();
|
|
|
- json jobj = obj["object"];
|
|
|
- auto module_ptr = _root->getModule(obj["name"].get<std::string>() + std::to_string(obj["appendix"].get<int>()));
|
|
|
+ auto module_ptr = _root->getModule(*jsub);
|
|
|
jargs["object"] = module_ptr->getIdentifier();
|
|
|
_root->removeModule(module_ptr);
|
|
|
}
|
|
@@ -138,80 +117,82 @@ namespace mdd {
|
|
|
|
|
|
json Connector::change(const json& args) {
|
|
|
json ret;
|
|
|
- if (!contains_subj_obj_structure(args))
|
|
|
+ auto jsub = args.find("subject");
|
|
|
+ auto jobj = args.find("object");
|
|
|
+ if (jsub == args.end() || jobj == args.end())
|
|
|
{
|
|
|
return ret;
|
|
|
}
|
|
|
- json sub = args["subject"];
|
|
|
- if (!sub.contains("type"))
|
|
|
+ auto jsub_typ = jsub->find("type");
|
|
|
+ if (jsub_typ == jsub->end())
|
|
|
{
|
|
|
return ret;
|
|
|
}
|
|
|
- json obj = args["object"];
|
|
|
ret["operation"] = "change";
|
|
|
json jargs;
|
|
|
- if (sub["type"] == "processor")
|
|
|
+ if (*jsub_typ == "processor")
|
|
|
{
|
|
|
- if (sub["subject"] == _root->getIdentifier())
|
|
|
+ if (*jsub == _root->getIdentifier())
|
|
|
{
|
|
|
jargs["subject"] = _root->getIdentifier();
|
|
|
- if (_root->configure(obj.dump()))
|
|
|
+ if (_root->configure(*jobj))
|
|
|
{
|
|
|
- jargs["object"] = obj;
|
|
|
+ jargs["object"] = *jobj;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- IModule::Ptr module_ptr = _root->getModule(sub["name"].get<std::string>() + std::to_string(sub["appendix"].get<int>()));
|
|
|
+ IModule::Ptr module_ptr = _root->getModule(*jsub);
|
|
|
if (module_ptr == nullptr) {
|
|
|
std::cout << "[Connector]: change: Was not able to find module." << std::endl;
|
|
|
return ret;
|
|
|
}
|
|
|
jargs["subject"] = module_ptr->getIdentifier();
|
|
|
- module_ptr->configure(obj);
|
|
|
- if (obj.contains("configure"))
|
|
|
+ module_ptr->configure(*jobj);
|
|
|
+ auto jobj_conf = jobj->find("configure");
|
|
|
+ if (jobj_conf != jobj->end())
|
|
|
{
|
|
|
jargs["object"]["configure"] = module_ptr->dump();
|
|
|
}
|
|
|
else {
|
|
|
- jargs["object"] = obj;
|
|
|
+ jargs["object"] = *jobj;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- else if (sub["type"] == "module")
|
|
|
+ else if (*jsub_typ == "module")
|
|
|
{
|
|
|
- IModule::Ptr module_ptr = _root->getModule(sub["name"].get<std::string>() + std::to_string(sub["appendix"].get<int>()));
|
|
|
+ IModule::Ptr module_ptr = _root->getModule(*jsub);
|
|
|
if (module_ptr == nullptr) {
|
|
|
std::cout << "[Connector]: change: Was not able to find module." << std::endl;
|
|
|
return ret;
|
|
|
}
|
|
|
jargs["subject"] = module_ptr->getIdentifier();
|
|
|
- module_ptr->configure(obj);
|
|
|
- if (obj.contains("configure"))
|
|
|
+ module_ptr->configure(*jobj);
|
|
|
+ if (jobj->contains("configure"))
|
|
|
{
|
|
|
jargs["object"]["configure"] = module_ptr->dump();
|
|
|
}
|
|
|
else {
|
|
|
- jargs["object"] = obj;
|
|
|
+ jargs["object"] = *jobj;
|
|
|
}
|
|
|
}
|
|
|
- else if (sub["type"] == "input")
|
|
|
+ else if (*jsub_typ == "input")
|
|
|
{
|
|
|
- IModule::Ptr module_ptr = _root->getModule(sub["prefix"].back().get<std::string>());
|
|
|
+ IModule::Ptr module_ptr = _root->getModule(*jsub);
|
|
|
if (module_ptr == nullptr) {
|
|
|
std::cout << "[Connector]: change: Was not able to find module." << std::endl;
|
|
|
return ret;
|
|
|
}
|
|
|
- IInput::Ptr in_ptr = module_ptr->getInput(sub["name"].get<std::string>() + std::to_string(sub["appendix"].get<int>()));
|
|
|
+ IInput::Ptr in_ptr = module_ptr->getInput(*jsub);
|
|
|
if (in_ptr == nullptr) {
|
|
|
std::cout << "[Connector]: change: Was not able to find input." << std::endl;
|
|
|
return ret;
|
|
|
}
|
|
|
- in_ptr->configure(obj);
|
|
|
+ in_ptr->configure(*jobj);
|
|
|
jargs["subject"] = in_ptr->getIdentifier();
|
|
|
- jargs["object"] = obj;
|
|
|
+ jargs["object"] = *jobj;
|
|
|
}
|
|
|
- else if (sub["type"] == "output")
|
|
|
+ else if (*jsub_typ == "output")
|
|
|
{
|
|
|
|
|
|
}
|
|
@@ -224,12 +205,14 @@ namespace mdd {
|
|
|
}
|
|
|
|
|
|
json Connector::state(const json& args) {
|
|
|
- json j;
|
|
|
+ json ret;
|
|
|
bool found = false;
|
|
|
+ //init
|
|
|
if (args == "all")
|
|
|
{
|
|
|
- j = encode();
|
|
|
+ ret = encode();
|
|
|
}
|
|
|
+ //Toolbox
|
|
|
if (args.contains("module"))
|
|
|
{
|
|
|
found = true;
|
|
@@ -242,7 +225,7 @@ namespace mdd {
|
|
|
json jmodule;
|
|
|
jmodule["key"] = modules[i];
|
|
|
jmodule["type"] = "module";
|
|
|
- j["module"].push_back(jmodule);
|
|
|
+ ret["module"].push_back(jmodule);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -257,19 +240,48 @@ namespace mdd {
|
|
|
json jprocessor;
|
|
|
jprocessor["key"] = processors[i];
|
|
|
jprocessor["type"] = "module";
|
|
|
- j["processor"].push_back(jprocessor);
|
|
|
+ ret["processor"].push_back(jprocessor);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ auto jtype = args.find("type");
|
|
|
+ if (jtype != args.end())
|
|
|
+ {
|
|
|
+ found = true;
|
|
|
+ auto module_ptr = _root->getModule(args);
|
|
|
+ if (*jtype == "processor")
|
|
|
+ {
|
|
|
+ if (IProcessor* processor_ptr = dynamic_cast<IProcessor*>(&(*module_ptr)))
|
|
|
+ {
|
|
|
+ ret["info"] = processor_ptr->dump();
|
|
|
}
|
|
|
}
|
|
|
+ else if (*jtype == "module")
|
|
|
+ {
|
|
|
+ ret["info"] = module_ptr->dump();
|
|
|
+ }
|
|
|
+ else if (*jtype == "input")
|
|
|
+ {
|
|
|
+ ret["info"] = module_ptr->getInput(args)->dump();
|
|
|
+ }
|
|
|
+ else if (*jtype == "output")
|
|
|
+ {
|
|
|
+ ret["info"] = module_ptr->getOutput(args)->dump();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
if (!found)
|
|
|
{
|
|
|
- j["error"] = "Reached end of state parser!";
|
|
|
+ ret["error"] = "Reached end of state parser!";
|
|
|
}
|
|
|
//std::cout << "[Connector]: state: " << j.dump() << std::endl;
|
|
|
- json ret;
|
|
|
- ret["operation"] = "state";
|
|
|
- ret["args"] = j;
|
|
|
- return ret;
|
|
|
+ json jret;
|
|
|
+ jret["operation"] = "state";
|
|
|
+ jret["args"] = ret;
|
|
|
+ return jret;
|
|
|
}
|
|
|
|
|
|
json Connector::save(const json& args) {
|