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.

168 lines
6.0 KiB

  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::map<coordinates, float> stateRewards;
  97. float faultyProbability = 0.1;
  98. float probIntended = 0.9;
  99. try {
  100. bool ok = phrase_parse(contentIter, contentLast, contentParser, qi::space, contentCells);
  101. // TODO if(background is not empty) {
  102. ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells);
  103. // TODO }
  104. if (configFilename->is_set()) {
  105. YamlConfigParser parser(configFilename->value(0));
  106. configurations = parser.parseConfiguration();
  107. }
  108. boost::escaped_list_separator<char> seps('\\', ';', '\n');
  109. Tokenizer csvParser(rewards, seps);
  110. for(auto iter = csvParser.begin(); iter != csvParser.end(); ++iter) {
  111. int x = std::stoi(*iter);
  112. int y = std::stoi(*(++iter));
  113. float reward = std::stof(*(++iter));
  114. stateRewards[std::make_pair(x,y)] = reward;
  115. }
  116. if (!properties.empty()) {
  117. auto faultProbabilityIdentifier = std::string("FaultProbability:");
  118. auto start_pos = properties.find(faultProbabilityIdentifier);
  119. if (start_pos != std::string::npos) {
  120. auto end_pos = properties.find('\n', start_pos);
  121. auto value = properties.substr(start_pos + faultProbabilityIdentifier.length(), end_pos - start_pos - faultProbabilityIdentifier.length());
  122. faultyProbability = std::stod(value);
  123. }
  124. }
  125. if(ok) {
  126. Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, probIntended, faultyProbability);
  127. grid.printToPrism(std::cout, configurations , gridOptions.getModelType());
  128. std::stringstream ss;
  129. // grid.printToPrism(file, configurations ,prism::ModelType::MDP);
  130. grid.printToPrism(ss, configurations , gridOptions.getModelType());
  131. std::string str = ss.str();
  132. grid.applyOverwrites(str, configurations);
  133. file << str;
  134. }
  135. } catch(qi::expectation_failure<pos_iterator_t> const& e) {
  136. std::cout << "expected: "; print_info(e.what_);
  137. std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
  138. std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
  139. } catch(const std::exception& e) {
  140. std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  141. std::cerr << "\t" << e.what() << std::endl;
  142. std::exit(EXIT_FAILURE);
  143. }
  144. return 0;
  145. }