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.

102 lines
4.3 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 cellToConjunction(const AgentName &agentName, const cell &c) {
  10. return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row);
  11. }
  12. std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) {
  13. return "x" + agentName + "=" + std::to_string(c.first) + "&y" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection);
  14. }
  15. std::map<ViewDirection, coordinates> getSurroundingCells(const cell &c) {
  16. return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}};
  17. }
  18. namespace prism {
  19. 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)
  20. : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys)
  21. { }
  22. void PrismFormulaPrinter::printFormulas() {
  23. for(const auto& [direction, cells] : restrictions) {
  24. printRestrictionFormula(direction, cells);
  25. }
  26. for(const auto& ball : balls) {
  27. std::string color = capitalize(ball.getColor());
  28. printRestrictionFormulaWithCondition(color + "Ball", getSurroundingCells(ball), "!" + color + "BallPickedUp");
  29. }
  30. for(const auto& box : boxes) {
  31. std::string color = capitalize(box.getColor());
  32. printRestrictionFormulaWithCondition(color + "Box", getSurroundingCells(box), "!" + color + "BoxPickedUp");
  33. }
  34. for(const auto& key : keys) {
  35. std::string color = capitalize(key.getColor());
  36. printRestrictionFormulaWithCondition(color + "Key", getSurroundingCells(key), "!" + color + "KeyPickedUp");
  37. }
  38. for(const auto& door : unlockedDoors) {
  39. std::string color = capitalize(door.getColor());
  40. printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened");
  41. }
  42. for(const auto& door : lockedDoors) {
  43. std::string color = capitalize(door.getColor());
  44. printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened");
  45. }
  46. }
  47. void PrismFormulaPrinter::printRestrictionFormula(const std::string &direction, const cells &grid_cells) {
  48. os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells));
  49. }
  50. void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const std::string &reason, const std::map<ViewDirection, coordinates> &coordinates, const std::string &condition) {
  51. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition);
  52. }
  53. std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) {
  54. return "formula " + formulaName + " = " + formula + ";\n";
  55. }
  56. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map<ViewDirection, coordinates> &cells) {
  57. bool first = true;
  58. std::string disjunction = "";
  59. for(const auto [viewDirection, coordinates] : cells) {
  60. if(first) first = false;
  61. else disjunction += " | ";
  62. disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")";
  63. }
  64. return disjunction;
  65. }
  66. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector<std::string> &conditions) {
  67. bool first = true;
  68. std::string disjunction = "";
  69. if(!conditions.empty()) {
  70. for(uint index = 0; index < cells.size(); index++) {
  71. if(first) first = false;
  72. else disjunction += " | ";
  73. disjunction += "(" + cellToConjunction(agentName, cells.at(index)) + "&" + conditions.at(index) + ")";
  74. }
  75. } else {
  76. for(auto const cell : cells) {
  77. if(first) first = false;
  78. else disjunction += " | ";
  79. disjunction += "(" + cellToConjunction(agentName, cell) + ")";
  80. }
  81. }
  82. return disjunction;
  83. }
  84. }