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.

185 lines
4.6 KiB

9 months ago
9 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. Constant = 'C'
  11. };
  12. struct Configuration
  13. {
  14. static const std::string overwrite_identifier_;
  15. static const std::string configuration_identifier_;
  16. std::string module_ {};
  17. std::string expression_{};
  18. std::string identifier_{};
  19. std::string end_identifier_{};
  20. std::vector<int> indexes_{0};
  21. ConfigType type_ {ConfigType::Label};
  22. bool overwrite_ {false};
  23. bool include_identifier_for_overwrite_{true};
  24. Configuration() = default;
  25. Configuration(std::string expression
  26. , std::string identifier
  27. , ConfigType type
  28. , bool overwrite = false
  29. , std::string module = ""
  30. , std::vector<int> indexes = {0}
  31. , std::string end_identifier = {";"}
  32. , bool include_identifier_for_overwrite = true) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, indexes_(indexes), end_identifier_{end_identifier}, include_identifier_for_overwrite_{include_identifier_for_overwrite} {}
  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 Probability {
  41. Probability() = default;
  42. Probability(const Probability&) = default;
  43. ~Probability() = default;
  44. std::string probability_;
  45. double value_;
  46. friend std::ostream& operator <<(std::ostream& os, const Probability& property);
  47. };
  48. struct Constant {
  49. private:
  50. public:
  51. std::string constant_;
  52. std::string type_;
  53. std::string value_;
  54. bool overwrite_{false};
  55. std::string createExpression() const;
  56. friend std::ostream& operator <<(std::ostream &os, const Constant& constant);
  57. };
  58. struct Label {
  59. private:
  60. public:
  61. std::string text_;
  62. std::string label_;
  63. bool overwrite_{false};
  64. std::string createExpression() const;
  65. friend std::ostream& operator <<(std::ostream &os, const Label& label);
  66. };
  67. struct Formula {
  68. private:
  69. public:
  70. std::string formula_;
  71. std::string content_;
  72. bool overwrite_ {false};
  73. std::string createExpression() const;
  74. friend std::ostream& operator << (std::ostream &os, const Formula& formula);
  75. };
  76. struct Command {
  77. public:
  78. std::string action_;
  79. std::string guard_;
  80. std::string update_;
  81. std::vector<int> indexes_{0};
  82. bool overwrite_ {false};
  83. std::string createExpression() const;
  84. friend std::ostream& operator << (std::ostream& os, const Command& command);
  85. };
  86. struct Module {
  87. public:
  88. std::vector<Command> commands_;
  89. std::string module_;
  90. std::string module_text_;
  91. bool overwrite_module{false};
  92. friend std::ostream& operator << (std::ostream& os, const Module& module);
  93. };
  94. template<>
  95. struct YAML::convert<Module> {
  96. static YAML::Node encode(const Module& rhs);
  97. static bool decode(const YAML::Node& node, Module& rhs);
  98. };
  99. template<>
  100. struct YAML::convert<Command> {
  101. static YAML::Node encode(const Command& rhs);
  102. static bool decode(const YAML::Node& node, Command& rhs);
  103. };
  104. template<>
  105. struct YAML::convert<Label> {
  106. static YAML::Node encode(const Label& rhs);
  107. static bool decode(const YAML::Node& node, Label& rhs);
  108. };
  109. template<>
  110. struct YAML::convert<Formula> {
  111. static YAML::Node encode(const Formula& rhs);
  112. static bool decode(const YAML::Node& node, Formula& rhs);
  113. };
  114. template<>
  115. struct YAML::convert<Constant> {
  116. static YAML::Node encode(const Constant& rhs);
  117. static bool decode(const YAML::Node& node, Constant& rhs);
  118. };
  119. template<>
  120. struct YAML::convert<Probability> {
  121. static YAML::Node encode(const Probability& rhs);
  122. static bool decode(const YAML::Node& node, Probability& rhs);
  123. };
  124. struct YamlConfigParseResult {
  125. YamlConfigParseResult(std::vector<Configuration> configurations, std::vector<Probability> probabilities)
  126. : configurations_(configurations), probabilities_(probabilities) {}
  127. ~YamlConfigParseResult() = default;
  128. YamlConfigParseResult(const YamlConfigParseResult&) = default;
  129. std::vector<Configuration> configurations_;
  130. std::vector<Probability> probabilities_;
  131. };
  132. struct YamlConfigParser {
  133. public:
  134. YamlConfigParser(std::string file) : file_(file) {}
  135. YamlConfigParser(const YamlConfigParser&) = delete;
  136. ~YamlConfigParser() = default;
  137. YamlConfigParseResult parseConfiguration();
  138. private:
  139. std::string file_;
  140. };