Willi Zschiebsch 4 rokov pred
rodič
commit
3bef9513b6

+ 0 - 1
lib/include/IInteractive.h

@@ -17,6 +17,5 @@ namespace mdd {
     {
     public:
         typedef std::shared_ptr<IInteractive> Ptr;
-        virtual std::string getParentID() = 0;
     };
 }

+ 2 - 2
lib/include/IManager.h

@@ -3,8 +3,8 @@
 namespace mdd {
 	class IManager {
 	public:
-		virtual const std::string& getConfiguration() = 0;
-		virtual bool configure(const std::string& config) = 0;
+		virtual const json& getConfiguration() = 0;
+		virtual bool configure(const json& config) = 0;
 		virtual void load(const json& j) = 0;
 		virtual json dump() = 0;
 		virtual json getIdentifier() = 0;

+ 2 - 1
lib/include/IModule.h

@@ -10,9 +10,10 @@
 #include <memory>
 
 namespace mdd {
-    class IModule 
+    class IModule
         : public IUnique
         , public IManager
+        , public std::enable_shared_from_this<IModule>
     {
     public:
         typedef std::shared_ptr<IModule> Ptr;

+ 4 - 0
lib/include/IUnique.h

@@ -3,6 +3,7 @@
 #include <string>
 
 namespace mdd{
+    class IModule;
     class IUnique{
     public:
         virtual std::string getType() = 0;
@@ -15,6 +16,9 @@ namespace mdd{
         virtual int getAppendix() = 0;
 
         virtual std::string getID() = 0;
+
+        virtual void setParent(std::shared_ptr<IModule> parent) = 0;
+        virtual std::vector<std::string> getParentID() = 0;
     };
 }
 #endif

+ 11 - 7
lib/include/Input.h

@@ -9,15 +9,18 @@
 #include "IUnique.h"
 
 namespace mdd {
-    class Input : public IInput{
+    class Input 
+        : public IInput
+        , public std::enable_shared_from_this<Input>
+    {
     private:
         std::string _name;
         int _appendix;
-        IModule* _parent;
+        std::weak_ptr<IModule> _parent;
 
         std::vector<double> _value;
         //std::function<bool(const json &)> _verification;
-        std::shared_ptr <IOutput> _output;
+        std::shared_ptr<IOutput> _output;
         bool _optimizable;
         std::shared_ptr<limits> _limit;
 
@@ -28,7 +31,7 @@ namespace mdd {
         bool configureChild(const json& config) override { return true; };
 
     public:
-        Input(IModule* parent, const std::string &name, int appendix, const std::vector<double>& default_value);
+        Input(const std::string& name, int appendix, const std::vector<double>& default_value = std::vector<double>({ 1 }));
         //const std::function<bool(const json &)> &verification = [](const json&) { return true; }
         std::string getType() override;
         std::string getGeneratorKey() override;
@@ -38,7 +41,8 @@ namespace mdd {
         int getAppendix() override;
         std::string getID() override;
 
-        std::string getParentID() override;
+        void setParent(std::shared_ptr<IModule> parent) override;
+        std::vector<std::string> getParentID() override;
 
         state getState() override;
         void resetState() override;
@@ -60,8 +64,8 @@ namespace mdd {
         bool connect(std::shared_ptr<IOutput> output) override;
         void disconnect() override;
 
-        bool configure(const std::string& config) override;
-        const std::string& getConfiguration() override;
+        bool configure(const json& config) override;
+        const json& getConfiguration() override;
         void load(const json& j) override;
         json dump() override;
         json getIdentifier() override;

+ 6 - 2
lib/include/ModuleBase.h

@@ -13,6 +13,7 @@ namespace mdd {
         int _appendix = 0;
 
     protected:
+        std::weak_ptr<IModule> _parent;
         json _base_config;
         ModuleBase(const std::string& base_config = "{}"); 
         std::string type = "module";
@@ -23,8 +24,8 @@ namespace mdd {
         bool configureChild(const json& config) override { return true; };
 
     public:
-        bool configure(const std::string& config) override;
-        const std::string& getConfiguration() override;
+        bool configure(const json& config) override;
+        const json& getConfiguration() override;
 
         size_t getNumInputs() override;
         size_t getNumOutputs() override;
@@ -36,6 +37,9 @@ namespace mdd {
 
         std::vector<std::shared_ptr<IInput>> getOptimizableInputs() override;
         std::vector<std::shared_ptr<IOutput>> getOptimizableOutputs() override;
+
+        void setParent(std::shared_ptr<IModule> parent) override;
+        std::vector<std::string> getParentID() override;
         
         std::string getGeneratorKey() override;
         std::string getType() override;

+ 2 - 2
lib/include/OptimizerBase.h

@@ -33,8 +33,8 @@ namespace mdd {
 		void updateLayout();
 		bool setEvaluation(std::string func) override;
 
-		bool configure(const std::string& config) override;
-		const std::string& getConfiguration() override;
+		bool configure(const json& config) override;
+		const json& getConfiguration() override;
 		//virtual std::string getGeneratorID() = 0;
 		void load(const json& j);
 		json dump();

+ 11 - 7
lib/include/Output.h

@@ -4,15 +4,18 @@
 #include "IModule.h"
 
 namespace mdd {
-    class Output : public IOutput{
+    class Output 
+        : public IOutput
+        , public std::enable_shared_from_this<Output>
+    {
     private:
         state _state;
         std::vector<double> _value;
         std::string _name;
         int _appendix;
-        std::vector<std::shared_ptr<IInput>> _connections;
+        std::vector<std::weak_ptr<IInput>> _connections;
         bool _optimizable;
-        IModule* _parent;
+        std::weak_ptr<IModule> _parent;
 
     protected:
         json _base_config;
@@ -21,7 +24,7 @@ namespace mdd {
         bool configureChild(const json& config) override { return true; };
 
     public:
-        Output(IModule* parent, const std::string& name, int appendix, const std::vector<double>& initial);
+        Output(const std::string& name, int appendix, const std::vector<double>& initial = std::vector<double>({ 1 }));
         const std::vector<double>& getValue() override;
         std::vector<double>& setValue() override;
         state setValue(const std::vector<double>& val) override;
@@ -36,15 +39,16 @@ namespace mdd {
         std::string setAppendix(int appendix) override;
         int getAppendix() override;
         std::string getID() override;
-        std::string getParentID() override;
+        void setParent(IModule::Ptr parent) override;
+        std::vector<std::string> getParentID() override;
         int addConnection(std::shared_ptr<IInput> input) override;
         int removeConnection(std::shared_ptr<IInput> input) override;
         bool connect(std::shared_ptr<IInput> input) override;
         std::vector<std::shared_ptr<IInput>> getConnections() override;
         void disconnect() override;
 
-        bool configure(const std::string& config) override;
-        const std::string& getConfiguration() override;
+        bool configure(const json& config) override;
+        const json& getConfiguration() override;
         void load(const json& j) override;
         json dump() override;
         json getIdentifier() override;

+ 1 - 4
lib/include/Parameter.h

@@ -3,14 +3,11 @@
 
 namespace mdd {
     class Parameter : public ModuleBase {
-    private:
-        IModule* _parent;
-    
     protected:
         bool configureChild(const json& config) override;
 
     public:
-        Parameter(IModule* parent);
+        Parameter();
         state update() override;
         std::string setName(const std::string& type) override;
         std::string setAppendix(int appendix) override;

+ 9 - 4
lib/include/ProcessorBase.h

@@ -8,9 +8,11 @@
 
 namespace mdd {
     class Registration;
-    class ProcessorBase : public IProcessor{
+    class ProcessorBase 
+        : public IProcessor
+        {
     private:
-
+        std::weak_ptr<IModule> _parent;
         std::string _name = "";
         int _appendix = 0;
 
@@ -28,8 +30,11 @@ namespace mdd {
         bool configureChild(const json& config) override;
 
     public:
-        bool configure(const std::string& config) override;
-        const std::string& getConfiguration() override;
+        bool configure(const json& config) override;
+        const json& getConfiguration() override;
+
+        void setParent(std::shared_ptr<IModule> parent) override;
+        std::vector<std::string> getParentID() override;
 
         std::string getGeneratorKey() override;
         std::string getType() override;

+ 0 - 2
lib/include/ProcessorStandard.h

@@ -30,8 +30,6 @@ namespace mdd {
 
     public:
         ProcessorStandard();
-        std::string addModule(std::shared_ptr<IModule> module) override;
-        void removeModule(std::shared_ptr<IModule> module) override;
         std::vector<std::shared_ptr<IModule>> getModulePriority();
         state update() override;
         std::shared_ptr<IOutput> getIteration();

+ 5 - 15
lib/src/Connector.cpp

@@ -147,7 +147,7 @@ namespace mdd {
 					return ret;
 				}
 				jargs["subject"] = module_ptr->getIdentifier();
-				module_ptr->configure(obj.dump());
+				module_ptr->configure(obj);
 				if (obj.contains("configure"))
 				{
 					jargs["object"]["configure"] = module_ptr->dump();
@@ -166,7 +166,7 @@ namespace mdd {
 				return ret;
 			}
 			jargs["subject"] = module_ptr->getIdentifier();
-			module_ptr->configure(obj.dump());
+			module_ptr->configure(obj);
 			if (obj.contains("configure"))
 			{
 				jargs["object"]["configure"] = module_ptr->dump();
@@ -187,19 +187,9 @@ namespace mdd {
 				std::cout << "[Connector]: change: Was not able to find input." << std::endl;
 				return ret;
 			}
-			if (obj.contains("value"))
-			{
-				json jval = json::parse(obj["value"].get<std::string>());
-				if (!jval.is_array())
-				{
-					return ret;
-				}
-				jval.get<std::vector<double>>();
-				std::vector<double> new_val = jval.get<std::vector<double>>();
-				in_ptr->setValue(new_val);
-				jargs["subject"] = in_ptr->getIdentifier();
-				jargs["object"] = obj;
-			}
+			in_ptr->configure(obj);
+			jargs["subject"] = in_ptr->getIdentifier();
+			jargs["object"] = obj;
 		}
 		else if (sub["type"] == "output")
 		{

+ 27 - 14
lib/src/Input.cpp

@@ -11,8 +11,7 @@ namespace mdd{
         _output = nullptr;
         return 0;
     }
-    Input::Input(IModule* parent, const std::string& name, int appendix, const std::vector<double>& default_value) {
-        _parent = parent;
+    Input::Input(const std::string& name, int appendix, const std::vector<double>& default_value) {
         _name = name;
         _value = default_value;
         _appendix = appendix;
@@ -52,8 +51,18 @@ namespace mdd{
         return _name + std::to_string(_appendix);
     }
 
-    std::string Input::getParentID() {
-        return _parent->getID();
+    void Input::setParent(std::shared_ptr<IModule> parent) {
+        _parent = parent;
+    }
+
+    std::vector<std::string> Input::getParentID() {
+        std::vector<std::string> ret;
+        if (auto parent_ptr = _parent.lock())
+        {
+            ret = parent_ptr->getParentID();
+            ret.push_back(parent_ptr->getID());
+        }
+        return ret;
     }
 
     state Input::getState() {
@@ -98,14 +107,14 @@ namespace mdd{
         if (_output != nullptr) {
             if (_output->getIdentifier() != output->getIdentifier())
             {
-                _output->removeConnection(std::make_shared<Input>((*this)));
+                _output->removeConnection(shared_from_this());
                 removeConnection();
             }
         }
         if (_output == nullptr)
         {
             addConnection(output);
-            _output->addConnection(std::make_shared<Input>((*this)));
+            _output->addConnection(shared_from_this());
         }
         return true;
     }
@@ -137,9 +146,9 @@ namespace mdd{
         return _limit;
     }
 
-    bool Input::configure(const std::string& config)
+    bool Input::configure(const json& config)
     {
-        json jconfig = json::parse(config);
+        json jconfig = config;
         bool success = false;
         auto jit = jconfig.find("name");
         if (jit != jconfig.end())
@@ -165,30 +174,34 @@ namespace mdd{
         jit = jconfig.find("value");
         if (jit != jconfig.end())
         {
-            _value = jit.value().get<std::vector<double>>();
+            json jval = jit.value();
+            if (jval.is_string()) {
+                jval = json::parse(jval.get<std::string>());
+            }
+            _value = jval.get<std::vector<double>>();
             success = true;
         }
         return success;
     }
 
-    const std::string& Input::getConfiguration()
+    const json& Input::getConfiguration()
     {
         _base_config["value"] = _value;
         _base_config["optimizable"] = _optimizable;
         _base_config["type"] = "input";
         _base_config["key"] = "Input";
-        return _base_config.dump();
+        return _base_config;
     }
 
     void Input::load(const json& j) 
     {
-        configure(j.dump());
+        configure(j);
         return;
     }
 
     json Input::dump() 
     {
-        json jdump = _base_config;
+        json jdump = getConfiguration();
         jdump["ID"] = getIdentifier();
         return jdump;
     }
@@ -198,7 +211,7 @@ namespace mdd{
         jID["name"] = _name;
         jID["appendix"] = _appendix;
         jID["type"] = "input";
-        jID["prefix"].push_back(_parent->getID());
+        jID["prefix"] = getParentID();
         return jID;
     }
 }

+ 53 - 19
lib/src/ModuleBase.cpp

@@ -22,8 +22,11 @@ namespace mdd {
 
     std::shared_ptr<IInput> ModuleBase::getInput(const std::string& input_id){
         for(auto& input : inputs){
-            if (input->getID() == input_id) {
-                return input;
+            if (auto in_ptr = input)
+            {
+                if (in_ptr->getID() == input_id) {
+                    return in_ptr;
+                }
             }
         }
         return nullptr;
@@ -39,8 +42,11 @@ namespace mdd {
 
     std::shared_ptr<IOutput> ModuleBase::getOutput(const std::string& output_id) {
         for (auto& output : outputs) {
-            if (output->getID() == output_id) {
-                return output;
+            if (auto out_ptr = output)
+            {
+                if (out_ptr->getID() == output_id) {
+                    return out_ptr;
+                }
             }
         }
         return nullptr;
@@ -50,9 +56,12 @@ namespace mdd {
         std::vector<std::shared_ptr<IInput>> ret;
         for (size_t i = 0; i < inputs.size(); i++)
         {
-            if (inputs[i]->isOptimizable())
+            if (auto in_ptr = inputs[i])
             {
-                ret.push_back(inputs[i]);
+                if (in_ptr->isOptimizable())
+                {
+                    ret.push_back(in_ptr);
+                }
             }
         }
         
@@ -63,9 +72,12 @@ namespace mdd {
         std::vector<std::shared_ptr<IOutput>> ret;
         for (size_t i = 0; i < outputs.size(); i++)
         {
-            if (outputs[i]->isOptimizable())
+            if (auto out_ptr = outputs[i])
             {
-                ret.push_back(outputs[i]);
+                if (out_ptr->isOptimizable())
+                {
+                    ret.push_back(out_ptr);
+                }
             }
         }
         
@@ -83,8 +95,8 @@ namespace mdd {
             _base_config["configure"] = jparse;
         }
     }
-    bool ModuleBase::configure(const std::string& config) {
-        json jconfig = json::parse(config);
+    bool ModuleBase::configure(const json& config) {
+        json jconfig = config;
         bool res = true;
         auto jit = jconfig.find("GUI");
         if (jit != jconfig.end())
@@ -102,15 +114,28 @@ namespace mdd {
         return true;
     }
 
-    const std::string& ModuleBase::getConfiguration()
+    const json& ModuleBase::getConfiguration()
     {
-        return _base_config.dump();
+        return _base_config;
     }
 
     std::string ModuleBase::getType() {
         return  type;
     }
 
+    void ModuleBase::setParent(std::shared_ptr<IModule> parent) {
+        _parent = parent;
+    }
+
+    std::vector<std::string> ModuleBase::getParentID() {
+        std::vector<std::string> ret;
+        if (auto parent_ptr = _parent.lock()) {
+            ret = parent_ptr->getParentID();
+            ret.push_back(parent_ptr->getID());
+        }
+        return ret;
+    }
+
     std::string ModuleBase::getGeneratorKey() {
         return  key;
     }
@@ -139,7 +164,7 @@ namespace mdd {
 
     void ModuleBase::load(const json& j)
     {
-        configure(j.dump());
+        configure(j);
 
         auto it = j.find("inputs");
         if (it!=j.end())
@@ -149,11 +174,12 @@ namespace mdd {
             std::vector<double> default_val = { 1 };
             for (size_t i = start_counter; i < jin.size(); i++)
             {
-                inputs.push_back(std::make_shared<Input>(this, "Value", 0, default_val));
+                inputs.push_back(std::make_shared<Input>("Value", 0, default_val));
                 inputs.back()->load(jin[i]);
             }
             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>>();
             }
         }
@@ -163,14 +189,14 @@ namespace mdd {
         {
             const size_t start_counter = outputs.size();
             const json& jout = it.value();
-            std::vector<double> default_val = { 1 };
             for (size_t i = start_counter; i < j["outputs"].size(); i++)
             {
-                outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
+                outputs.push_back(std::make_shared<Output>( "Value", 0));
                 outputs.back()->load(jout[i]);
             }
             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>>();
             }
         }
@@ -186,11 +212,13 @@ namespace mdd {
         
         for (auto& in : inputs)
         {
+            in->setParent(shared_from_this());
             _base_config["inputs"].push_back(in->dump());
         }
 
         for (auto& out : outputs)
         {
+            out->setParent(shared_from_this());
             _base_config["outputs"].push_back(out->dump());
         }
 
@@ -200,11 +228,17 @@ namespace mdd {
     void ModuleBase::disconnect() {
         for (size_t i = 0; i < inputs.size(); i++)
         {
-            inputs[i]->disconnect();
+            if (auto in_ptr = inputs[i])
+            {
+                in_ptr->disconnect();
+            }
         }
         for (size_t i = 0; i < outputs.size(); i++)
         {
-            outputs[i]->disconnect();
+            if (auto out_ptr = outputs[i])
+            {
+                out_ptr->disconnect();
+            }
         }
     }
 
@@ -213,7 +247,7 @@ namespace mdd {
         jID["name"] = _name;
         jID["appendix"] = getAppendix();
         jID["type"] = type;
-        jID["prefix"] = std::vector<std::string>(); 
+        jID["prefix"] = getParentID(); 
         return jID;
     }
 }

+ 2 - 2
lib/src/ModuleHTTP.cpp

@@ -97,10 +97,10 @@ namespace mdd{
             size_t length = vec.size();
             if (server[length]["value"].is_array())
             {
-                vec.push_back(std::make_shared<T>(this, server[length]["type"].get<std::string>(), length, server[length]["value"].get<std::vector<double>>()));
+                vec.push_back(std::make_shared<T>(server[length]["type"].get<std::string>(), length, server[length]["value"].get<std::vector<double>>()));
             }
             else {
-                vec.push_back(std::make_shared<T>(this, server[length]["type"].get<std::string>(), length, std::vector<double>{server[length]["value"].get<double>()}));
+                vec.push_back(std::make_shared<T>(server[length]["type"].get<std::string>(), length, std::vector<double>{server[length]["value"].get<double>()}));
                 std::cout << "Warning: Server expects single values, but mdd work with arrays!" << std::endl;
             }
         }

+ 4 - 4
lib/src/ModuleMath.cpp

@@ -52,10 +52,10 @@ namespace mdd {
     {
         _ops.at("add");
         _operation = _ops.find("add")->second;
-        std::vector<double> default_val = {1};
-        inputs.push_back(std::make_shared<Input>(this, "Value", 0, default_val));
-        inputs.push_back(std::make_shared<Input>(this, "Value", 1, default_val));
-        outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
+        //std::vector<double> default_val = {1};
+        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 = "ModuleMath";
         setName("Math");
     }

+ 6 - 6
lib/src/ModuleMerge.cpp

@@ -21,17 +21,16 @@ namespace mdd {
 			}
         })JSON")
 	{
-		std::vector<double> default_val = { 1 };
-		inputs.push_back(std::make_shared<Input>(this, "Value", 0, default_val));
-		inputs.push_back(std::make_shared<Input>(this, "Value", 1, default_val));
-		outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
+		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() {
 		std::vector<double> default_val = { 1 };
-		inputs.push_back(std::make_shared<Input>(this, "Value", inputs.size(), default_val));
+		inputs.push_back(std::make_shared<Input>("Value", inputs.size()));
 		return inputs.size();
 	}
 
@@ -69,7 +68,8 @@ namespace mdd {
 			else {
 				for (size_t j = length; j < new_length; j++)
 				{
-					inputs.push_back(std::make_shared<Input>(this, "Value", j, std::vector<double> {1}));
+					inputs.push_back(std::make_shared<Input>("Value", j));
+					inputs.back()->setParent(shared_from_this());
 				}
 			}
 			_base_config["configure"]["inputs"]["value"] = jval;

+ 4 - 2
lib/src/ModuleSQL.cpp

@@ -207,10 +207,12 @@ namespace mdd {
 		{
 			return false;
 		}
-		inputs.push_back(std::make_shared<Input>(this, _content[0].key, inputs.size(), std::vector<double>{0}));
+		inputs.push_back(std::make_shared<Input>(_content[0].key, inputs.size()));
+		inputs.back()->setParent(shared_from_this());
 		for (auto& cont : _content)
 		{
-			outputs.push_back(std::make_shared<Output>(this, cont.key, outputs.size(), std::vector<double>{1}));
+			outputs.push_back(std::make_shared<Output>(cont.key, outputs.size()));
+			outputs.back()->setParent(shared_from_this());
 		}
 
 		_base_config["configure"]["database"]["value"] = _dbname;

+ 5 - 5
lib/src/ModuleSwitch.cpp

@@ -17,10 +17,9 @@ namespace mdd{
             }
         })JSON")
     {    
-        std::vector<double> default_val = { 1 };
-        inputs.push_back(std::make_shared<Input>(this, "Switch", 0, default_val));
-        inputs.push_back(std::make_shared<Input>(this, "Default", 0, default_val));
-        outputs.push_back(std::make_shared<Output>(this, "Value", 0, default_val));
+        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");
     }
@@ -44,7 +43,8 @@ namespace mdd{
 
                 for (size_t j = length_before - 1; j < length; j++)
                 {
-                    inputs.push_back(std::make_shared<Input>(this, "Value", inputs.size() - 1, std::vector<double>{1}));
+                    inputs.push_back(std::make_shared<Input>("Value", inputs.size() - 1));
+                    inputs.back()->setParent(shared_from_this());
                 }
                 for (size_t j = length; j < length_before - 1; j++)
                 {

+ 4 - 4
lib/src/OptimizerBase.cpp

@@ -64,8 +64,8 @@ namespace mdd{
 		return parser.compile(func, _func_expr);
 	}
 	
-	bool OptimizerBase::configure(const std::string& config) {
-		json jconfig = json::parse(config);
+	bool OptimizerBase::configure(const json& config) {
+		json jconfig = config;
 		auto jit = jconfig.find("configure");
 		if (jit != jconfig.end())
 		{
@@ -78,8 +78,8 @@ namespace mdd{
 		return true;
 	}
 
-	const std::string& OptimizerBase::getConfiguration() {
-		return _base_config.dump();
+	const json& OptimizerBase::getConfiguration() {
+		return _base_config;
 	}
 	//virtual std::string getGeneratorID() = 0;
 	void OptimizerBase::load(const json& j) {

+ 40 - 19
lib/src/Output.cpp

@@ -1,5 +1,6 @@
 #include "Output.h"
 namespace mdd {
+    //template<typename T> class shared : public std::shared_ptr<T> {};
     int Output::addConnection(std::shared_ptr<IInput> input)
     {
         _connections.push_back(input);
@@ -8,17 +9,19 @@ namespace mdd {
     int Output::removeConnection(std::shared_ptr<IInput> input)
     {
         for (auto it = _connections.begin(); it != _connections.end(); ++it) {
-            if ((*it)->getIdentifier() == input->getIdentifier())
+            if (auto in_ptr = it->lock())
             {
-                _connections.erase(it);
-                return _connections.size();
+                if (in_ptr->getIdentifier() == input->getIdentifier())
+                {
+                    _connections.erase(it);
+                    return _connections.size();
+                }
             }
         }
         return _connections.size();
     }
-    Output::Output(IModule* parent, const std::string& name, int appendix, const std::vector<double>& initial) :
-            _state(state::UNCHANGED),
-            _parent(parent)
+    Output::Output(const std::string& name, int appendix, const std::vector<double>& initial) :
+            _state(state::UNCHANGED)
     {
         _appendix = appendix;
         _name = name;
@@ -92,8 +95,18 @@ namespace mdd {
         return _name + std::to_string(_appendix);
     }
 
-    std::string Output::getParentID() {
-        return _parent->getID();
+    void Output::setParent(IModule::Ptr parent) {
+        _parent = parent;
+    }
+
+    std::vector<std::string> Output::getParentID() {
+        std::vector<std::string> ret;
+        if (auto parent_ptr = _parent.lock())
+        {
+            ret = parent_ptr->getParentID();
+            ret.push_back(parent_ptr->getID());
+        }
+        return ret;
     }
 
     bool Output::connect(std::shared_ptr<IInput> input)
@@ -108,28 +121,36 @@ namespace mdd {
         if (input->getConnection() == nullptr)
         {
             addConnection(input);
-            input->addConnection(std::make_shared<Output>((*this)));
+            input->addConnection(shared_from_this());
         }
         return true;
     }
     std::vector<std::shared_ptr<IInput>> Output::getConnections()
     {
-        return _connections;
+        std::vector<std::shared_ptr<IInput>> ret;
+        for (size_t i = 0; i < _connections.size(); i++)
+        {
+            if (auto& ptr = _connections[i].lock())
+            {
+                ret.push_back(ptr);
+            }
+        }
+        return ret;
     }
     void Output::disconnect() {
         for (size_t i = 0; i < _connections.size(); i++)
         {
-            if (_connections[i] != nullptr)
+            if (auto ptr = _connections[i].lock())
             {
-                _connections[i]->removeConnection(std::make_shared<Output>((*this)));
+                ptr->removeConnection(std::make_shared<Output>((*this)));
             }
         }
         _connections.clear();
     }
 
-    bool Output::configure(const std::string& config)
+    bool Output::configure(const json& config)
     {
-        json jconfig = json::parse(config);
+        json jconfig = config;
         bool success = false;
         auto jit = jconfig.find("name");
         if (jit != jconfig.end())
@@ -161,24 +182,24 @@ namespace mdd {
         return success;
     }
 
-    const std::string& Output::getConfiguration()
+    const json& Output::getConfiguration()
     {
         _base_config["optimizable"] = _optimizable;
         _base_config["value"] = _value;
         _base_config["type"] = "output";
         _base_config["key"] = "Output";
-        return _base_config.dump();
+        return _base_config;
     }
 
     void Output::load(const json& j)
     {
-        configure(j.dump());
+        configure(j);
         return;
     }
 
     json Output::dump()
     {
-        json jdump = _base_config;
+        json jdump = getConfiguration();
         jdump["ID"] = getIdentifier();
         return jdump;
     }
@@ -188,7 +209,7 @@ namespace mdd {
         jID["name"] = _name;
         jID["appendix"] = _appendix;
         jID["type"] = "output";
-        jID["prefix"].push_back(_parent->getID());
+        jID["prefix"]= getParentID();
         return jID;
     }
 }

+ 3 - 5
lib/src/Parameter.cpp

@@ -1,17 +1,15 @@
 #include "Parameter.h"
 
 namespace mdd {
-    Parameter::Parameter(IModule* parent)
+    Parameter::Parameter()
         : ModuleBase(R"JSON(
         [{
             "name":"appendix",
             "value":0
         }])JSON")
     {
-        _parent = parent;
-        std::vector<double> default_val = { 1 };
-        inputs.push_back(std::make_shared<Input>(parent, "Value", 0, default_val));
-        outputs.push_back(std::make_shared<Output>(parent, "Value", 0, default_val));
+        inputs.push_back(std::make_shared<Input>("Value", 0));
+        outputs.push_back(std::make_shared<Output>("Value", 0));
         
         key = "Parameter";
         setName(key);

+ 104 - 49
lib/src/ProcessorBase.cpp

@@ -13,8 +13,8 @@ namespace mdd{
         }
     }
 
-    bool ProcessorBase::configure(const std::string& config) {
-        json jconfig = json::parse(config);
+    bool ProcessorBase::configure(const json& config) {
+        json jconfig = config;
         bool sucess = false;
         auto jit = jconfig.find("GUI");
         if (jit != jconfig.end())
@@ -30,9 +30,13 @@ namespace mdd{
             {
                 for (size_t i = 0; i < jit.value().size(); i++)
                 {
-                    inputs.push_back(std::make_shared<Parameter>(this));
-                    inputs.back()->setName(jit2.value()["type"].get<std::string>());
-                    inputs.back()->setAppendix(jit2.value()["appendix"].get<int>());
+                    inputs.push_back(std::make_shared<Parameter>());
+                    if (auto input_ptr = inputs.back())
+                    {
+                        input_ptr->setParent(shared_from_this());
+                        input_ptr->setName(jit2.value()["type"].get<std::string>());
+                        input_ptr->setAppendix(jit2.value()["appendix"].get<int>());
+                    }
                 }
                 sucess = true;
             }
@@ -42,9 +46,13 @@ namespace mdd{
             {
                 for (size_t i = 0; i < jit.value().size(); i++)
                 {
-                    outputs.push_back(std::make_shared<Parameter>(this));
-                    outputs.back()->setName(jit2.value()["type"].get<std::string>());
-                    outputs.back()->setAppendix(jit2.value()["appendix"].get<int>());
+                    outputs.push_back(std::make_shared<Parameter>());
+                    if (auto output_ptr = outputs.back())
+                    {
+                        output_ptr->setParent(shared_from_this());
+                        output_ptr->setName(jit2.value()["type"].get<std::string>());
+                        output_ptr->setAppendix(jit2.value()["appendix"].get<int>());
+                    }
                 }
                 sucess = true;
             }
@@ -57,9 +65,13 @@ namespace mdd{
             {
                 const json& jmodule = jit.value()[i];
                 addModule(regi.generateModule(jmodule["id"].get<std::string>()));
-                modules.back()->setName(jmodule["type"].get<std::string>());
-                modules.back()->setAppendix(jmodule["appendix"].get<int>());
-                modules.back()->load(jmodule["load"]);
+                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"]);
+                }
             }
             sucess = true;
         }
@@ -85,15 +97,29 @@ namespace mdd{
         return true;
     }
 
-    const std::string& ProcessorBase::getConfiguration() {
+    const json& ProcessorBase::getConfiguration() {
         //update module apendix ?
-        return _base_config.dump();
+        return _base_config;
     }
 
     std::string ProcessorBase::getType() {
         return  type;
     }
 
+    void ProcessorBase::setParent(std::shared_ptr<IModule> parent) {
+        _parent = parent;
+    }
+
+    std::vector<std::string> ProcessorBase::getParentID() {
+        std::vector<std::string> ret;
+        if (auto parent_ptr = _parent.lock())
+        {
+            ret = parent_ptr->getParentID();
+            ret.push_back(parent_ptr->getID());
+        }
+        return ret;
+    }
+
     std::string ProcessorBase::getGeneratorKey() {
         return  key;
     }
@@ -123,13 +149,17 @@ namespace mdd{
 
     std::string ProcessorBase::addModule(std::shared_ptr<IModule> module){
         modules.emplace_back(module);
-        modules.back()->setAppendix(modules.size()-1);
-        return modules.back()->getID();
+        if (auto module_ptr = modules.back())
+        {
+            module_ptr->setParent(shared_from_this());
+            module_ptr->setAppendix(modules.size() - 1);
+            return module_ptr->getID();
+        }
+        return "";
     }
 
     void ProcessorBase::removeModule(std::shared_ptr<IModule> module)
     {
-
         for (auto it = modules.begin(); it != modules.end(); ++it) {
             if ((*it) == module)
             {
@@ -182,10 +212,13 @@ namespace mdd{
         else {
             for (size_t i = 0; i < modules.size(); i++)
             {
-                if (modules[i]->getID() == str_module_id)
+                if (auto module_ptr = modules[i])
                 {
-                    out_ptr = modules[i]->getOutput(str_output_id);
-                    break;
+                    if (module_ptr->getID() == str_module_id)
+                    {
+                        out_ptr = module_ptr->getOutput(str_output_id);
+                        break;
+                    }
                 }
             }
         }
@@ -204,11 +237,14 @@ namespace mdd{
             else {
                 for (size_t i = 0; i < modules.size(); i++)
                 {
-                    if (modules[i]->getID() == str_module_id)
+                    if (auto module_ptr = modules[i])
                     {
-                        auto in_ptr = modules[i]->getInput(str_input_id);
-                        in_ptr->connect(out_ptr);
-                        break;
+                        if (module_ptr->getID() == str_module_id)
+                        {
+                            auto in_ptr = module_ptr->getInput(str_input_id);
+                            in_ptr->connect(out_ptr);
+                            break;
+                        }
                     }
                 }
             }
@@ -269,8 +305,11 @@ namespace mdd{
                 {
                     addModule(regi.generateProcessor(jmodule["key"]));
                 }
-                modules.back()->load(jmodule);
-                int check = 0;
+                if (auto module_ptr = modules.back())
+                {
+                    module_ptr->setParent(shared_from_this());
+                    module_ptr->load(jmodule);
+                }
             }
         }
 
@@ -342,23 +381,30 @@ namespace mdd{
         ret["params"]["inputs"];
         for (size_t i = 0; i < inputs.size(); i++)
         {
-            json sub;
-            sub["type"] = inputs[i]->getType();
-            sub["appendix"] = inputs[i]->getAppendix();
-            ret["params"]["inputs"].push_back(sub);
+            if (auto in_ptr = inputs[i])
+            {
+                json sub;
+                sub["type"] = in_ptr->getType();
+                sub["appendix"] = in_ptr->getAppendix();
+                ret["params"]["inputs"].push_back(sub);
+            }
         }
 
         ret["params"]["outputs"];
         for (size_t i = 0; i < outputs.size(); i++)
         {
-            json sub;
-            sub["type"] = outputs[i]->getType();
-            sub["appendix"] = outputs[i]->getAppendix();
-            ret["params"]["outputs"].push_back(sub);
+            if (auto out_ptr = outputs[i])
+            {
+                json sub;
+                sub["type"] = out_ptr->getType();
+                sub["appendix"] = out_ptr->getAppendix();
+                ret["params"]["outputs"].push_back(sub);
+            }
         }
 
         for (auto& in : processor_inputs)
         {
+            in->setParent(shared_from_this());
             ret["inputs"].push_back(in->dump());
         }
         for (auto& in : inputs)
@@ -368,6 +414,7 @@ namespace mdd{
 
         for (auto& out : processor_outputs)
         {
+            out->setParent(shared_from_this());
             ret["outputs"].push_back(out->dump());
         }
         for (auto& out : outputs)
@@ -376,28 +423,36 @@ namespace mdd{
         }
         for (auto& mod : modules)
         {
-            if (mod->getType()=="module")
-            {
-                ret["modules"].push_back(mod->dump());
-            }
-            else
+            if (auto module_ptr = mod)
             {
-                ret["modules"].push_back(mod->dump());
+                if (module_ptr->getType() == "module")
+                {
+                    ret["modules"].push_back(module_ptr->dump());
+                }
+                else
+                {
+                    ret["modules"].push_back(module_ptr->dump());
+                }
             }
-           
+        }
 
-            for (size_t j = 0; j < mod->getNumOutputs(); j++)
+        for (auto& mod : modules)
+        {
+            if (auto module_ptr = mod)
             {
-                auto input_connections = mod->getOutput(j)->getConnections();
-                if (!input_connections.empty())
+                for (size_t j = 0; j < module_ptr->getNumOutputs(); j++)
                 {
-                    json connect;
-                    connect["output"] = mod->getOutput(j)->getIdentifier();
-                    for (size_t k = 0; k < input_connections.size(); k++)
+                    auto input_connections = module_ptr->getOutput(j)->getConnections();
+                    if (!input_connections.empty())
                     {
-                        connect["inputs"].push_back(input_connections[k]->getIdentifier());
+                        json connect;
+                        connect["output"] = module_ptr->getOutput(j)->getIdentifier();
+                        for (size_t k = 0; k < input_connections.size(); k++)
+                        {
+                            connect["inputs"].push_back(input_connections[k]->getIdentifier());
+                        }
+                        ret["connections"].push_back(connect);
                     }
-                    ret["connections"].push_back(connect);
                 }
             }
         }
@@ -409,7 +464,7 @@ namespace mdd{
         jID["name"] = _name;
         jID["appendix"] = getAppendix();
         jID["type"] = type;
-        jID["prefix"] = std::vector<std::string>();
+        jID["prefix"] = getParentID();
         return jID;
     }
 

+ 1 - 20
lib/src/ProcessorStandard.cpp

@@ -26,7 +26,7 @@ namespace mdd {
         
         key = "ProcessorStandard";
         setName(key);
-        processor_outputs.push_back(std::make_shared<Output>(this, "Iterator", 0, std::vector<double>{0}));
+        processor_outputs.push_back(std::make_shared<Output>("Iterator", 0, std::vector<double>{0}));
     }
 
     bool ProcessorStandard::configureChild(const json& config) {
@@ -66,25 +66,6 @@ namespace mdd {
         return found;
     }
 
-    std::string ProcessorStandard::addModule(std::shared_ptr<IModule> module)
-    {
-        std::string id = ProcessorBase::addModule(module);
-        //_priority_list.emplace_back(module);
-        return id;
-    }
-
-    void ProcessorStandard::removeModule(std::shared_ptr<IModule> module)
-    {
-        ProcessorBase::removeModule(module);
-        //for (auto it = _priority_list.begin(); it != _priority_list.end(); ++it) {
-        //    if (it->module_ptr == module)
-        //    {
-        //        _priority_list.erase(it);
-        //        return;
-        //    }
-        //}
-    }
-
     std::vector<std::shared_ptr<IModule>> ProcessorStandard::getModulePriority()
     {
         std::vector <std::shared_ptr<IModule>> ret;

+ 1 - 1
lib/test/test_Output.cpp

@@ -7,7 +7,7 @@
 using namespace mdd;
 
 TEST(Output, setValue) {
-    std::shared_ptr<Output> out_ptr = std::make_shared<Output>(nullptr, "TEST", 0, std::vector<double>{0});
+    std::shared_ptr<Output> out_ptr = std::make_shared<Output>("TEST", 0, std::vector<double>{0});
     EXPECT_FLOAT_EQ(out_ptr->getValue()[0], 0.0);
     out_ptr->setValue(std::vector<double>{1});
     EXPECT_FLOAT_EQ(out_ptr->getValue()[0], 1.0);

+ 6 - 6
lib/test/test_ProcessorStandard.cpp

@@ -48,7 +48,7 @@ TEST(ProcessorStandard, CalculateSimpleFormula){
     processor->addModule(f3);
     processor->addModule(f4);
 
-    processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
+    processor->getOutputParams().push_back(std::make_shared<Parameter>());
     processor->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
    
     processor->update();
@@ -98,7 +98,7 @@ TEST(ProcessorStandard, CalculateAdvancedFormula){
     processor->addModule(f3);
     processor->addModule(f2);
     processor->addModule(f1);
-    processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
+    processor->getOutputParams().push_back(std::make_shared<Parameter>());
     processor->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
     processor->update();
     //std::cout << test->getOutput(test->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
@@ -129,7 +129,7 @@ TEST(ProcessorStandard, CalculateExtremeFormula){
     calcModule->getInput(0)->connect(switchModule->getOutput(0));
     calcModule->getInput(1)->setValue() = { 2.0 };
 
-    processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
+    processor->getOutputParams().push_back(std::make_shared<Parameter>());
     processor->getOutputParams().at(0)->getInput(0)->connect(calcModule->getOutput(0));
     processor->update();
 
@@ -175,7 +175,7 @@ TEST(ProcessorStandard, CalculateAdvancedFormulaWithSTATIC) {
     processor->addModule(f3);
     processor->addModule(f2);
     processor->addModule(f1);
-    processor->getOutputParams().push_back(std::make_shared<Parameter>(&(*processor)));
+    processor->getOutputParams().push_back(std::make_shared<Parameter>());
     processor->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
     processor->update();
     //std::cout << test->getOutput(test->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;
@@ -222,7 +222,7 @@ TEST(ProcessorStandard, PrioritySTATIC) {
     process_manual->addModule(f2);
     process_manual->addModule(f3);
     process_manual->addModule(f4);
-    process_manual->getOutputParams().push_back(std::make_shared<Parameter>(&(*process_manual)));
+    process_manual->getOutputParams().push_back(std::make_shared<Parameter>());
     process_manual->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
     process_manual->update();
 
@@ -230,7 +230,7 @@ TEST(ProcessorStandard, PrioritySTATIC) {
     process_static->addModule(f3);
     process_static->addModule(f2);
     process_static->addModule(f1);
-    process_static->getOutputParams().push_back(std::make_shared<Parameter>(&(*process_static)));
+    process_static->getOutputParams().push_back(std::make_shared<Parameter>());
     process_static->getOutputParams().at(0)->getInput(0)->connect(f4->getOutput(0));
     process_static->update();
     //std::cout << process_static->getOutput(process_static->getOutputIDs()[1])->getValue()["value"].dump() << std::endl;

+ 9 - 5
server/src/main.cpp

@@ -62,6 +62,7 @@ private:
     }
 
     void try_request(json request) {
+        //auto start = std::chrono::steady_clock::now();
         json jmsg = msg_header();
         if (request.is_array())
         {
@@ -80,8 +81,7 @@ private:
         zmq::message_t msg(smsg.size());
         memcpy(topic.data(), stopic.c_str(), stopic.size());
         memcpy(msg.data(), smsg.c_str(), smsg.size());
-
-        Sleep(500);
+        //auto middle = std::chrono::steady_clock::now();
         try {
             std::cout << "[change]: " << stopic << ": " << smsg << std::endl;
             s_sendmore(publisher_socket, stopic);
@@ -92,11 +92,15 @@ private:
         catch (zmq::error_t& e) {
             std::cout << e.what() << std::endl;
         }
-        std::cout << "Test" << std::endl;
         //msg.rebuild(3);
         //topic.rebuild(4);
 
         ++counter;
+        
+        //auto end = std::chrono::steady_clock::now();
+        //std::chrono::duration<double> elapsed_seconds = middle - start;
+        //std::chrono::duration<double> elapsed_seconds2 = end - middle;
+        //std::cout << "[Server]: 1:" << elapsed_seconds.count() << " s 2:" << elapsed_seconds2.count() << "s\n";
     }
 
 public:
@@ -128,6 +132,7 @@ public:
         {
             std::cout << "[Server] received wrong msg!" << std::endl;
         }
+        
         json msg;
         try
         {
@@ -157,13 +162,13 @@ public:
         {
             return send_alive();
         }
-
         json jcontext = msg["context"];
 
         if (jcontext.is_array())
         {
             for (size_t i = 0; i < jcontext.size(); i++)
             {
+               
                 if (jcontext[i].contains("try"))
                 {
                     try_request(jcontext[i]["try"]);
@@ -187,7 +192,6 @@ public:
             }
         }
     }
-
 };