123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "Input.h"
- namespace mdd{
- int Input::addConnection(std::shared_ptr<IOutput> output)
- {
- _output = output;
- return 1;
- }
- int Input::removeConnection(std::shared_ptr<IOutput> output)
- {
- _output = nullptr;
- return 0;
- }
- Input::Input(IModule* parent, const std::string& name, int appendix, const std::vector<double>& 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<double>& Input::getValue() {
- if(_output == nullptr){
- return _value;
- } else{
- return _output->getValue();
- }
- }
- std::shared_ptr<IOutput> Input::getConnection() { return _output; }
- std::vector<double>& Input::setValue(){
- return _value;
- }
- state Input::setValue(const std::vector<double>& val) {
- if (val != _value)
- {
- _value = val;
- return state::CHANGED;
- }
- else {
- return state::UNCHANGED;
- }
- }
- bool Input::connect(std::shared_ptr<IOutput> output){
- if (_output != nullptr) {
- if (_output->getIdentifier() != output->getIdentifier())
- {
- _output->removeConnection(std::make_shared<Input>((*this)));
- removeConnection();
- }
- }
- if (_output == nullptr)
- {
- addConnection(output);
- _output->addConnection(std::make_shared<Input>((*this)));
- }
- return true;
- }
- void Input::disconnect() {
- if (_output != nullptr)
- {
- _output->removeConnection(std::make_shared<Input>((*this)));
- }
- }
- bool Input::isOptimizable() {
- return _optimizable;
- }
- void Input::setOptimizability(bool state) {
- _optimizable = state;
- if (_optimizable && _limit == nullptr)
- {
- _limit = std::make_shared<limits>();
- }
- }
- const std::shared_ptr<limits> Input::getLimits()
- {
- return _limit;
- }
- std::shared_ptr<limits>& 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<std::string>();
- success = true;
- }
- jit = jconfig.find("appendix");
- if (jit != jconfig.end())
- {
- _appendix = jit.value().get<int>();
- success = true;
- }
- jit = jconfig.find("optimizable");
- if (jit != jconfig.end())
- {
- _optimizable = jit.value().get<bool>();
- success = true;
- }
- jit = jconfig.find("value");
- if (jit != jconfig.end())
- {
- _value = jit.value().get<std::vector<double>>();
- 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;
- }
- }
|