srule.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. '''This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. See COPYING.lesser and README.md in the root of this project for full details'''
  4. from ..util.infinity import INFINITY
  5. from .arule import ARule
  6. from ..tcore.resolver import Resolver
  7. class SRule(ARule):
  8. '''
  9. Applies the transformation as long as matches can be found.
  10. '''
  11. def __init__(self, LHS, RHS, max_iterations=INFINITY,sendAndApplyDeltaFunc=None):
  12. '''
  13. Applies the transformation as long as matches can be found.
  14. @param LHS: The pre-condition pattern (LHS + NACs).
  15. @param RHS: The post-condition pattern (RHS).
  16. @param max_iterations: The maximum number of times to apply the transformation.
  17. '''
  18. super(SRule, self).__init__(LHS, RHS,sendAndApplyDeltaFunc)
  19. self.I.max_iterations = max_iterations
  20. def packet_in(self, packet):
  21. self.exception = None
  22. self.is_success = False
  23. # Match
  24. packet = self.M.packet_in(packet)
  25. if not self.M.is_success:
  26. self.exception = self.M.exception
  27. return packet
  28. # Choose the first match
  29. packet = self.I.packet_in(packet)
  30. if not self.I.is_success:
  31. self.exception = self.I.exception
  32. return packet
  33. while True:
  34. # Rewrite
  35. packet = self.W.packet_in(packet)
  36. if not self.W.is_success:
  37. self.exception = self.W.exception
  38. return packet
  39. # Rule has been applied once, so it's a success anyway
  40. self.is_success = True
  41. if self.I.iterations == self.I.max_iterations:
  42. return packet
  43. # Re-Match
  44. packet = self.M.packet_in(packet)
  45. if not self.M.is_success:
  46. self.exception = self.M.exception
  47. return packet
  48. # Choose another match
  49. packet = self.I.next_in(packet)
  50. # No more iterations are left
  51. if not self.I.is_success:
  52. if self.I.exception:
  53. self.exception = self.I.exception
  54. return packet
  55. class SRule_r(SRule):
  56. '''
  57. Applies the transformation on one match.
  58. '''
  59. def __init__(self, LHS, RHS, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  60. '''
  61. Applies the transformation as long as matches can be found.
  62. @param LHS: The pre-condition pattern (LHS + NACs).
  63. @param RHS: The post-condition pattern (RHS).
  64. @param max_iterations: The maximum number of times to apply the transformation.
  65. @param external_matches_only: Resolve conflicts ignoring the matches found in this SRule.
  66. @param custom_resolution: Override the default resolution function.
  67. '''
  68. super(SRule_r, self).__init__(LHS, RHS, max_iterations)
  69. self.R = Resolver(external_matches_only=external_matches_only,
  70. custom_resolution=custom_resolution)
  71. def packet_in(self, packet):
  72. self.exception = None
  73. self.is_success = False
  74. # Match
  75. packet = self.M.packet_in(packet)
  76. if not self.M.is_success:
  77. self.exception = self.M.exception
  78. return packet
  79. # Choose the first match
  80. packet = self.I.packet_in(packet)
  81. if not self.I.is_success:
  82. self.exception = self.I.exception
  83. return packet
  84. while True:
  85. # Rewrite
  86. packet = self.W.packet_in(packet)
  87. if not self.W.is_success:
  88. self.exception = self.W.exception
  89. return packet
  90. # Resolve any conflicts if necessary
  91. packet = self.R.packet_in(packet)
  92. if not self.R.is_success:
  93. self.exception = self.R.exception
  94. return packet
  95. # Rule has been applied once, so it's a success anyway
  96. self.is_success = True
  97. if self.I.iterations == self.I.max_iterations:
  98. return packet
  99. # Re-Match
  100. packet = self.M.packet_in(packet)
  101. if not self.M.is_success:
  102. self.exception = self.M.exception
  103. return packet
  104. # Choose another match
  105. packet = self.I.next_in(packet)
  106. # No more iterations are left
  107. if not self.I.is_success:
  108. if self.I.exception:
  109. self.exception = self.I.exception
  110. return packet