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.

179 lines
4.2 KiB

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