The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

303 lines
9.5 KiB

2 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.module_ = node["module"].as<std::string>();
  61. if (node["commands"]) {
  62. rhs.commands_ = node["commands"].as<std::vector<Command>>();
  63. }
  64. if (node["module_text"]) {
  65. rhs.module_text_ = node["module_text"].as<std::string>();
  66. }
  67. if (node["overwrite"]) {
  68. rhs.overwrite_module = node["overwrite"].as<bool>();
  69. }
  70. return true;
  71. }
  72. YAML::Node YAML::convert<Command>::encode(const Command& rhs) {
  73. YAML::Node node;
  74. node.push_back(rhs.action_);
  75. node.push_back(rhs.guard_);
  76. node.push_back(rhs.overwrite_);
  77. node.push_back(rhs.update_);
  78. return node;
  79. }
  80. bool YAML::convert<Command>::decode(const YAML::Node& node, Command& rhs) {
  81. if (!node.Type() == NodeType::Map) {
  82. return false;
  83. }
  84. rhs.action_ = node["action"].as<std::string>();
  85. if (node["guard"]) {
  86. rhs.guard_ = node["guard"].as<std::string>();
  87. }
  88. if (node["update"]) {
  89. rhs.update_ = node["update"].as<std::string>();
  90. }
  91. if (node["overwrite"]) {
  92. rhs.overwrite_ = node["overwrite"].as<bool>();
  93. }
  94. if (node["index"]) {
  95. try {
  96. rhs.indexes_ = node["index"].as<std::vector<int>>();
  97. }
  98. catch(const std::exception& e) {
  99. rhs.indexes_ = {node["index"].as<int>()};
  100. }
  101. }
  102. return true;
  103. }
  104. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  105. YAML::Node node;
  106. node.push_back(rhs.label_);
  107. node.push_back(rhs.text_);
  108. return node;
  109. }
  110. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  111. if (!node.Type() == NodeType::Map || !node["label"] || !node["text"]) {
  112. return false;
  113. }
  114. rhs.label_ = node["label"].as<std::string>();
  115. rhs.text_ = node["text"].as<std::string>();
  116. if (node["overwrite"]) {
  117. rhs.overwrite_ = node["overwrite"].as<bool>();
  118. }
  119. return true;
  120. }
  121. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  122. YAML::Node node;
  123. node.push_back(rhs.content_);
  124. node.push_back(rhs.formula_);
  125. node.push_back(rhs.overwrite_);
  126. return node;
  127. }
  128. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  129. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["formula"] || !node["content"]) {
  130. return false;
  131. }
  132. rhs.formula_ = node["formula"].as<std::string>();
  133. rhs.content_ = node["content"].as<std::string>();
  134. if(node["overwrite"]) {
  135. rhs.overwrite_ = node["overwrite"].as<bool>();
  136. }
  137. return true;
  138. }
  139. YAML::Node YAML::convert<Constant>::encode(const Constant& rhs) {
  140. YAML::Node node;
  141. node.push_back(rhs.constant_);
  142. node.push_back(rhs.value_);
  143. node.push_back(rhs.type_);
  144. node.push_back(rhs.overwrite_);
  145. return node;
  146. }
  147. bool YAML::convert<Constant>::decode(const YAML::Node& node, Constant& rhs) {
  148. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["constant"] || !node["type"] || !node["value"]) {
  149. return false;
  150. }
  151. rhs.constant_ = node["constant"].as<std::string>();
  152. rhs.type_ = node["type"].as<std::string>();
  153. rhs.value_ = node["value"].as<std::string>();
  154. if(node["overwrite"]) {
  155. rhs.overwrite_ = node["overwrite"].as<bool>();
  156. }
  157. return true;
  158. }
  159. YAML::Node YAML::convert<Property>::encode(const Property& rhs) {
  160. YAML::Node node;
  161. node.push_back(rhs.property);
  162. node.push_back(rhs.value_);
  163. return node;
  164. }
  165. bool YAML::convert<Property>::decode(const YAML::Node& node, Property& rhs) {
  166. if (!node.IsDefined() || !node["property"] || !node["value"]) {
  167. return false;
  168. }
  169. rhs.property = node["property"].as<std::string>();
  170. try {
  171. rhs.value_ = node["value"].as<double>();
  172. }
  173. catch(const std::exception& e) {
  174. rhs.value_str_ = node["value"].as<std::string>();
  175. }
  176. return true;
  177. }
  178. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  179. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  180. YamlConfigParseResult YamlConfigParser::parseConfiguration() {
  181. std::vector<Configuration> configuration;
  182. std::vector<Property> properties;
  183. try {
  184. YAML::Node config = YAML::LoadFile(file_);
  185. std::vector<Label> labels;
  186. std::vector<Formula> formulas;
  187. std::vector<Module> modules;
  188. std::vector<Constant> constants;
  189. if (config["labels"]) {
  190. labels = config["labels"].as<std::vector<Label>>();
  191. }
  192. if (config["formulas"]) {
  193. formulas = config["formulas"].as<std::vector<Formula>>();
  194. }
  195. if (config["modules"]) {
  196. modules = config["modules"].as<std::vector<Module>>();
  197. }
  198. if (config["constants"]) {
  199. constants = config["constants"].as<std::vector<Constant>>();
  200. }
  201. if (config["properties"]) {
  202. properties = config["properties"].as<std::vector<Property>>();
  203. }
  204. for (auto& label : labels) {
  205. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  206. }
  207. for (auto& formula : formulas) {
  208. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  209. }
  210. for (auto& module : modules) {
  211. if (module.overwrite_module) {
  212. Configuration config = Configuration(module.module_text_, "module " + module.module_ + "\n", ConfigType::Module, true, module.module_, {0}, "endmodule");
  213. configuration.push_back(config);
  214. continue;
  215. }
  216. for (auto& command : module.commands_) {
  217. Configuration config;
  218. if (!command.guard_.empty() && !command.action_.empty() && command.update_.empty()) {
  219. config = Configuration(" " + command.guard_, command.action_, ConfigType::GuardOnly, true, module.module_, command.indexes_, "->");
  220. } else if (!command.update_.empty() && !command.action_.empty() && command.guard_.empty()) {
  221. config = Configuration( " " + command.update_, command.action_, ConfigType::UpdateOnly, true, module.module_, command.indexes_, ";");
  222. } else {
  223. config = Configuration(command.createExpression(), command.action_, ConfigType::Module, command.overwrite_, module.module_, command.indexes_);
  224. }
  225. configuration.push_back(config);
  226. }
  227. }
  228. for (auto& constant : constants) {
  229. // std::cout << constant.constant_ << std::endl;
  230. configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_});
  231. }
  232. }
  233. catch(const std::exception& e) {
  234. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  235. std::cout << "\t" << e.what() << std::endl;
  236. std::cout << "while parsing configuration " << file_ << std::endl;
  237. }
  238. return YamlConfigParseResult(configuration, properties);
  239. }