rollbacker.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
  2. # at the University of Antwerp (http://msdl.uantwerpen.be/).
  3. #
  4. # Licensed under the GNU Public License v3 (the "License");
  5. # you may not use this file except in compliance with the
  6. # License. You may obtain a copy of the License at
  7. # https://www.gnu.org/licenses/gpl-3.0.en.html
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an
  11. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the
  14. # License.
  15. from pytcore.util.infinity import INFINITY
  16. from pytcore.tcore.iterator import Iterator
  17. from pytcore.tcore.messages import TransformationException
  18. class Rollbacker(Iterator):
  19. '''
  20. Provides back-tracking capacity.
  21. '''
  22. def __init__(self, condition, max_iterations=INFINITY):
  23. '''
  24. Selects one match from the packet.
  25. @param condition: The pre-condition pattern.
  26. @param max_iterations: The maximum number of times to select.
  27. By default, this is +INFINITY.
  28. '''
  29. super(Rollbacker, self).__init__(condition, max_iterations)
  30. self.checkpoints = [] # Stack of file names
  31. def packet_in(self, packet):
  32. self.exception = None
  33. self.is_success = False
  34. try:
  35. self.establish(packet)
  36. self.is_success = True
  37. except Exception as e:
  38. self.is_success = False
  39. self.exception = TransformationException(e)
  40. self.exception.packet = packet
  41. self.exception.transformation_unit = self
  42. finally:
  43. self.iterations = 1
  44. return packet
  45. def next_in(self, packet):
  46. self.exception = None
  47. self.is_success = False
  48. if self.iterations < self.max_iterations:
  49. # If came from the same scope as the rollbacker, then just pass it over
  50. if packet.current in packet.match_sets:
  51. self.iterations += 1
  52. self.is_success = True
  53. return packet
  54. else:
  55. try:
  56. packet.set_state(self.restore())
  57. self.is_success = True
  58. except Exception as e:
  59. self.is_success = False
  60. self.excepion = TransformationException(e)
  61. self.exception.packet = packet
  62. self.exception.transformation_unit = self
  63. finally:
  64. return packet
  65. else: # self.iterations == self.max_iterations
  66. try:
  67. packet = self.restore()
  68. self.is_success = True
  69. except:
  70. self.is_success = False
  71. finally:
  72. return packet
  73. def establish(self, packet):
  74. # fileName = '%d.tc_state.%d' % (self._id, len(self.checkpoints))
  75. # with open(fileName, 'w') as storage:
  76. # pickle.dump(packet, storage)
  77. # self.checkpoints.append(fileName)
  78. self.checkpoints.append(packet.copy_state(self.condition))
  79. def restore(self):
  80. # with open(self.checkpoints[-1], 'r') as storage:
  81. # packet = pickle.load(storage)
  82. # return packet
  83. # os.remove(self.checkpoints[-1])
  84. if len(self.checkpoints) > 0:
  85. return self.checkpoints.pop()
  86. raise Exception('There are no checkpoints to restore')
  87. def discard(self):
  88. # os.remove(self.checkpoints[-1])
  89. if len(self.checkpoints) > 0:
  90. del self.checkpoints[-1]
  91. raise Exception('There are no checkpoints to discard')
  92. def discard_all(self):
  93. # for fn in self.checkpoints:
  94. # os.remove(fn)
  95. self.checkpoints = []