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.

267 lines
8.1 KiB

11 months ago
11 months ago
11 months ago
  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 Command& command) {
  12. os << command.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& command : module.commands_) {
  22. os << command << 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 Command::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.commands_);
  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.commands_ = node["commands"].as<std::vector<Command>>();
  61. rhs.module_ = node["module"].as<std::string>();
  62. return true;
  63. }
  64. YAML::Node YAML::convert<Command>::encode(const Command& 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<Command>::decode(const YAML::Node& node, Command& 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. try {
  84. rhs.indexes_ = node["index"].as<std::vector<int>>();
  85. }
  86. catch(const std::exception& e) {
  87. rhs.indexes_ = {node["index"].as<int>()};
  88. }
  89. }
  90. return true;
  91. }
  92. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  93. YAML::Node node;
  94. node.push_back(rhs.label_);
  95. node.push_back(rhs.text_);
  96. return node;
  97. }
  98. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  99. if (!node.Type() == NodeType::Map || !node["label"] || !node["text"]) {
  100. return false;
  101. }
  102. rhs.label_ = node["label"].as<std::string>();
  103. rhs.text_ = node["text"].as<std::string>();
  104. if (node["overwrite"]) {
  105. rhs.overwrite_ = node["overwrite"].as<bool>();
  106. }
  107. return true;
  108. }
  109. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  110. YAML::Node node;
  111. node.push_back(rhs.content_);
  112. node.push_back(rhs.formula_);
  113. node.push_back(rhs.overwrite_);
  114. return node;
  115. }
  116. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  117. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["formula"] || !node["content"]) {
  118. return false;
  119. }
  120. rhs.formula_ = node["formula"].as<std::string>();
  121. rhs.content_ = node["content"].as<std::string>();
  122. if(node["overwrite"]) {
  123. rhs.overwrite_ = node["overwrite"].as<bool>();
  124. }
  125. return true;
  126. }
  127. YAML::Node YAML::convert<Constant>::encode(const Constant& rhs) {
  128. YAML::Node node;
  129. node.push_back(rhs.constant_);
  130. node.push_back(rhs.value_);
  131. node.push_back(rhs.type_);
  132. node.push_back(rhs.overwrite_);
  133. return node;
  134. }
  135. bool YAML::convert<Constant>::decode(const YAML::Node& node, Constant& rhs) {
  136. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["constant"] || !node["type"] || !node["value"]) {
  137. return false;
  138. }
  139. rhs.constant_ = node["constant"].as<std::string>();
  140. rhs.type_ = node["type"].as<std::string>();
  141. rhs.value_ = node["value"].as<std::string>();
  142. if(node["overwrite"]) {
  143. rhs.overwrite_ = node["overwrite"].as<bool>();
  144. }
  145. return true;
  146. }
  147. YAML::Node YAML::convert<Probability>::encode(const Probability& rhs) {
  148. YAML::Node node;
  149. node.push_back(rhs.probability_);
  150. node.push_back(rhs.value_);
  151. return node;
  152. }
  153. bool YAML::convert<Probability>::decode(const YAML::Node& node, Probability& rhs) {
  154. if (!node.IsDefined() || !node["probability"] || !node["value"]) {
  155. return false;
  156. }
  157. rhs.probability_ = node["probability"].as<std::string>();
  158. rhs.value_ = node["value"].as<double>();
  159. return true;
  160. }
  161. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  162. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  163. YamlConfigParseResult YamlConfigParser::parseConfiguration() {
  164. std::vector<Configuration> configuration;
  165. std::vector<Probability> probabilities;
  166. try {
  167. YAML::Node config = YAML::LoadFile(file_);
  168. std::vector<Label> labels;
  169. std::vector<Formula> formulas;
  170. std::vector<Module> modules;
  171. std::vector<Constant> constants;
  172. if (config["labels"]) {
  173. labels = config["labels"].as<std::vector<Label>>();
  174. }
  175. if (config["formulas"]) {
  176. formulas = config["formulas"].as<std::vector<Formula>>();
  177. }
  178. if (config["modules"]) {
  179. modules = config["modules"].as<std::vector<Module>>();
  180. }
  181. if (config["constants"]) {
  182. constants = config["constants"].as<std::vector<Constant>>();
  183. }
  184. if (config["probabilities"]) {
  185. probabilities = config["probabilities"].as<std::vector<Probability>>();
  186. }
  187. for (auto& label : labels) {
  188. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  189. }
  190. for (auto& formula : formulas) {
  191. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  192. }
  193. for (auto& module : modules) {
  194. for (auto& command : module.commands_) {
  195. configuration.push_back({command.createExpression(), command.action_, ConfigType::Module, command.overwrite_, module.module_, command.indexes_});
  196. }
  197. }
  198. for (auto& constant : constants) {
  199. // std::cout << constant.constant_ << std::endl;
  200. configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_});
  201. }
  202. }
  203. catch(const std::exception& e) {
  204. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  205. std::cout << "\t" << e.what() << std::endl;
  206. std::cout << "while parsing configuration " << file_ << std::endl;
  207. }
  208. return YamlConfigParseResult(configuration, probabilities);
  209. }