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.

218 lines
11 KiB

  1. #include "PrismFormulaPrinter.h"
  2. #include <map>
  3. #include <string>
  4. #include <algorithm>
  5. std::string oneOffToString(const int &offset) {
  6. return offset != 0 ? ( offset == 1 ? "+1" : "-1" ) : "";
  7. }
  8. std::string vectorToDisjunction(const std::vector<std::string> &formulae) {
  9. bool first = true;
  10. std::string disjunction = "";
  11. for(const auto &formula : formulae) {
  12. if(first) first = false;
  13. else disjunction += " | ";
  14. disjunction += formula;
  15. }
  16. return disjunction;
  17. }
  18. std::string cellToConjunction(const AgentName &agentName, const cell &c) {
  19. return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row);
  20. }
  21. std::string cellToConjunctionWithOffset(const AgentName &agentName, const cell &c, const std::string &xOffset, const std::string &yOffset){
  22. return "x" + agentName + xOffset + "=" + std::to_string(c.column) + "&y" + agentName + yOffset + "=" + std::to_string(c.row);
  23. }
  24. std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) {
  25. return "x" + agentName + "=" + std::to_string(c.first) + "&y" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection);
  26. }
  27. std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair<int, int> &relativePosition) {
  28. std::string xOffset = oneOffToString(relativePosition.first);
  29. std::string yOffset = oneOffToString(relativePosition.second);
  30. return "x" + agentName + xOffset + "=x" + identifier + "&y" + agentName + yOffset + "=y" + identifier;
  31. }
  32. std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair<int, int> &relativePosition, const ViewDirection viewDirection) {
  33. std::string xOffset = oneOffToString(relativePosition.first);
  34. std::string yOffset = oneOffToString(relativePosition.second);
  35. return "x" + agentName + xOffset + "=x" + identifier + "&y" + agentName + yOffset + "=y" + identifier + "&view" + agentName + "=" + std::to_string(viewDirection);
  36. }
  37. std::map<ViewDirection, coordinates> getAdjacentCells(const cell &c) {
  38. return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}};
  39. }
  40. std::map<ViewDirection, std::pair<int, int>> getRelativeAdjacentCells() {
  41. return { {1, {0,+1}}, {2, {-1,0}}, {3, {0,-1}}, {0, {+1,0}} };
  42. }
  43. std::map<std::string, std::pair<int, int>> getRelativeSurroundingCells() {
  44. return { {"NorthWest", {-1,-1}}, {"North", { 0,-1}}, {"NorthEast", {+1,-1}},
  45. {"West", {-1, 0}}, {"East", {+1, 0}},
  46. {"SouthWest", {-1,+1}}, {"South", { 0,+1}}, {"SouthEast", {+1,+1}} };
  47. }
  48. namespace prism {
  49. PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map<std::string, cells> &restrictions, const cells &walls, 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)
  50. : os(os), restrictions(restrictions), walls(walls), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals)
  51. { }
  52. void PrismFormulaPrinter::print(const AgentName &agentName) {
  53. for(const auto& [direction, cells] : restrictions) {
  54. printRestrictionFormula(agentName, direction, cells);
  55. }
  56. if(slipperyBehaviour()) {
  57. for(const auto& [direction, cells] : slipperyTiles) {
  58. printIsOnFormula(agentName, "Slippery", cells, direction);
  59. }
  60. std::vector<std::string> allSlipperyDirections = {agentName + "IsOnSlipperyNorth", agentName + "IsOnSlipperyEast", agentName + "IsOnSlipperySouth", agentName + "IsOnSlipperyWest"};
  61. os << buildFormula(agentName + "IsOnSlippery", vectorToDisjunction(allSlipperyDirections));
  62. for(const auto& [direction, relativePosition] : getRelativeSurroundingCells()) {
  63. printSlipRestrictionFormula(agentName, direction);
  64. }
  65. }
  66. printIsOnFormula(agentName, "Lava", lava);
  67. printIsOnFormula(agentName, "Goal", goals);
  68. for(const auto& ball : balls) {
  69. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  70. printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  71. portableObjects.push_back(agentName + "Carrying" + identifier);
  72. }
  73. for(const auto& box : boxes) {
  74. std::string identifier = capitalize(box.getColor()) + box.getType();
  75. printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  76. portableObjects.push_back(agentName + "Carrying" + identifier);
  77. }
  78. for(const auto& key : keys) {
  79. std::string identifier = capitalize(key.getColor()) + key.getType();
  80. printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  81. portableObjects.push_back(agentName + "Carrying" + identifier);
  82. }
  83. for(const auto& door : unlockedDoors) {
  84. std::string identifier = capitalize(door.getColor()) + door.getType();
  85. printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open");
  86. printIsNextToFormula(agentName, identifier, getAdjacentCells(door));
  87. }
  88. for(const auto& door : lockedDoors) {
  89. std::string identifier = capitalize(door.getColor()) + door.getType();
  90. printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open");
  91. printIsNextToFormula(agentName, identifier, getAdjacentCells(door));
  92. }
  93. if(conditionalMovementRestrictions.size() > 0) {
  94. os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions));
  95. os << buildFormula(agentName + "IsCarrying", vectorToDisjunction(portableObjects));
  96. }
  97. }
  98. void PrismFormulaPrinter::printRestrictionFormula(const AgentName &agentName, const std::string &direction, const cells &grid_cells) {
  99. os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells));
  100. }
  101. void PrismFormulaPrinter::printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction) {
  102. os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells));
  103. }
  104. void PrismFormulaPrinter::printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map<ViewDirection, coordinates> &coordinates) {
  105. os << buildFormula(agentName + "IsNextTo" + type, buildDisjunction(agentName, coordinates));
  106. }
  107. void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map<ViewDirection, coordinates> &coordinates, const std::string &condition) {
  108. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition);
  109. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  110. }
  111. void PrismFormulaPrinter::printRelativeRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::string &condition) {
  112. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, reason) + ") & " + condition);
  113. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  114. }
  115. void PrismFormulaPrinter::printSlipRestrictionFormula(const AgentName &agentName, const std::string &direction) {
  116. std::pair<int, int> slipCell = getRelativeSurroundingCells().at(direction);
  117. bool semicolon = anyPortableObject() ? false : true;
  118. os << buildFormula(agentName + "CannotSlip" + direction, buildDisjunction(agentName, walls, slipCell), semicolon);
  119. for(auto const key : keys) {
  120. std::string identifier = capitalize(key.getColor()) + key.getType();
  121. os << " | " << objectPositionToConjunction(agentName, identifier, slipCell);
  122. }
  123. for(auto const ball : balls) {
  124. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  125. os << " | " << objectPositionToConjunction(agentName, identifier, slipCell);
  126. }
  127. for(auto const box : boxes) {
  128. std::string identifier = capitalize(box.getColor()) + box.getType();
  129. os << " | " << objectPositionToConjunction(agentName, identifier, slipCell);
  130. }
  131. os << ";\n";
  132. }
  133. std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula, const bool semicolon) {
  134. return "formula " + formulaName + " = " + formula + (semicolon ? ";\n": "");
  135. }
  136. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map<ViewDirection, coordinates> &cells) {
  137. if(cells.size() == 0) return "false";
  138. bool first = true;
  139. std::string disjunction = "";
  140. for(const auto [viewDirection, coordinates] : cells) {
  141. if(first) first = false;
  142. else disjunction += " | ";
  143. disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")";
  144. }
  145. return disjunction;
  146. }
  147. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells) {
  148. if(cells.size() == 0) return "false";
  149. bool first = true;
  150. std::string disjunction = "";
  151. for(auto const cell : cells) {
  152. if(first) first = false;
  153. else disjunction += " | ";
  154. disjunction += "(" + cellToConjunction(agentName, cell) + ")";
  155. }
  156. return disjunction;
  157. }
  158. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::string &reason) {
  159. std::string disjunction = "";
  160. bool first = true;
  161. for(auto const [viewDirection, relativePosition] : getRelativeAdjacentCells()) {
  162. if(first) first = false;
  163. else disjunction += " | ";
  164. disjunction += "(" + objectPositionToConjunction(agentName, reason, relativePosition, viewDirection) + ")";
  165. }
  166. return disjunction;
  167. }
  168. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::pair<int, int> &offset) {
  169. std::string disjunction = "";
  170. bool first = true;
  171. std::string xOffset = oneOffToString(offset.first);
  172. std::string yOffset = oneOffToString(offset.second);
  173. for(auto const cell : cells) {
  174. if(first) first = false;
  175. else disjunction += " | ";
  176. disjunction += "(" + cellToConjunctionWithOffset(agentName, cell, xOffset, yOffset) + ")";
  177. }
  178. return disjunction;
  179. }
  180. bool PrismFormulaPrinter::slipperyBehaviour() const {
  181. return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty();
  182. }
  183. bool PrismFormulaPrinter::anyPortableObject() const {
  184. return !keys.empty() || !boxes.empty() || !balls.empty();
  185. }
  186. }