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.

237 lines
7.2 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 Constant& constant) {
  16. os << "const " << constant.type_ << " " << constant.constant_ << " = " << constant.value_;
  17. return os;
  18. }
  19. std::ostream& operator << (std::ostream& os, const Module& module) {
  20. os << "Module: " << module.module_ << std::endl;
  21. for (auto& action : module.actions_) {
  22. os << action << std::endl;
  23. }
  24. return os;
  25. }
  26. std::string Label::createExpression() const {
  27. if (overwrite_) {
  28. return "label \"" + label_ + "\" = " + text_ + Configuration::overwrite_identifier_;
  29. }
  30. return "label \"" + label_ + "\" = " + text_ + Configuration::configuration_identifier_;
  31. }
  32. std::string Formula::createExpression() const {
  33. if (overwrite_) {
  34. return "formula " + formula_ + " = " + content_ + Configuration::overwrite_identifier_;
  35. }
  36. return "formula " + formula_ + " = " + content_ + Configuration::configuration_identifier_;
  37. }
  38. std::string Action::createExpression() const {
  39. if (overwrite_) {
  40. return action_ + "\t" + guard_ + "-> " + update_ + Configuration::overwrite_identifier_;
  41. }
  42. return "\t" + action_ + "\t" + guard_ + "-> " + update_+ Configuration::configuration_identifier_;
  43. }
  44. std::string Constant::createExpression() const {
  45. if (overwrite_) {
  46. return "const " + type_ + " " + constant_ + " = " + value_ + Configuration::overwrite_identifier_;
  47. }
  48. return "const " + type_ + " " + constant_ + " = " + value_ + Configuration::configuration_identifier_;
  49. }
  50. YAML::Node YAML::convert<Module>::encode(const Module& rhs) {
  51. YAML::Node node;
  52. node.push_back(rhs.module_);
  53. node.push_back(rhs.actions_);
  54. return node;
  55. }
  56. bool YAML::convert<Module>::decode(const YAML::Node& node, Module& rhs) {
  57. if (!node.Type() == NodeType::Map) {
  58. return false;
  59. }
  60. rhs.actions_ = node["actions"].as<std::vector<Action>>();
  61. rhs.module_ = node["module"].as<std::string>();
  62. return true;
  63. }
  64. YAML::Node YAML::convert<Action>::encode(const Action& rhs) {
  65. YAML::Node node;
  66. node.push_back(rhs.action_);
  67. node.push_back(rhs.guard_);
  68. node.push_back(rhs.overwrite_);
  69. node.push_back(rhs.update_);
  70. return node;
  71. }
  72. bool YAML::convert<Action>::decode(const YAML::Node& node, Action& rhs) {
  73. if (!node.Type() == NodeType::Map) {
  74. return false;
  75. }
  76. rhs.action_ = node["action"].as<std::string>();
  77. rhs.guard_ = node["guard"].as<std::string>();
  78. rhs.update_ = node["update"].as<std::string>();
  79. if (node["overwrite"]) {
  80. rhs.overwrite_ = node["overwrite"].as<bool>();
  81. }
  82. if (node["index"]) {
  83. rhs.index_ = node["index"].as<int>();
  84. }
  85. return true;
  86. }
  87. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  88. YAML::Node node;
  89. node.push_back(rhs.label_);
  90. node.push_back(rhs.text_);
  91. return node;
  92. }
  93. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  94. if (!node.Type() == NodeType::Map || !node["label"] || !node["text"]) {
  95. return false;
  96. }
  97. rhs.label_ = node["label"].as<std::string>();
  98. rhs.text_ = node["text"].as<std::string>();
  99. if (node["overwrite"]) {
  100. rhs.overwrite_ = node["overwrite"].as<bool>();
  101. }
  102. return true;
  103. }
  104. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  105. YAML::Node node;
  106. node.push_back(rhs.content_);
  107. node.push_back(rhs.formula_);
  108. node.push_back(rhs.overwrite_);
  109. return node;
  110. }
  111. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  112. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["formula"] || !node["content"]) {
  113. return false;
  114. }
  115. rhs.formula_ = node["formula"].as<std::string>();
  116. rhs.content_ = node["content"].as<std::string>();
  117. if(node["overwrite"]) {
  118. rhs.overwrite_ = node["overwrite"].as<bool>();
  119. }
  120. return true;
  121. }
  122. YAML::Node YAML::convert<Constant>::encode(const Constant& rhs) {
  123. YAML::Node node;
  124. node.push_back(rhs.constant_);
  125. node.push_back(rhs.value_);
  126. node.push_back(rhs.type_);
  127. node.push_back(rhs.overwrite_);
  128. return node;
  129. }
  130. bool YAML::convert<Constant>::decode(const YAML::Node& node, Constant& rhs) {
  131. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["constant"] || !node["type"] || !node["value"]) {
  132. return false;
  133. }
  134. rhs.constant_ = node["constant"].as<std::string>();
  135. rhs.type_ = node["type"].as<std::string>();
  136. rhs.value_ = node["value"].as<std::string>();
  137. if(node["overwrite"]) {
  138. rhs.overwrite_ = node["overwrite"].as<bool>();
  139. }
  140. return true;
  141. }
  142. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  143. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  144. std::vector<Configuration> YamlConfigParser::parseConfiguration() {
  145. std::vector<Configuration> configuration;
  146. try {
  147. YAML::Node config = YAML::LoadFile(file_);
  148. std::vector<Label> labels;
  149. std::vector<Formula> formulas;
  150. std::vector<Module> modules;
  151. std::vector<Constant> constants;
  152. if (config["labels"]) {
  153. labels = config["labels"].as<std::vector<Label>>();
  154. }
  155. if (config["formulas"]) {
  156. formulas = config["formulas"].as<std::vector<Formula>>();
  157. }
  158. if (config["modules"]) {
  159. modules = config["modules"].as<std::vector<Module>>();
  160. }
  161. if (config["constants"]) {
  162. constants = config["constants"].as<std::vector<Constant>>();
  163. }
  164. for (auto& label : labels) {
  165. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  166. }
  167. for (auto& formula : formulas) {
  168. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  169. }
  170. for (auto& module : modules) {
  171. for (auto& action : module.actions_) {
  172. configuration.push_back({action.createExpression(), action.action_, ConfigType::Module, action.overwrite_, module.module_, action.index_});
  173. }
  174. }
  175. for (auto& constant : constants) {
  176. // std::cout << constant.constant_ << std::endl;
  177. configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_});
  178. }
  179. }
  180. catch(const std::exception& e) {
  181. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  182. std::cout << "\t" << e.what() << std::endl;
  183. std::cout << "while parsing configuration " << file_ << std::endl;
  184. }
  185. return configuration;
  186. }