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.

129 lines
5.4 KiB

  1. #include "PrismFormulaPrinter.h"
  2. #include <map>
  3. #include <string>
  4. #include <algorithm>
  5. std::string capitalize(std::string string) {
  6. string[0] = std::toupper(string[0]);
  7. return string;
  8. }
  9. std::string vectorToDisjunction(const std::vector<std::string> &formulae) {
  10. bool first = true;
  11. std::string disjunction = "";
  12. for(const auto &formula : formulae) {
  13. if(first) first = false;
  14. else disjunction += " | ";
  15. disjunction += formula;
  16. }
  17. return disjunction;
  18. }
  19. std::string cellToConjunction(const AgentName &agentName, const cell &c) {
  20. return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row);
  21. }
  22. std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) {
  23. return "x" + agentName + "=" + std::to_string(c.first) + "&y" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection);
  24. }
  25. std::map<ViewDirection, coordinates> getSurroundingCells(const cell &c) {
  26. return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}};
  27. }
  28. namespace prism {
  29. PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map<std::string, cells> &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map<std::string, cells> &slipperyTiles, const cells &lava)
  30. : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava)
  31. { }
  32. void PrismFormulaPrinter::printFormulas() {
  33. for(const auto& [direction, cells] : restrictions) {
  34. printRestrictionFormula(direction, cells);
  35. }
  36. for(const auto& [direction, cells] : slipperyTiles) {
  37. printIsOnFormula("Slippery", cells, direction);
  38. }
  39. printIsOnFormula("Lava", lava);
  40. for(const auto& ball : balls) {
  41. std::string color = capitalize(ball.getColor());
  42. printRestrictionFormulaWithCondition(color + "Ball", getSurroundingCells(ball), "!" + color + "BallPickedUp");
  43. }
  44. for(const auto& box : boxes) {
  45. std::string color = capitalize(box.getColor());
  46. printRestrictionFormulaWithCondition(color + "Box", getSurroundingCells(box), "!" + color + "BoxPickedUp");
  47. }
  48. for(const auto& key : keys) {
  49. std::string color = capitalize(key.getColor());
  50. printRestrictionFormulaWithCondition(color + "Key", getSurroundingCells(key), "!" + color + "KeyPickedUp");
  51. }
  52. for(const auto& door : unlockedDoors) {
  53. std::string color = capitalize(door.getColor());
  54. printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened");
  55. }
  56. for(const auto& door : lockedDoors) {
  57. std::string color = capitalize(door.getColor());
  58. printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened");
  59. }
  60. if(conditionalMovementRestrictions.size() > 0) {
  61. os << buildFormula("CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions));
  62. }
  63. }
  64. void PrismFormulaPrinter::printRestrictionFormula(const std::string &direction, const cells &grid_cells) {
  65. os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells));
  66. }
  67. void PrismFormulaPrinter::printIsOnFormula(const std::string &type, const cells &grid_cells, const std::string &direction) {
  68. os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells));
  69. }
  70. void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const std::string &reason, const std::map<ViewDirection, coordinates> &coordinates, const std::string &condition) {
  71. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition);
  72. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  73. }
  74. std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) {
  75. return "formula " + formulaName + " = " + formula + ";\n";
  76. }
  77. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map<ViewDirection, coordinates> &cells) {
  78. if(cells.size() == 0) return "false";
  79. bool first = true;
  80. std::string disjunction = "";
  81. for(const auto [viewDirection, coordinates] : cells) {
  82. if(first) first = false;
  83. else disjunction += " | ";
  84. disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")";
  85. }
  86. return disjunction;
  87. }
  88. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector<std::string> &conditions) {
  89. if(cells.size() == 0) return "false";
  90. bool first = true;
  91. std::string disjunction = "";
  92. if(!conditions.empty()) {
  93. for(uint index = 0; index < cells.size(); index++) {
  94. if(first) first = false;
  95. else disjunction += " | ";
  96. disjunction += "(" + cellToConjunction(agentName, cells.at(index)) + "&" + conditions.at(index) + ")";
  97. }
  98. } else {
  99. for(auto const cell : cells) {
  100. if(first) first = false;
  101. else disjunction += " | ";
  102. disjunction += "(" + cellToConjunction(agentName, cell) + ")";
  103. }
  104. }
  105. return disjunction;
  106. }
  107. }