You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.5 KiB

  1. #include "ConfigYaml.h"
  2. #include <iostream>
  3. std::ostream& operator <<(std::ostream &os, const Label& label) {
  4. os << "\"" << label.label_ << "\"" << "=" << label.text_;
  5. return os;
  6. }
  7. std::ostream& operator << (std::ostream &os, const Formula& formula) {
  8. os << formula.formula_ << "=" << formula.content_;
  9. return os;
  10. }
  11. std::ostream& operator << (std::ostream& os, const Action& action) {
  12. os << action.action_;
  13. return os;
  14. }
  15. std::ostream& operator << (std::ostream& os, const Module& module) {
  16. os << "Module: " << module.module_ << std::endl;
  17. for (auto& action : module.actions_) {
  18. os << action << std::endl;
  19. }
  20. return os;
  21. }
  22. std::string Label::createExpression() const {
  23. if (overwrite_) {
  24. return "label \"" + label_ + "\" = " + text_ + Configuration::overwrite_identifier_;
  25. }
  26. return "label \"" + label_ + "\" = " + text_ + Configuration::configuration_identifier_;
  27. }
  28. std::string Formula::createExpression() const {
  29. if (overwrite_) {
  30. return "formula " + formula_ + " = " + content_ + Configuration::overwrite_identifier_;
  31. }
  32. return "formula " + formula_ + " = " + content_ + Configuration::configuration_identifier_;
  33. }
  34. std::string Action::createExpression() const {
  35. if (overwrite_) {
  36. return action_ + "\t" + guard_ + "-> " + update_ + Configuration::overwrite_identifier_;
  37. }
  38. return "\t" + action_ + "\t" + guard_ + "-> " + update_+ Configuration::configuration_identifier_;
  39. }
  40. YAML::Node YAML::convert<Module>::encode(const Module& rhs) {
  41. YAML::Node node;
  42. node.push_back(rhs.module_);
  43. node.push_back(rhs.actions_);
  44. return node;
  45. }
  46. bool YAML::convert<Module>::decode(const YAML::Node& node, Module& rhs) {
  47. if (!node.Type() == NodeType::Map) {
  48. return false;
  49. }
  50. rhs.actions_ = node["actions"].as<std::vector<Action>>();
  51. rhs.module_ = node["module"].as<std::string>();
  52. return true;
  53. }
  54. YAML::Node YAML::convert<Action>::encode(const Action& rhs) {
  55. YAML::Node node;
  56. node.push_back(rhs.action_);
  57. node.push_back(rhs.guard_);
  58. node.push_back(rhs.overwrite_);
  59. node.push_back(rhs.update_);
  60. return node;
  61. }
  62. bool YAML::convert<Action>::decode(const YAML::Node& node, Action& rhs) {
  63. if (!node.Type() == NodeType::Map) {
  64. return false;
  65. }
  66. rhs.action_ = node["action"].as<std::string>();
  67. rhs.guard_ = node["guard"].as<std::string>();
  68. rhs.update_ = node["update"].as<std::string>();
  69. if (node["overwrite"]) {
  70. rhs.overwrite_ = node["overwrite"].as<bool>();
  71. }
  72. return true;
  73. }
  74. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  75. YAML::Node node;
  76. node.push_back(rhs.label_);
  77. node.push_back(rhs.text_);
  78. return node;
  79. }
  80. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  81. if (!node.Type() == NodeType::Map || !node["label"] || !node["text"]) {
  82. return false;
  83. }
  84. rhs.label_ = node["label"].as<std::string>();
  85. rhs.text_ = node["text"].as<std::string>();
  86. if (node["overwrite"]) {
  87. rhs.overwrite_ = node["overwrite"].as<bool>();
  88. }
  89. return true;
  90. }
  91. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  92. YAML::Node node;
  93. node.push_back(rhs.content_);
  94. node.push_back(rhs.formula_);
  95. node.push_back(rhs.overwrite_);
  96. return node;
  97. }
  98. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  99. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["formula"] || !node["content"]) {
  100. return false;
  101. }
  102. rhs.formula_ = node["formula"].as<std::string>();
  103. rhs.content_ = node["content"].as<std::string>();
  104. if(node["overwrite"]) {
  105. rhs.overwrite_ = node["overwrite"].as<bool>();
  106. }
  107. return true;
  108. }
  109. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  110. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  111. std::vector<Configuration> YamlConfigParser::parseConfiguration() {
  112. std::vector<Configuration> configuration;
  113. try {
  114. YAML::Node config = YAML::LoadFile(file_);
  115. std::vector<Label> labels;
  116. std::vector<Formula> formulas;
  117. std::vector<Module> modules;
  118. if (config["labels"]) {
  119. labels = config["labels"].as<std::vector<Label>>();
  120. }
  121. if (config["formulas"]) {
  122. formulas = config["formulas"].as<std::vector<Formula>>();
  123. }
  124. if (config["modules"]) {
  125. modules = config["modules"].as<std::vector<Module>>();
  126. }
  127. for (auto& label : labels) {
  128. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  129. }
  130. for (auto& formula : formulas) {
  131. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  132. }
  133. for (auto& module : modules) {
  134. for (auto& action : module.actions_) {
  135. configuration.push_back({action.createExpression(), action.action_, ConfigType::Module, action.overwrite_, module.module_});
  136. }
  137. }
  138. }
  139. catch(const std::exception& e) {
  140. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  141. std::cout << "\t" << e.what() << std::endl;
  142. std::cout << "while parsing configuration " << file_ << std::endl;
  143. }
  144. return configuration;
  145. }