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.

262 lines
7.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 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. YAML::Node YAML::convert<Probability>::encode(const Probability& rhs) {
  143. YAML::Node node;
  144. node.push_back(rhs.probability_);
  145. node.push_back(rhs.value_);
  146. return node;
  147. }
  148. bool YAML::convert<Probability>::decode(const YAML::Node& node, Probability& rhs) {
  149. if (!node.IsDefined() || !node["probability"] || !node["value"]) {
  150. return false;
  151. }
  152. rhs.probability_ = node["probability"].as<std::string>();
  153. rhs.value_ = node["value"].as<double>();
  154. return true;
  155. }
  156. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  157. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  158. YamlConfigParseResult YamlConfigParser::parseConfiguration() {
  159. std::vector<Configuration> configuration;
  160. std::vector<Probability> probabilities;
  161. try {
  162. YAML::Node config = YAML::LoadFile(file_);
  163. std::vector<Label> labels;
  164. std::vector<Formula> formulas;
  165. std::vector<Module> modules;
  166. std::vector<Constant> constants;
  167. if (config["labels"]) {
  168. labels = config["labels"].as<std::vector<Label>>();
  169. }
  170. if (config["formulas"]) {
  171. formulas = config["formulas"].as<std::vector<Formula>>();
  172. }
  173. if (config["modules"]) {
  174. modules = config["modules"].as<std::vector<Module>>();
  175. }
  176. if (config["constants"]) {
  177. constants = config["constants"].as<std::vector<Constant>>();
  178. }
  179. if (config["probabilities"]) {
  180. probabilities = config["probabilities"].as<std::vector<Probability>>();
  181. }
  182. for (auto& label : labels) {
  183. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  184. }
  185. for (auto& formula : formulas) {
  186. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  187. }
  188. for (auto& module : modules) {
  189. for (auto& action : module.actions_) {
  190. configuration.push_back({action.createExpression(), action.action_, ConfigType::Module, action.overwrite_, module.module_, action.index_});
  191. }
  192. }
  193. for (auto& constant : constants) {
  194. // std::cout << constant.constant_ << std::endl;
  195. configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_});
  196. }
  197. }
  198. catch(const std::exception& e) {
  199. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  200. std::cout << "\t" << e.what() << std::endl;
  201. std::cout << "while parsing configuration " << file_ << std::endl;
  202. }
  203. return YamlConfigParseResult(configuration, probabilities);
  204. }