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.

130 lines
5.7 KiB

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