فهرست منبع

start with property manager

Willi Zschiebsch 4 سال پیش
والد
کامیت
6282f388f1

+ 0 - 2
lib/include/Connector.h

@@ -30,8 +30,6 @@ namespace mdd {
 			{"state", std::bind(&Connector::state,this,std::placeholders::_1)}
         };
 
-		bool contains_subj_obj_structure(const json& args);
-
 		/**
 		*subject
 		*object

+ 2 - 2
lib/include/IModule.h

@@ -27,9 +27,9 @@ namespace mdd {
         //MAth1;Value1
         //MAth1->get(Value1)
         //Value1
-        virtual std::shared_ptr<IInput> getInput(const std::string& id) = 0;
+        virtual std::shared_ptr<IInput> getInput(const json& jid) = 0;
         virtual std::shared_ptr<IInput> getInput(size_t index) = 0;
-        virtual std::shared_ptr<IOutput> getOutput(const std::string& id) = 0;
+        virtual std::shared_ptr<IOutput> getOutput(const json& jid) = 0;
         virtual std::shared_ptr<IOutput> getOutput(size_t index) = 0;
 
         virtual std::vector<std::shared_ptr<IInput>> getOptimizableInputs() = 0;

+ 1 - 1
lib/include/IProcessor.h

@@ -20,7 +20,7 @@ namespace mdd
  
         virtual size_t getNumModuls() = 0;
         virtual std::shared_ptr<IModule> getModule(size_t index) = 0;
-        virtual std::shared_ptr<IModule> getModule(const std::string& id) = 0;
+        virtual std::shared_ptr<IModule> getModule(const json& jid) = 0;
         virtual bool connect(const std::string& output_id, const std::vector<std::string>& input_ids) = 0;
     };
 }

+ 2 - 2
lib/include/ModuleBase.h

@@ -31,9 +31,9 @@ namespace mdd {
         size_t getNumOutputs() override;
 
         std::shared_ptr<IInput> getInput(size_t index) override;
-        std::shared_ptr<IInput> getInput(const std::string& id) override;
+        std::shared_ptr<IInput> getInput(const json& jid) override;
         std::shared_ptr<IOutput> getOutput(size_t index) override;
-        std::shared_ptr<IOutput> getOutput(const std::string& id) override;
+        std::shared_ptr<IOutput> getOutput(const json& jid) override;
 
         std::vector<std::shared_ptr<IInput>> getOptimizableInputs() override;
         std::vector<std::shared_ptr<IOutput>> getOptimizableOutputs() override;

+ 5 - 3
lib/include/ProcessorBase.h

@@ -54,9 +54,9 @@ namespace mdd {
 
         size_t getNumInputs() override;
         size_t getNumOutputs() override;
-        std::shared_ptr<IInput> getInput(const std::string& id) override;
+        std::shared_ptr<IInput> getInput(const json& jid) override;
         std::shared_ptr<IInput> getInput(size_t index) override;
-        std::shared_ptr<IOutput> getOutput(const std::string& id) override;
+        std::shared_ptr<IOutput> getOutput(const json& jid) override;
         std::shared_ptr<IOutput> getOutput(size_t index) override;
 
         std::vector<std::shared_ptr<IInput>> getOptimizableInputs() override;
@@ -65,13 +65,15 @@ namespace mdd {
         std::vector<std::shared_ptr<IModule >>& getModules();
         size_t getNumModuls() override;
         std::shared_ptr<IModule> getModule(size_t index) override;
-        std::shared_ptr<IModule> getModule(const std::string& id) override;
+        std::shared_ptr<IModule> getModule(const json& jid) override;
 
         bool connect(const std::string& output_id, const std::vector<std::string>& input_ids);
         void disconnect() override;
         void load(const json& j) override;
         json dump() override;
         json getIdentifier() override;
+
+        state update() override { return state::UNCHANGED; };
     };
 }
 //*/

+ 94 - 82
lib/src/Connector.cpp

@@ -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) {

+ 11 - 1
lib/src/Input.cpp

@@ -167,7 +167,7 @@ namespace mdd{
         jit = jconfig.find("optimizable");
         if (jit != jconfig.end())
         {
-            _optimizable = jit.value().get<bool>();
+            setOptimizability(jit.value().get<bool>());
             success = true;
         }
 
@@ -190,6 +190,16 @@ namespace mdd{
         _base_config["optimizable"] = _optimizable;
         _base_config["type"] = "input";
         _base_config["key"] = "Input";
+        if (_optimizable == true && _limit != nullptr)
+        {
+            json jlimit;
+            jlimit["min"] = _limit->min;
+            jlimit["max"] = _limit->max;
+            jlimit["step"] = _limit->step;
+            jlimit["rule"] = _limit->rule;
+            jlimit["elements"] = _limit->elements;
+            _base_config["limit"] = jlimit;
+        }
         return _base_config;
     }
 

+ 6 - 4
lib/src/ModuleBase.cpp

@@ -20,11 +20,12 @@ namespace mdd {
         return nullptr;
     }
 
-    std::shared_ptr<IInput> ModuleBase::getInput(const std::string& input_id){
+    std::shared_ptr<IInput> ModuleBase::getInput(const json& jid) {
+        std::string sid = jid["name"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
         for(auto& input : inputs){
             if (auto in_ptr = input)
             {
-                if (in_ptr->getID() == input_id) {
+                if (in_ptr->getID() == sid) {
                     return in_ptr;
                 }
             }
@@ -40,11 +41,12 @@ namespace mdd {
         return nullptr;
     }
 
-    std::shared_ptr<IOutput> ModuleBase::getOutput(const std::string& output_id) {
+    std::shared_ptr<IOutput> ModuleBase::getOutput(const json& jid) {
+        std::string sid = jid["name"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
         for (auto& output : outputs) {
             if (auto out_ptr = output)
             {
-                if (out_ptr->getID() == output_id) {
+                if (out_ptr->getID() == sid) {
                     return out_ptr;
                 }
             }

+ 66 - 18
lib/src/ProcessorBase.cpp

@@ -338,7 +338,7 @@ namespace mdd{
             for(json jcon : j["connections"])
             {
                 json jout = jcon["output"];
-                IModule::Ptr module_out_ptr = getModule(jout["prefix"].back().get<std::string>());
+                IModule::Ptr module_out_ptr = getModule(jout);
                 IOutput::Ptr out_ptr = nullptr;
                 std::string id = jout["name"].get<std::string>() + std::to_string(jout["appendix"].get<int>());
                 for (size_t i = 0; i < module_out_ptr->getNumOutputs(); i++)
@@ -352,7 +352,7 @@ namespace mdd{
                 }
                 for (json jin : jcon["inputs"])
                 {
-                    IModule::Ptr module_in_ptr = getModule(jin["prefix"].back().get<std::string>());
+                    IModule::Ptr module_in_ptr = getModule(jin);
                     IInput::Ptr in_ptr = nullptr;
                     std::string id = jin["name"].get<std::string>() + std::to_string(jin["appendix"].get<int>());
                     for (size_t i = 0; i < module_in_ptr->getNumInputs(); i++)
@@ -533,14 +533,15 @@ namespace mdd{
         return ret;
     }
    
-    std::shared_ptr<IOutput> ProcessorBase::getOutput(const std::string& id){
+    std::shared_ptr<IOutput> ProcessorBase::getOutput(const json& jid){
+        std::string sid = jid["name"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
         for (auto& output : processor_outputs) {
-            if(output->getID() == id){
+            if(output->getID() == sid){
                 return output;
             }
         }
         for (auto& output : outputs) {
-            if(output->getOutput(0)->getID() == id){
+            if(output->getOutput(0)->getID() == sid){
                 return output->getOutput(0);
             }
         }
@@ -560,27 +561,74 @@ namespace mdd{
         return nullptr;
     }
 
-    std::shared_ptr<IInput> ProcessorBase::getInput(const std::string& id){
-        for (auto& input : processor_inputs) {
-            if(input->getID() == id){
-                return input;
+    std::shared_ptr<IInput> ProcessorBase::getInput(const json& jid){
+        auto jid_pref = jid.find("prefix");
+        if (jid_pref != jid.end())
+        {
+            auto mod = getModule(jid);
+            if (mod == shared_from_this())
+            {
+                std::string sid = jid["name"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
+                for (auto& input : processor_inputs) {
+                    if (input->getID() == sid) {
+                        return input;
+                    }
+                }
+                for (auto& input : inputs) {
+                    if (input->getInput(0)->getID() == sid) {
+                        return input->getInput(0);
+                    }
+                }
             }
-        }
-        for (auto& input : inputs) {
-            if (input->getInput(0)->getID() == id) {
-                return input->getInput(0);
+            else
+            {
+                return mod->getInput(jid);
             }
         }
         return nullptr;
     }
 
-    std::shared_ptr<IModule> ProcessorBase::getModule(const std::string& id){
-        for (auto& module : modules) {
-            if(module->getID() == id){
-                return module;
+    std::shared_ptr<IModule> ProcessorBase::getModule(const json& jid){
+        json jid_copy = jid;
+        auto jid_typ = jid.find("type");
+        auto jid_copy_pref = jid_copy.find("prefix");
+        jid_copy_pref->erase(jid_copy_pref->begin());
+        int final_size = 0;
+        if (jid_typ != jid.end())
+        {
+            std::string sid;
+            if (*jid_typ == "input" || *jid_typ == "output")
+            {
+                final_size = 1;
+                sid = jid_copy_pref->at(0).get<std::string>();
+            }
+            else
+            {
+                sid = jid_copy["name"].get<std::string>() + std::to_string(jid_copy["appendix"].get<int>());
+            }
+
+            IModule::Ptr module_ptr;
+            for (auto& module : modules) {
+                if (module->getID() == sid) {
+                    module_ptr = module;
+                    break;
+                }
+            }
+            if (jid_copy_pref->size() > final_size)
+            {
+                if (ProcessorBase* proc_ptr = dynamic_cast<ProcessorBase*>(&(*module_ptr)))
+                {
+                    return proc_ptr->getModule(jid_copy);
+                }
+            }
+            else
+            {
+                return module_ptr;
             }
         }
-        return nullptr;
+        
+        return shared_from_this();
+        //return nullptr;
     }