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.

175 lines
4.8 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_ + "; // Overwrite";
  25. }
  26. return "label \"" + label_ + "\" = " + text_ + ";";
  27. }
  28. std::string Formula::createExpression() const {
  29. if (overwrite_) {
  30. return "formula " + formula_ + " = " + content_ + "; // Overwrite";
  31. }
  32. return "formula " + formula_ + " = " + content_ + ";";
  33. }
  34. std::string Action::createExpression() const {
  35. if (overwrite_) {
  36. return action_ + "\t" + guard_ + "-> " + update_ + "; // Overwrite";
  37. }
  38. return "\t" + action_ + "\t" + guard_ + "-> " + update_ + ";";
  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) {
  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.Type() == NodeType::Map) {
  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. std::vector<Configuration> YamlConfigParser::parseConfiguration() {
  110. std::vector<Configuration> configuration;
  111. try {
  112. YAML::Node config = YAML::LoadFile(file_);
  113. const std::vector<Label> labels = config["labels"].as<std::vector<Label>>();
  114. const std::vector<Formula> formulas = config["formulas"].as<std::vector<Formula>>();
  115. const std::vector<Module> modules = config["modules"].as<std::vector<Module>>();
  116. for (auto& label : labels) {
  117. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  118. }
  119. for (auto& formula : formulas) {
  120. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  121. }
  122. for (auto& module : modules) {
  123. for (auto& action : module.actions_) {
  124. configuration.push_back({action.createExpression(), action.action_, ConfigType::Module, action.overwrite_, module.module_});
  125. }
  126. }
  127. }
  128. catch(const std::exception& e) {
  129. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  130. std::cout << "\t" << e.what() << std::endl;
  131. std::cout << "while parsing configuration " << file_ << std::endl;
  132. }
  133. return configuration;
  134. }