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.

149 lines
3.9 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. YAML::Node YAML::convert<Module>::encode(const Module& rhs) {
  23. YAML::Node node;
  24. node.push_back(rhs.module_);
  25. node.push_back(rhs.actions_);
  26. return node;
  27. }
  28. bool YAML::convert<Module>::decode(const YAML::Node& node, Module& rhs) {
  29. if (!node.Type() == NodeType::Map) {
  30. return false;
  31. }
  32. rhs.actions_ = node["actions"].as<std::vector<Action>>();
  33. rhs.module_ = node["module"].as<std::string>();
  34. return true;
  35. }
  36. YAML::Node YAML::convert<Action>::encode(const Action& rhs) {
  37. YAML::Node node;
  38. node.push_back(rhs.action_);
  39. node.push_back(rhs.guard_);
  40. node.push_back(rhs.overwrite_);
  41. node.push_back(rhs.update_);
  42. return node;
  43. }
  44. bool YAML::convert<Action>::decode(const YAML::Node& node, Action& rhs) {
  45. if (!node.Type() == NodeType::Map) {
  46. return false;
  47. }
  48. rhs.action_ = node["action"].as<std::string>();
  49. rhs.guard_ = node["guard"].as<std::string>();
  50. rhs.update_ = node["update"].as<std::string>();
  51. if (node["overwrite"]) {
  52. rhs.overwrite_ = node["overwrite"].as<bool>();
  53. }
  54. return true;
  55. }
  56. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  57. YAML::Node node;
  58. node.push_back(rhs.label_);
  59. node.push_back(rhs.text_);
  60. return node;
  61. }
  62. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  63. if (!node.Type() == NodeType::Map) {
  64. return false;
  65. }
  66. rhs.label_ = node["label"].as<std::string>();
  67. rhs.text_ = node["text"].as<std::string>();
  68. if (node["overwrite"]) {
  69. rhs.overwrite_ = node["overwrite"].as<bool>();
  70. }
  71. return true;
  72. }
  73. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  74. YAML::Node node;
  75. node.push_back(rhs.content_);
  76. node.push_back(rhs.formula_);
  77. node.push_back(rhs.overwrite_);
  78. return node;
  79. }
  80. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  81. if (!node.Type() == NodeType::Map) {
  82. return false;
  83. }
  84. rhs.formula_ = node["formula"].as<std::string>();
  85. rhs.content_ = node["content"].as<std::string>();
  86. if(node["overwrite"]) {
  87. rhs.overwrite_ = node["overwrite"].as<bool>();
  88. }
  89. return true;
  90. }
  91. std::vector<Configuration> YamlConfigParser::parseConfiguration() {
  92. std::vector<Configuration> configuration;
  93. try {
  94. YAML::Node config = YAML::LoadFile(file_);
  95. const std::vector<Label> labels = config["labels"].as<std::vector<Label>>();
  96. const std::vector<Formula> formulas = config["formulas"].as<std::vector<Formula>>();
  97. const std::vector<Module> modules = config["modules"].as<std::vector<Module>>();
  98. for (auto& label : labels) {
  99. configuration.push_back({label.text_, label.label_, ConfigType::Label, label.overwrite_});
  100. }
  101. for (auto& formula : formulas) {
  102. configuration.push_back({formula.content_, formula.formula_ , ConfigType::Formula, formula.overwrite_});
  103. }
  104. for (auto& module : modules) {
  105. std::cout << module << std::endl;
  106. }
  107. }
  108. catch(const std::exception& e) {
  109. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  110. std::cout << "\t" << e.what() << std::endl;
  111. std::cout << "while parsing configuration " << file_ << std::endl;
  112. }
  113. return configuration;
  114. }