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.

268 lines
13 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 "col" + agentName + "=" + std::to_string(c.column) + "&row" + 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 "col" + agentName + xOffset + "=" + std::to_string(c.column) + "&row" + agentName + yOffset + "=" + std::to_string(c.row);
  23. }
  24. std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) {
  25. return "col" + agentName + "=" + std::to_string(c.first) + "&row" + 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 "col" + agentName + xOffset + "=col" + identifier + "&row" + agentName + yOffset + "=row" + 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 "col" + agentName + xOffset + "=col" + identifier + "&row" + agentName + yOffset + "=row" + 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, const AgentNameAndPositionMap &agentNameAndPositionMap, const bool faulty)
  50. : os(os), restrictions(restrictions), walls(walls), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals), agentNameAndPositionMap(agentNameAndPositionMap), faulty(faulty)
  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. } else {
  66. os << buildFormula(agentName + "IsOnSlippery", "false");
  67. }
  68. if(!lava.empty()) printIsOnFormula(agentName, "Lava", lava);
  69. if(!goals.empty()) printIsOnFormula(agentName, "Goal", goals);
  70. for(const auto& ball : balls) {
  71. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  72. printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  73. portableObjects.push_back(agentName + "Carrying" + identifier);
  74. }
  75. for(const auto& box : boxes) {
  76. std::string identifier = capitalize(box.getColor()) + box.getType();
  77. printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  78. portableObjects.push_back(agentName + "Carrying" + identifier);
  79. }
  80. for(const auto& key : keys) {
  81. std::string identifier = capitalize(key.getColor()) + key.getType();
  82. printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  83. portableObjects.push_back(agentName + "Carrying" + identifier);
  84. }
  85. for(const auto& door : unlockedDoors) {
  86. std::string identifier = capitalize(door.getColor()) + door.getType();
  87. printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open");
  88. printIsNextToFormula(agentName, identifier, getAdjacentCells(door));
  89. }
  90. for(const auto& door : lockedDoors) {
  91. std::string identifier = capitalize(door.getColor()) + door.getType();
  92. printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open");
  93. printIsNextToFormula(agentName, identifier, getAdjacentCells(door));
  94. }
  95. if(conditionalMovementRestrictions.size() > 0) {
  96. os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions));
  97. os << buildFormula(agentName + "IsCarrying", vectorToDisjunction(portableObjects));
  98. }
  99. }
  100. void PrismFormulaPrinter::printRestrictionFormula(const AgentName &agentName, const std::string &direction, const cells &grid_cells) {
  101. os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells));
  102. }
  103. void PrismFormulaPrinter::printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction) {
  104. os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells));
  105. }
  106. void PrismFormulaPrinter::printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map<ViewDirection, coordinates> &coordinates) {
  107. os << buildFormula(agentName + "IsNextTo" + type, buildDisjunction(agentName, coordinates));
  108. }
  109. void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map<ViewDirection, coordinates> &coordinates, const std::string &condition) {
  110. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition);
  111. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  112. }
  113. void PrismFormulaPrinter::printRelativeRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::string &condition) {
  114. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, reason) + ") & " + condition);
  115. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  116. }
  117. void PrismFormulaPrinter::printSlipRestrictionFormula(const AgentName &agentName, const std::string &direction) {
  118. std::pair<int, int> slipCell = getRelativeSurroundingCells().at(direction);
  119. bool semicolon = anyPortableObject() ? false : true;
  120. os << buildFormula(agentName + "CannotSlip" + direction, buildDisjunction(agentName, walls, slipCell), semicolon);
  121. for(auto const key : keys) {
  122. std::string identifier = capitalize(key.getColor()) + key.getType();
  123. os << " | " << objectPositionToConjunction(agentName, identifier, slipCell);
  124. }
  125. for(auto const ball : balls) {
  126. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  127. os << " | " << objectPositionToConjunction(agentName, identifier, slipCell);
  128. }
  129. for(auto const box : boxes) {
  130. std::string identifier = capitalize(box.getColor()) + box.getType();
  131. os << " | " << objectPositionToConjunction(agentName, identifier, slipCell);
  132. }
  133. if(!semicolon) os << ";\n";
  134. }
  135. void PrismFormulaPrinter::printCollisionFormula(const AgentName &agentName) {
  136. if(!agentNameAndPositionMap.empty()) {
  137. os << "formula collision = ";
  138. bool first = true;
  139. for(auto const [name, coordinates] : agentNameAndPositionMap) {
  140. if(name == agentName) continue;
  141. if(first) first = false;
  142. else os << " | ";
  143. os << "(col"+agentName+"=col"+name+"&row"+agentName+"=row"+name+")";
  144. }
  145. os << ";\n";
  146. printCollisionLabel();
  147. }
  148. }
  149. void PrismFormulaPrinter::printCollisionLabel() {
  150. if(!agentNameAndPositionMap.empty()) {
  151. os << "label \"collision\" = collision;\n";
  152. }
  153. }
  154. void PrismFormulaPrinter::printInitStruct() {
  155. os << "init\n";
  156. bool first = true;
  157. for(auto const [a, coordinates] : agentNameAndPositionMap) {
  158. if(first) first = false;
  159. else os << " & ";
  160. os << "(col"+a+"="+std::to_string(coordinates.first)+"&row"+a+"="+std::to_string(coordinates.second)+" & ";
  161. os << "(view"+a+"=0|view"+a+"=1|view"+a+"=2|view"+a+"=3) ";
  162. if(faulty) os << " & previousAction"+a+"="+std::to_string(NOFAULT);
  163. os << ")";
  164. }
  165. for(auto const ball : balls) {
  166. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  167. os << " & (col"+identifier+"="+std::to_string(ball.column)+"&row"+identifier+"="+std::to_string(ball.row)+") ";
  168. }
  169. for(auto const key : keys) {
  170. std::string identifier = capitalize(key.getColor()) + key.getType();
  171. os << " & (col"+identifier+"="+std::to_string(key.column)+"&row"+identifier+"="+std::to_string(key.row)+") ";
  172. }
  173. for(auto const box : boxes) {
  174. std::string identifier = capitalize(box.getColor()) + box.getType();
  175. os << " & (col"+identifier+"="+std::to_string(box.column)+"&row"+identifier+"="+std::to_string(box.row)+") ";
  176. }
  177. os << "endinit\n\n";
  178. }
  179. std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula, const bool semicolon) {
  180. return "formula " + formulaName + " = " + formula + (semicolon ? ";\n": "");
  181. }
  182. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map<ViewDirection, coordinates> &cells) {
  183. if(cells.size() == 0) return "false";
  184. bool first = true;
  185. std::string disjunction = "";
  186. for(const auto [viewDirection, coordinates] : cells) {
  187. if(first) first = false;
  188. else disjunction += " | ";
  189. disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")";
  190. }
  191. return disjunction;
  192. }
  193. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells) {
  194. if(cells.size() == 0) return "false";
  195. bool first = true;
  196. std::string disjunction = "";
  197. for(auto const cell : cells) {
  198. if(first) first = false;
  199. else disjunction += " | ";
  200. disjunction += "(" + cellToConjunction(agentName, cell) + ")";
  201. }
  202. return disjunction;
  203. }
  204. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::string &reason) {
  205. std::string disjunction = "";
  206. bool first = true;
  207. for(auto const [viewDirection, relativePosition] : getRelativeAdjacentCells()) {
  208. if(first) first = false;
  209. else disjunction += " | ";
  210. disjunction += "(" + objectPositionToConjunction(agentName, reason, relativePosition, viewDirection) + ")";
  211. }
  212. return disjunction;
  213. }
  214. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::pair<int, int> &offset) {
  215. std::string disjunction = "";
  216. bool first = true;
  217. std::string xOffset = oneOffToString(offset.first);
  218. std::string yOffset = oneOffToString(offset.second);
  219. for(auto const cell : cells) {
  220. if(first) first = false;
  221. else disjunction += " | ";
  222. disjunction += "(" + cellToConjunctionWithOffset(agentName, cell, xOffset, yOffset) + ")";
  223. }
  224. return disjunction;
  225. }
  226. bool PrismFormulaPrinter::slipperyBehaviour() const {
  227. return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty();
  228. }
  229. bool PrismFormulaPrinter::anyPortableObject() const {
  230. return !keys.empty() || !boxes.empty() || !balls.empty();
  231. }
  232. }