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.

186 lines
4.4 KiB

3 months ago
  1. #pragma once
  2. #include <vector>
  3. #include <ostream>
  4. #include <utility>
  5. #include "yaml-cpp/yaml.h"
  6. enum class ConfigType : char {
  7. Label = 'L',
  8. Formula = 'F',
  9. Module = 'M',
  10. UpdateOnly = 'U',
  11. GuardOnly = 'G',
  12. Constant = 'C'
  13. };
  14. struct Configuration
  15. {
  16. static const std::string overwrite_identifier_;
  17. static const std::string configuration_identifier_;
  18. std::string module_ {};
  19. std::string expression_{};
  20. std::string identifier_{};
  21. std::string end_identifier_{};
  22. std::vector<int> indexes_{0};
  23. ConfigType type_ {ConfigType::Label};
  24. bool overwrite_ {false};
  25. Configuration() = default;
  26. Configuration(std::string expression
  27. , std::string identifier
  28. , ConfigType type
  29. , bool overwrite = false
  30. , std::string module = ""
  31. , std::vector<int> indexes = {0}
  32. , std::string end_identifier = {";"}) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, indexes_(indexes), end_identifier_{end_identifier} {}
  33. ~Configuration() = default;
  34. Configuration(const Configuration&) = default;
  35. friend std::ostream& operator << (std::ostream& os, const Configuration& config) {
  36. os << "Configuration with Type: " << static_cast<char>(config.type_) << std::endl;
  37. return os << "\tExpression=" << config.expression_ << std::endl;
  38. }
  39. };
  40. struct Property {
  41. Property() = default;
  42. Property(const Property&) = default;
  43. ~Property() = default;
  44. std::string property;
  45. double value_;
  46. std::string value_str_;
  47. friend std::ostream& operator <<(std::ostream& os, const Property& property);
  48. };
  49. struct Constant {
  50. private:
  51. public:
  52. std::string constant_;
  53. std::string type_;
  54. std::string value_;
  55. bool overwrite_{false};
  56. std::string createExpression() const;
  57. friend std::ostream& operator <<(std::ostream &os, const Constant& constant);
  58. };
  59. struct Label {
  60. private:
  61. public:
  62. std::string text_;
  63. std::string label_;
  64. bool overwrite_{false};
  65. std::string createExpression() const;
  66. friend std::ostream& operator <<(std::ostream &os, const Label& label);
  67. };
  68. struct Formula {
  69. private:
  70. public:
  71. std::string formula_;
  72. std::string content_;
  73. bool overwrite_ {false};
  74. std::string createExpression() const;
  75. friend std::ostream& operator << (std::ostream &os, const Formula& formula);
  76. };
  77. struct Command {
  78. public:
  79. std::string action_;
  80. std::string guard_;
  81. std::string update_;
  82. std::vector<int> indexes_{0};
  83. bool overwrite_ {false};
  84. std::string createExpression() const;
  85. friend std::ostream& operator << (std::ostream& os, const Command& command);
  86. };
  87. struct Module {
  88. public:
  89. std::vector<Command> commands_;
  90. std::string module_;
  91. std::string module_text_;
  92. bool overwrite_module{false};
  93. friend std::ostream& operator << (std::ostream& os, const Module& module);
  94. };
  95. template<>
  96. struct YAML::convert<Module> {
  97. static YAML::Node encode(const Module& rhs);
  98. static bool decode(const YAML::Node& node, Module& rhs);
  99. };
  100. template<>
  101. struct YAML::convert<Command> {
  102. static YAML::Node encode(const Command& rhs);
  103. static bool decode(const YAML::Node& node, Command& rhs);
  104. };
  105. template<>
  106. struct YAML::convert<Label> {
  107. static YAML::Node encode(const Label& rhs);
  108. static bool decode(const YAML::Node& node, Label& rhs);
  109. };
  110. template<>
  111. struct YAML::convert<Formula> {
  112. static YAML::Node encode(const Formula& rhs);
  113. static bool decode(const YAML::Node& node, Formula& rhs);
  114. };
  115. template<>
  116. struct YAML::convert<Constant> {
  117. static YAML::Node encode(const Constant& rhs);
  118. static bool decode(const YAML::Node& node, Constant& rhs);
  119. };
  120. template<>
  121. struct YAML::convert<Property> {
  122. static YAML::Node encode(const Property& rhs);
  123. static bool decode(const YAML::Node& node, Property& rhs);
  124. };
  125. struct YamlConfigParseResult {
  126. YamlConfigParseResult(std::vector<Configuration> configurations, std::vector<Property> probabilities)
  127. : configurations_(configurations), properties_(probabilities) {}
  128. ~YamlConfigParseResult() = default;
  129. YamlConfigParseResult(const YamlConfigParseResult&) = default;
  130. std::vector<Configuration> configurations_;
  131. std::vector<Property> properties_;
  132. };
  133. struct YamlConfigParser {
  134. public:
  135. YamlConfigParser(std::string file) : file_(file) {}
  136. YamlConfigParser(const YamlConfigParser&) = delete;
  137. ~YamlConfigParser() = default;
  138. YamlConfigParseResult parseConfiguration();
  139. private:
  140. std::string file_;
  141. };