Browse Source

working on more processors

Willi Zschiebsch 4 years ago
parent
commit
8c56031125

+ 2 - 0
lib/include/IInteractive.h

@@ -17,5 +17,7 @@ namespace mdd {
     {
     public:
         typedef std::shared_ptr<IInteractive> Ptr;
+        virtual std::string setName(const std::string& name) = 0;
+        virtual std::string getName() = 0;
     };
 }

+ 0 - 4
lib/include/IUnique.h

@@ -8,10 +8,6 @@ namespace mdd{
     public:
         virtual std::string getType() = 0;
         virtual std::string getGeneratorKey() = 0;
-
-        virtual std::string setName(const std::string& name) = 0;
-        virtual std::string getName() = 0;
-        
         virtual std::string setAppendix(int appendix) = 0;
         virtual int getAppendix() = 0;
 

+ 1 - 4
lib/include/ModuleBase.h

@@ -9,14 +9,13 @@ namespace mdd {
 
     class ModuleBase : public  IModule{
     private:
-        std::string _name = "";
         int _appendix = 0;
 
     protected:
         std::weak_ptr<IModule> _parent;
         json _base_config;
         ModuleBase(const std::string& base_config = "{}"); 
-        std::string type = "module";
+        const std::string type = "module";
         std::string key;
         std::vector<std::shared_ptr<Input>> inputs;
         std::vector<std::shared_ptr<Output>> outputs;
@@ -43,8 +42,6 @@ namespace mdd {
         
         std::string getGeneratorKey() override;
         std::string getType() override;
-        std::string setName(const std::string& name) override;
-        std::string getName() override;
         std::string setAppendix(int appendix) override;
         int getAppendix() override;
         std::string getID() override;

+ 1 - 1
lib/include/Parameter.h

@@ -9,7 +9,7 @@ namespace mdd {
     public:
         Parameter();
         state update() override;
-        std::string setName(const std::string& type) override;
+        std::string setName(const std::string& type);
         std::string setAppendix(int appendix) override;
     };
 }

+ 0 - 3
lib/include/ProcessorBase.h

@@ -13,7 +13,6 @@ namespace mdd {
         {
     private:
         std::weak_ptr<IModule> _parent;
-        std::string _name = "";
         int _appendix = 0;
 
         unsigned int module_counter = 0;
@@ -40,8 +39,6 @@ namespace mdd {
 
         std::string getGeneratorKey() override;
         std::string getType() override;
-        std::string setName(const std::string& name) override;
-        std::string getName() override;
         std::string setAppendix(int appendix) override;
         int getAppendix() override;
         std::string getID() override;

+ 21 - 15
lib/src/Connector.cpp

@@ -16,13 +16,17 @@ namespace mdd {
 		}
 		ret["operation"] = "add";
 		json jargs;
+		ProcessorBase* proc_ptr = nullptr;
 		if (*jsub_typ == "processor")
 		{
-			jargs["subject"] = _root->getIdentifier();
-		}
-		else {
-			json j;
-			return j;
+			if (proc_ptr = dynamic_cast<ProcessorBase*>(&(*_root->getModule(*jsub))))
+			{
+				jargs["subject"] = proc_ptr->getIdentifier();
+			}
+			else {
+				json j;
+				return j;
+			}
 		}
 
 		auto jobj_typ = jobj->find("type");
@@ -34,12 +38,13 @@ namespace mdd {
 
 		if (*jobj_typ == "processor")
 		{
-
+			proc_ptr->addModule(Registration().generateProcessor((*jobj)["key"].get<std::string>()));
+			jargs["object"] = proc_ptr->getModules().back()->dump();
 		}
 		else if (*jobj_typ == "module")
 		{
-			_root->addModule(Registration().generateModule((*jobj)["key"].get<std::string>()));
-			jargs["object"] = _root->getModules().back()->dump();
+			proc_ptr->addModule(Registration().generateModule((*jobj)["key"].get<std::string>()));
+			jargs["object"] = proc_ptr->getModules().back()->dump();
 		}
 		else if(*jobj_typ == "connection")
 		{
@@ -103,12 +108,12 @@ namespace mdd {
 		json jargs;
 		if (*jsub_typ == "processor")
 		{
-			if (*jsub == _root->getIdentifier())
+			if (ProcessorBase* proc_ptr = dynamic_cast<ProcessorBase*>(&(*_root->getModule(*jsub))))
 			{
-				jargs["subject"] = _root->getIdentifier();
-				auto module_ptr = _root->getModule(*jsub);
+				auto module_ptr = proc_ptr->getModule(*jobj);
+				jargs["subject"] = proc_ptr->getIdentifier();
 				jargs["object"] = module_ptr->getIdentifier();
-				_root->removeModule(module_ptr);
+				proc_ptr->removeModule(module_ptr);
 			}
 		}
 		ret["args"] = jargs;
@@ -251,7 +256,7 @@ namespace mdd {
 				{
 					json jprocessor;
 					jprocessor["key"] = processors[i];
-					jprocessor["type"] = "module";
+					jprocessor["type"] = "processor";
 					ret["processor"].push_back(jprocessor);
 				}
 			}
@@ -347,8 +352,9 @@ namespace mdd {
 		else
 		{
 			_root = regi.generateProcessor("ProcessorStandard");
-			_root->setName("root");
-
+			json jconfig = _root->getConfiguration();
+			jconfig["GUI"]["name"]["value"]	= "root";
+			_root->configure(jconfig);
 			_opt = regi.generateOptimizer("OptimizerEvolutionary");
 		}
 		_opt->connect(_root);

+ 14 - 6
lib/src/Input.cpp

@@ -48,7 +48,7 @@ namespace mdd{
     }
 
     std::string Input::getID() {
-        return _name + std::to_string(_appendix);
+        return key + std::to_string(_appendix);
     }
 
     void Input::setParent(std::shared_ptr<IModule> parent) {
@@ -193,11 +193,19 @@ namespace mdd{
         if (jit != jconfig.end())
         {
             json jval = jit.value();
-            if (jval.is_string()) {
-                jval = json::parse(jval.get<std::string>());
+            try
+            {
+                if (jval.is_string()) {
+                    jval = json::parse(jval.get<std::string>());
+                }
+                _value = jval.get<std::vector<double>>();
+                success = true;
             }
-            _value = jval.get<std::vector<double>>();
-            success = true;
+            catch (const nlohmann::detail::parse_error&)
+            {
+                success = false;
+            }
+            
         }
 
         auto jit_limit = jparse.find("limit");
@@ -296,7 +304,7 @@ namespace mdd{
 
     json Input::getIdentifier() {
         json jID;
-        jID["name"] = _name;
+        jID["key"] = key;
         jID["appendix"] = _appendix;
         jID["type"] = "input";
         jID["prefix"] = getParentID();

+ 4 - 18
lib/src/ModuleBase.cpp

@@ -21,7 +21,7 @@ namespace mdd {
     }
 
     std::shared_ptr<IInput> ModuleBase::getInput(const json& jid) {
-        std::string sid = jid["name"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
+        std::string sid = jid["key"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
         for(auto& input : inputs){
             if (auto in_ptr = input)
             {
@@ -42,7 +42,7 @@ namespace mdd {
     }
 
     std::shared_ptr<IOutput> ModuleBase::getOutput(const json& jid) {
-        std::string sid = jid["name"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
+        std::string sid = jid["key"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
         for (auto& output : outputs) {
             if (auto out_ptr = output)
             {
@@ -118,11 +118,6 @@ namespace mdd {
 
     const json& ModuleBase::getConfiguration()
     {
-        auto jit = _base_config.find("GUI");
-        if (jit == _base_config.end())
-        {
-            _base_config["GUI"]["name"] = _name;
-        }
         return _base_config;
     }
 
@@ -147,15 +142,6 @@ namespace mdd {
         return  key;
     }
 
-    std::string ModuleBase::setName(const std::string& name) {
-        _name = name;
-        return getID();
-    }
-
-    std::string ModuleBase::getName() {
-        return  _name;
-    }
-
     std::string ModuleBase::setAppendix(int appendix) {
         _appendix = appendix;
         return  getID();
@@ -187,7 +173,7 @@ namespace mdd {
             for (size_t i = 0; i < jin.size(); i++)
             {
                 inputs[i]->setParent(shared_from_this());
-                inputs[i]->setValue()=jin[i]["value"].get<std::vector<double>>();
+                inputs[i]->configure(jin[i]);
             }
         }
 
@@ -204,7 +190,7 @@ namespace mdd {
             for (size_t i = 0; i < jout.size(); i++)
             {
                 outputs[i]->setParent(shared_from_this());
-                outputs[i]->setValue() = jout[i]["value"].get<std::vector<double>>();
+                inputs[i]->configure(jout[i]);
             }
         }
     }

+ 10 - 1
lib/src/ModuleHTTP.cpp

@@ -137,6 +137,7 @@ namespace mdd{
     ModuleHTTP::ModuleHTTP()// std::string fname, std::string id, int port):
         : ModuleBase(R"JSON(
         {
+        "configure":{
             "file":{
                 "value": ""
             },
@@ -146,11 +147,19 @@ namespace mdd{
             "port":{
                 "value": 0
             }
+        },
+        "GUI":{
+            "name":{
+                "value": "#HTTP"
+            },
+            "color":{
+                "value": "#0257ad"
+            }
+        }
         })JSON")
         ,_port(0)
     {
         key = "ModuleHTTP";
-        setName("HTTP");
     }
 
     bool ModuleHTTP::configureChild(const json& config) {

+ 6 - 2
lib/src/ModuleMath.cpp

@@ -32,7 +32,7 @@ namespace mdd {
 
     ModuleMath::ModuleMath()
         : ModuleBase(R"JSON(
-        {
+        {"configure":{
             "operation":{
             "value":"add",
             "options":  [
@@ -48,6 +48,11 @@ namespace mdd {
                             "greater"
                         ]
             }
+        },
+        "GUI":{
+            "name":{"value":"Math"},
+            "color":{"value":"#b91203"}
+        }
         })JSON")
     {
         _ops.at("add");
@@ -57,7 +62,6 @@ namespace mdd {
         inputs.push_back(std::make_shared<Input>("Value", 1));
         outputs.push_back(std::make_shared<Output>("Value", 0));
         key = "ModuleMath";
-        setName("Math");
     }
 
     bool ModuleMath::configureChild(const json& config)

+ 6 - 1
lib/src/ModuleMerge.cpp

@@ -16,16 +16,21 @@ namespace mdd {
 	ModuleMerge::ModuleMerge()
 		:ModuleBase(R"JSON(
         {
+		"configure":{
             "inputs":{
 				"value":2
 			}
+		},
+        "GUI":{
+            "name":{"value":"Merge"},
+            "color":{"value":"#940297"}
+        }
         })JSON")
 	{
 		inputs.push_back(std::make_shared<Input>("Value", 0));
 		inputs.push_back(std::make_shared<Input>("Value", 1));
 		outputs.push_back(std::make_shared<Output>("Value", 0));
 		key = "ModuleMerge";
-		setName("Merge");
 	}
 
 	int ModuleMerge::addModuleInput() {

+ 6 - 1
lib/src/ModuleSQL.cpp

@@ -41,13 +41,18 @@ namespace mdd {
 	ModuleSQL::ModuleSQL()
 	: ModuleBase(R"JSON(
         {
+		"configure":{
             "database":{
 				"value":""
 			}
+		},
+        "GUI":{
+            "name":{"value":"SQL"},
+            "color":{"value":"#01a8ad"}
+        }
         })JSON")
 	{
 		key = "ModuleSQL";
-		setName("SQL");
 	}
 
 	state ModuleSQL::update() {

+ 6 - 1
lib/src/ModuleSwitch.cpp

@@ -12,16 +12,21 @@ namespace mdd{
     ModuleSwitch::ModuleSwitch()
         :ModuleBase(R"JSON(
         {
+        "configure":{
             "size":{
                 "value": 1
             }
+        },
+        "GUI":{
+            "name":{"value":"Switch"},
+            "color":{"value":"#fd8d18"}
+        }
         })JSON")
     {    
         inputs.push_back(std::make_shared<Input>( "Switch", 0));
         inputs.push_back(std::make_shared<Input>( "Default", 0));
         outputs.push_back(std::make_shared<Output>("Value", 0));
         key = "ModuleSwitch";
-        setName("Switch");
     }
     bool ModuleSwitch::configureChild(const json& config) {
         auto jit = config.find("size");

+ 2 - 2
lib/src/Parameter.cpp

@@ -20,9 +20,9 @@ namespace mdd {
     }
 
     std::string Parameter::setName(const std::string& name) {
-        inputs[0]->setName(name);
+        //inputs[0]->setName(name);
         outputs[0]->setName(name);
-        return ModuleBase::setName(name);
+        return name;
     }
 
     std::string Parameter::setAppendix(int appendix)

+ 20 - 23
lib/src/ProcessorBase.cpp

@@ -6,7 +6,7 @@ namespace mdd{
         json jparse = json::parse(base_config);
         if (jparse.contains("configure"))
         {
-            _base_config = base_config;
+            _base_config = jparse;
         }
         else {
             _base_config["configure"] = jparse;
@@ -68,7 +68,6 @@ namespace mdd{
                 if (auto module_ptr = modules.back())
                 {
                     module_ptr->setParent(shared_from_this());
-                    module_ptr->setName(jmodule["type"].get<std::string>());
                     module_ptr->setAppendix(jmodule["appendix"].get<int>());
                     module_ptr->load(jmodule["load"]);
                 }
@@ -99,11 +98,6 @@ namespace mdd{
 
     const json& ProcessorBase::getConfiguration() {
         //update module apendix ? 
-        auto jit = _base_config.find("GUI");
-        if (jit == _base_config.end())
-        {
-            _base_config["GUI"]["name"] = _name;
-        }
         return _base_config;
     }
 
@@ -129,15 +123,6 @@ namespace mdd{
         return  key;
     }
 
-    std::string ProcessorBase::setName(const std::string& name) {
-        _name = name;
-        return getID();
-    }
-
-    std::string ProcessorBase::getName() {
-        return  _name;
-    }
-
     std::string ProcessorBase::setAppendix(int appendix) {
         _appendix = appendix;
         return  getID();
@@ -282,7 +267,6 @@ namespace mdd{
         if (j.contains("ID"))
         {
             setAppendix(j["ID"]["appendix"].get<int>());
-            setName(j["ID"]["name"].get<std::string>());
             j["ID"]["type"];
 
         }
@@ -609,6 +593,13 @@ namespace mdd{
         json jid_copy = jid;
         auto jid_typ = jid.find("type");
         auto jid_copy_pref = jid_copy.find("prefix");
+        //check if called non recursive
+        json own_prefix = getIdentifier()["prefix"];
+        if (jid_copy_pref->dump() == own_prefix.dump())
+        {
+            jid_copy_pref->clear();
+        }
+        //recursive call
         if (!jid_copy_pref->empty())
         {
             jid_copy_pref->erase(jid_copy_pref->begin());
@@ -617,14 +608,20 @@ namespace mdd{
         if (jid_typ != jid.end())
         {
             std::string sid;
-            if (*jid_typ == "input" || *jid_typ == "output")
+            if (jid_copy_pref->empty())
             {
-                final_size = 1;
-                sid = jid_copy_pref->at(0).get<std::string>();
+                if (*jid_typ == "input" || *jid_typ == "output")
+                {
+                    final_size = 1;
+                    sid = jid_copy_pref->at(0).get<std::string>();
+                }
+                else
+                {
+                    sid = jid_copy["key"].get<std::string>() + std::to_string(jid_copy["appendix"].get<int>());
+                }
             }
-            else
-            {
-                sid = jid_copy["key"].get<std::string>() + std::to_string(jid_copy["appendix"].get<int>());
+            else {
+                sid = jid_copy_pref->begin()->get<std::string>();
             }
 
             IModule::Ptr module_ptr;

+ 6 - 1
lib/src/ProcessorStandard.cpp

@@ -4,6 +4,7 @@ namespace mdd {
     ProcessorStandard::ProcessorStandard()
         :ProcessorBase(R"JSON(
         {
+        "configure":{
             "priority":
             {
             "value":"manual",
@@ -18,6 +19,11 @@ namespace mdd {
             {
                 "value": -1
             }
+        },
+        "GUI":{
+            "name":{"value":"Processor"},
+            "color":{"value":"#006838"}
+        }
         }
         )JSON")
     {
@@ -25,7 +31,6 @@ namespace mdd {
         _maxIterations = -1;
         
         key = "ProcessorStandard";
-        setName(key);
         processor_outputs.push_back(std::make_shared<Output>("Iterator", 0, std::vector<double>{0}));
     }