#include "Input.h" namespace mdd{ int Input::addConnection(std::shared_ptr output) { _output = output; return 1; } int Input::removeConnection(std::shared_ptr output) { _output = nullptr; return 0; } Input::Input(IModule* parent, const std::string& name, int appendix, const std::vector& default_value) { _parent = parent; _name = name; _value = default_value; _appendix = appendix; _optimizable = false; type = "input"; key = "Input"; } std::string Input::getType() { return type; } std::string Input::getGeneratorKey() { return key; } std::string Input::setName(const std::string& name){ _name = name; return getID(); } std::string Input::getName() { return _name; } std::string Input::setAppendix(int appendix){ _appendix = appendix; return getID(); } int Input::getAppendix() { return _appendix; } std::string Input::getID() { return _name + std::to_string(_appendix); } std::string Input::getParentID() { return _parent->getID(); } state Input::getState() { if (_output == nullptr) { return state::UNCHANGED; } else { return _output->getState(); } } void Input::resetState() { } const std::vector& Input::getValue() { if(_output == nullptr){ return _value; } else{ return _output->getValue(); } } std::shared_ptr Input::getConnection() { return _output; } std::vector& Input::setValue(){ return _value; } state Input::setValue(const std::vector& val) { if (val != _value) { _value = val; return state::CHANGED; } else { return state::UNCHANGED; } } bool Input::connect(std::shared_ptr output){ if (_output != nullptr) { if (_output->getIdentifier() != output->getIdentifier()) { _output->removeConnection(std::make_shared((*this))); removeConnection(); } } if (_output == nullptr) { addConnection(output); _output->addConnection(std::make_shared((*this))); } return true; } void Input::disconnect() { if (_output != nullptr) { _output->removeConnection(std::make_shared((*this))); } } bool Input::isOptimizable() { return _optimizable; } void Input::setOptimizability(bool state) { _optimizable = state; if (_optimizable && _limit == nullptr) { _limit = std::make_shared(); } } const std::shared_ptr Input::getLimits() { return _limit; } std::shared_ptr& Input::setLimits() { return _limit; } bool Input::configure(const std::string& config) { json jconfig = json::parse(config); bool success = false; auto jit = jconfig.find("name"); if (jit != jconfig.end()) { _name = jit.value().get(); success = true; } jit = jconfig.find("appendix"); if (jit != jconfig.end()) { _appendix = jit.value().get(); success = true; } jit = jconfig.find("optimizable"); if (jit != jconfig.end()) { _optimizable = jit.value().get(); success = true; } jit = jconfig.find("value"); if (jit != jconfig.end()) { _value = jit.value().get>(); success = true; } return success; } const std::string& Input::getConfiguration() { _base_config["value"] = _value; _base_config["optimizable"] = _optimizable; _base_config["type"] = "input"; _base_config["key"] = "Input"; return _base_config.dump(); } void Input::load(const json& j) { configure(j.dump()); return; } json Input::dump() { json jdump = _base_config; jdump["ID"] = getIdentifier(); return jdump; } json Input::getIdentifier() { json jID; jID["name"] = _name; jID["appendix"] = _appendix; jID["type"] = "input"; jID["prefix"].push_back(_parent->getID()); return jID; } }