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.

171 lines
6.2 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. #include "util/OptionParser.h"
  2. #include "util/MinigridGrammar.h"
  3. #include "util/Grid.h"
  4. #include "util/ConfigYaml.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <filesystem>
  8. #include <sstream>
  9. std::vector<std::string> parseCommaSeparatedString(std::string const& str) {
  10. std::vector<std::string> result;
  11. std::stringstream stream(str);
  12. while(stream.good()) {
  13. std::string substr;
  14. getline(stream, substr, ',');
  15. substr.at(0) = std::toupper(substr.at(0));
  16. result.push_back(substr);
  17. }
  18. return result;
  19. }
  20. struct printer {
  21. typedef boost::spirit::utf8_string string;
  22. void element(string const& tag, string const& value, int depth) const {
  23. for (int i = 0; i < (depth*4); ++i) std::cout << ' ';
  24. std::cout << "tag: " << tag;
  25. if (value != "") std::cout << ", value: " << value;
  26. std::cout << std::endl;
  27. }
  28. };
  29. void print_info(boost::spirit::info const& what) {
  30. using boost::spirit::basic_info_walker;
  31. printer pr;
  32. basic_info_walker<printer> walker(pr, what.tag, 0);
  33. boost::apply_visitor(walker, what.value);
  34. }
  35. int main(int argc, char* argv[]) {
  36. popl::OptionParser optionParser("Allowed options");
  37. auto helpOption = optionParser.add<popl::Switch>("h", "help", "Print this help message.");
  38. auto inputFilename = optionParser.add<popl::Value<std::string>>("i", "input-file", "Filename of the input file.");
  39. auto outputFilename = optionParser.add<popl::Value<std::string>>("o", "output-file", "Filename for the output file.");
  40. auto configFilename = optionParser.add<popl::Value<std::string>, popl::Attribute::optional>("c", "config-file", "Filename of the predicate configuration file.");
  41. try {
  42. optionParser.parse(argc, argv);
  43. if(helpOption->count() > 0) {
  44. std::cout << optionParser << std::endl;
  45. return EXIT_SUCCESS;
  46. }
  47. } catch (const popl::invalid_option &e) {
  48. return io::printPoplException(e);
  49. } catch (const std::exception &e) {
  50. std::cerr << "Exception: " << e.what() << "\n";
  51. return EXIT_FAILURE;
  52. }
  53. GridOptions gridOptions = { {}, {} };
  54. std::fstream file {outputFilename->value(0), file.trunc | file.out};
  55. std::fstream infile {inputFilename->value(0), infile.in};
  56. std::string line, content, background, rewards, properties;
  57. std::cout << "\n";
  58. bool parsingBackground = false;
  59. bool parsingStateRewards = false;
  60. bool parsingEnvironmentProperties = false;
  61. while (std::getline(infile, line) && !line.empty()) {
  62. if(line.at(0) == '-' && line.at(line.size() - 1) == '-' && parsingBackground) {
  63. parsingStateRewards = true;
  64. parsingBackground = false;
  65. continue;
  66. } else if (line.at(0) == '-' && line.at(line.size() - 1 ) == '-' && parsingStateRewards) {
  67. parsingStateRewards = false;
  68. parsingEnvironmentProperties = true;
  69. continue;
  70. } else if(line.at(0) == '-' && line.at(line.size() - 1) == '-') {
  71. parsingBackground = true;
  72. continue;
  73. }
  74. if(!parsingBackground && !parsingStateRewards && !parsingEnvironmentProperties) {
  75. content += line + "\n";
  76. } else if (parsingBackground) {
  77. background += line + "\n";
  78. } else if(parsingStateRewards) {
  79. rewards += line + "\n";
  80. } else if (parsingEnvironmentProperties) {
  81. properties += line + "\n";
  82. }
  83. }
  84. std::cout << "\n";
  85. pos_iterator_t contentFirst(content.begin());
  86. pos_iterator_t contentIter = contentFirst;
  87. pos_iterator_t contentLast(content.end());
  88. MinigridParser<pos_iterator_t> contentParser(contentFirst);
  89. pos_iterator_t backgroundFirst(background.begin());
  90. pos_iterator_t backgroundIter = backgroundFirst;
  91. pos_iterator_t backgroundLast(background.end());
  92. MinigridParser<pos_iterator_t> backgroundParser(backgroundFirst);
  93. cells contentCells;
  94. cells backgroundCells;
  95. std::vector<Configuration> configurations;
  96. std::vector<Probability> probabilities;
  97. std::map<coordinates, float> stateRewards;
  98. float faultyProbability = 0.1;
  99. float probIntended = 0.9;
  100. try {
  101. bool ok = phrase_parse(contentIter, contentLast, contentParser, qi::space, contentCells);
  102. // TODO if(background is not empty) {
  103. ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells);
  104. // TODO }
  105. if (configFilename->is_set()) {
  106. YamlConfigParser parser(configFilename->value(0));
  107. auto parseResult = parser.parseConfiguration();
  108. configurations = parseResult.configurations_;
  109. probabilities = parseResult.probabilities_;
  110. }
  111. boost::escaped_list_separator<char> seps('\\', ';', '\n');
  112. Tokenizer csvParser(rewards, seps);
  113. for(auto iter = csvParser.begin(); iter != csvParser.end(); ++iter) {
  114. int x = std::stoi(*iter);
  115. int y = std::stoi(*(++iter));
  116. float reward = std::stof(*(++iter));
  117. stateRewards[std::make_pair(x,y)] = reward;
  118. }
  119. if (!properties.empty()) {
  120. auto faultProbabilityIdentifier = std::string("FaultProbability:");
  121. auto start_pos = properties.find(faultProbabilityIdentifier);
  122. if (start_pos != std::string::npos) {
  123. auto end_pos = properties.find('\n', start_pos);
  124. auto value = properties.substr(start_pos + faultProbabilityIdentifier.length(), end_pos - start_pos - faultProbabilityIdentifier.length());
  125. faultyProbability = std::stod(value);
  126. }
  127. }
  128. if(ok) {
  129. Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, probIntended, faultyProbability);
  130. grid.printToPrism(std::cout, configurations , gridOptions.getModelType());
  131. std::stringstream ss;
  132. // grid.printToPrism(file, configurations ,prism::ModelType::MDP);
  133. grid.printToPrism(ss, configurations , gridOptions.getModelType());
  134. std::string str = ss.str();
  135. grid.applyOverwrites(str, configurations);
  136. file << str;
  137. }
  138. } catch(qi::expectation_failure<pos_iterator_t> const& e) {
  139. std::cout << "expected: "; print_info(e.what_);
  140. std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
  141. std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
  142. } catch(const std::exception& e) {
  143. std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  144. std::cerr << "\t" << e.what() << std::endl;
  145. std::exit(EXIT_FAILURE);
  146. }
  147. return 0;
  148. }