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.

190 lines
5.6 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. if (node["index"]) {
  73. rhs.index_ = node["index"].as<int>();
  74. }
  75. return true;
  76. }
  77. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  78. YAML::Node node;
  79. node.push_back(rhs.label_);
  80. node.push_back(rhs.text_);
  81. return node;
  82. }
  83. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  84. if (!node.Type() == NodeType::Map || !node["label"] || !node["text"]) {
  85. return false;
  86. }
  87. rhs.label_ = node["label"].as<std::string>();
  88. rhs.text_ = node["text"].as<std::string>();
  89. if (node["overwrite"]) {
  90. rhs.overwrite_ = node["overwrite"].as<bool>();
  91. }
  92. return true;
  93. }
  94. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  95. YAML::Node node;
  96. node.push_back(rhs.content_);
  97. node.push_back(rhs.formula_);
  98. node.push_back(rhs.overwrite_);
  99. return node;
  100. }
  101. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  102. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["formula"] || !node["content"]) {
  103. return false;
  104. }
  105. rhs.formula_ = node["formula"].as<std::string>();
  106. rhs.content_ = node["content"].as<std::string>();
  107. if(node["overwrite"]) {
  108. rhs.overwrite_ = node["overwrite"].as<bool>();
  109. }
  110. return true;
  111. }
  112. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  113. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  114. std::vector<Configuration> YamlConfigParser::parseConfiguration() {
  115. std::vector<Configuration> configuration;
  116. try {
  117. YAML::Node config = YAML::LoadFile(file_);
  118. std::vector<Label> labels;
  119. std::vector<Formula> formulas;
  120. std::vector<Module> modules;
  121. if (config["labels"]) {
  122. labels = config["labels"].as<std::vector<Label>>();
  123. }
  124. if (config["formulas"]) {
  125. formulas = config["formulas"].as<std::vector<Formula>>();
  126. }
  127. if (config["modules"]) {
  128. modules = config["modules"].as<std::vector<Module>>();
  129. }
  130. for (auto& label : labels) {
  131. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  132. }
  133. for (auto& formula : formulas) {
  134. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  135. }
  136. for (auto& module : modules) {
  137. for (auto& action : module.actions_) {
  138. configuration.push_back({action.createExpression(), action.action_, ConfigType::Module, action.overwrite_, module.module_, action.index_});
  139. }
  140. }
  141. }
  142. catch(const std::exception& e) {
  143. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  144. std::cout << "\t" << e.what() << std::endl;
  145. std::cout << "while parsing configuration " << file_ << std::endl;
  146. }
  147. return configuration;
  148. }