Input.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "Input.h"
  2. namespace mdd{
  3. int Input::addConnection(std::shared_ptr<IOutput> output)
  4. {
  5. _output = output;
  6. return 1;
  7. }
  8. int Input::removeConnection(std::shared_ptr<IOutput> output)
  9. {
  10. _output = nullptr;
  11. return 0;
  12. }
  13. Input::Input(IModule* parent, const std::string& name, int appendix, const std::vector<double>& default_value) {
  14. _parent = parent;
  15. _name = name;
  16. _value = default_value;
  17. _appendix = appendix;
  18. _optimizable = false;
  19. type = "input";
  20. key = "Input";
  21. }
  22. std::string Input::getType() {
  23. return type;
  24. }
  25. std::string Input::getGeneratorKey() {
  26. return key;
  27. }
  28. std::string Input::setName(const std::string& name){
  29. _name = name;
  30. return getID();
  31. }
  32. std::string Input::getName() {
  33. return _name;
  34. }
  35. std::string Input::setAppendix(int appendix){
  36. _appendix = appendix;
  37. return getID();
  38. }
  39. int Input::getAppendix() {
  40. return _appendix;
  41. }
  42. std::string Input::getID() {
  43. return _name + std::to_string(_appendix);
  44. }
  45. std::string Input::getParentID() {
  46. return _parent->getID();
  47. }
  48. state Input::getState() {
  49. if (_output == nullptr) {
  50. return state::UNCHANGED;
  51. }
  52. else {
  53. return _output->getState();
  54. }
  55. }
  56. void Input::resetState() {
  57. }
  58. const std::vector<double>& Input::getValue() {
  59. if(_output == nullptr){
  60. return _value;
  61. } else{
  62. return _output->getValue();
  63. }
  64. }
  65. std::shared_ptr<IOutput> Input::getConnection() { return _output; }
  66. std::vector<double>& Input::setValue(){
  67. return _value;
  68. }
  69. state Input::setValue(const std::vector<double>& val) {
  70. if (val != _value)
  71. {
  72. _value = val;
  73. return state::CHANGED;
  74. }
  75. else {
  76. return state::UNCHANGED;
  77. }
  78. }
  79. bool Input::connect(std::shared_ptr<IOutput> output){
  80. if (_output != nullptr) {
  81. if (_output->getIdentifier() != output->getIdentifier())
  82. {
  83. _output->removeConnection(std::make_shared<Input>((*this)));
  84. removeConnection();
  85. }
  86. }
  87. if (_output == nullptr)
  88. {
  89. addConnection(output);
  90. _output->addConnection(std::make_shared<Input>((*this)));
  91. }
  92. return true;
  93. }
  94. void Input::disconnect() {
  95. if (_output != nullptr)
  96. {
  97. _output->removeConnection(std::make_shared<Input>((*this)));
  98. }
  99. }
  100. bool Input::isOptimizable() {
  101. return _optimizable;
  102. }
  103. void Input::setOptimizability(bool state) {
  104. _optimizable = state;
  105. if (_optimizable && _limit == nullptr)
  106. {
  107. _limit = std::make_shared<limits>();
  108. }
  109. }
  110. const std::shared_ptr<limits> Input::getLimits()
  111. {
  112. return _limit;
  113. }
  114. std::shared_ptr<limits>& Input::setLimits()
  115. {
  116. return _limit;
  117. }
  118. bool Input::configure(const std::string& config)
  119. {
  120. json jconfig = json::parse(config);
  121. bool success = false;
  122. auto jit = jconfig.find("name");
  123. if (jit != jconfig.end())
  124. {
  125. _name = jit.value().get<std::string>();
  126. success = true;
  127. }
  128. jit = jconfig.find("appendix");
  129. if (jit != jconfig.end())
  130. {
  131. _appendix = jit.value().get<int>();
  132. success = true;
  133. }
  134. jit = jconfig.find("optimizable");
  135. if (jit != jconfig.end())
  136. {
  137. _optimizable = jit.value().get<bool>();
  138. success = true;
  139. }
  140. jit = jconfig.find("value");
  141. if (jit != jconfig.end())
  142. {
  143. _value = jit.value().get<std::vector<double>>();
  144. success = true;
  145. }
  146. return success;
  147. }
  148. const std::string& Input::getConfiguration()
  149. {
  150. _base_config["value"] = _value;
  151. _base_config["optimizable"] = _optimizable;
  152. _base_config["type"] = "input";
  153. _base_config["key"] = "Input";
  154. return _base_config.dump();
  155. }
  156. void Input::load(const json& j)
  157. {
  158. configure(j.dump());
  159. return;
  160. }
  161. json Input::dump()
  162. {
  163. json jdump = _base_config;
  164. jdump["ID"] = getIdentifier();
  165. return jdump;
  166. }
  167. json Input::getIdentifier() {
  168. json jID;
  169. jID["name"] = _name;
  170. jID["appendix"] = _appendix;
  171. jID["type"] = "input";
  172. jID["prefix"].push_back(_parent->getID());
  173. return jID;
  174. }
  175. }