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.

137 lines
6.6 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 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, const cells &goals)
  26. : os(os), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals)
  27. { }
  28. void PrismFormulaPrinter::print(const AgentName &agentName) {
  29. for(const auto& [direction, cells] : restrictions) {
  30. printRestrictionFormula(agentName, direction, cells);
  31. }
  32. for(const auto& [direction, cells] : slipperyTiles) {
  33. printIsOnFormula(agentName, "Slippery", cells, direction);
  34. }
  35. std::vector<std::string> allSlipperyDirections = {agentName + "IsOnSlipperyNorth", agentName + "IsOnSlipperyEast", agentName + "IsOnSlipperySouth", agentName + "IsOnSlipperyWest"};
  36. os << buildFormula(agentName + "IsOnSlippery", vectorToDisjunction(allSlipperyDirections));
  37. printIsOnFormula(agentName, "Lava", lava);
  38. printIsOnFormula(agentName, "Goal", goals);
  39. for(const auto& ball : balls) {
  40. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  41. printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(ball), "!" + identifier + "PickedUp");
  42. portableObjects.push_back(agentName + "Carrying" + identifier);
  43. }
  44. for(const auto& box : boxes) {
  45. std::string identifier = capitalize(box.getColor()) + box.getType();
  46. printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(box), "!" + identifier + "PickedUp");
  47. portableObjects.push_back(agentName + "Carrying" + identifier);
  48. }
  49. for(const auto& key : keys) {
  50. std::string identifier = capitalize(key.getColor()) + key.getType();
  51. printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(key), "!" + identifier + "PickedUp");
  52. portableObjects.push_back(agentName + "Carrying" + identifier);
  53. }
  54. for(const auto& door : unlockedDoors) {
  55. std::string identifier = capitalize(door.getColor()) + door.getType();
  56. printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(door), "!" + identifier + "Open");
  57. printIsNextToFormula(agentName, identifier, getSurroundingCells(door));
  58. }
  59. for(const auto& door : lockedDoors) {
  60. std::string identifier = capitalize(door.getColor()) + door.getType();
  61. printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(door), "!" + identifier + "Open");
  62. printIsNextToFormula(agentName, identifier, getSurroundingCells(door));
  63. }
  64. if(conditionalMovementRestrictions.size() > 0) {
  65. os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions));
  66. os << buildFormula(agentName + "IsCarrying", vectorToDisjunction(portableObjects));
  67. }
  68. }
  69. void PrismFormulaPrinter::printRestrictionFormula(const AgentName &agentName, const std::string &direction, const cells &grid_cells) {
  70. os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells));
  71. }
  72. void PrismFormulaPrinter::printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction) {
  73. os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells));
  74. }
  75. void PrismFormulaPrinter::printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map<ViewDirection, coordinates> &coordinates) {
  76. os << buildFormula(agentName + "IsNextTo" + type, buildDisjunction(agentName, coordinates));
  77. }
  78. void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map<ViewDirection, coordinates> &coordinates, const std::string &condition) {
  79. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition);
  80. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  81. }
  82. std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) {
  83. return "formula " + formulaName + " = " + formula + ";\n";
  84. }
  85. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map<ViewDirection, coordinates> &cells) {
  86. if(cells.size() == 0) return "false";
  87. bool first = true;
  88. std::string disjunction = "";
  89. for(const auto [viewDirection, coordinates] : cells) {
  90. if(first) first = false;
  91. else disjunction += " | ";
  92. disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")";
  93. }
  94. return disjunction;
  95. }
  96. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector<std::string> &conditions) {
  97. if(cells.size() == 0) return "false";
  98. bool first = true;
  99. std::string disjunction = "";
  100. if(!conditions.empty()) {
  101. for(uint index = 0; index < cells.size(); index++) {
  102. if(first) first = false;
  103. else disjunction += " | ";
  104. disjunction += "(" + cellToConjunction(agentName, cells.at(index)) + "&" + conditions.at(index) + ")";
  105. }
  106. } else {
  107. for(auto const cell : cells) {
  108. if(first) first = false;
  109. else disjunction += " | ";
  110. disjunction += "(" + cellToConjunction(agentName, cell) + ")";
  111. }
  112. }
  113. return disjunction;
  114. }
  115. }