srule.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. '''*****************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Eugene Syriani
  4. This file is part of AToMPM.
  5. AToMPM is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU Lesser General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or (at your option) any later
  8. version.
  9. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************'''
  15. from ..util.infinity import INFINITY
  16. from arule import ARule
  17. from ..tcore.resolver import Resolver
  18. class SRule(ARule):
  19. '''
  20. Applies the transformation as long as matches can be found.
  21. '''
  22. def __init__(self, LHS, RHS, max_iterations=INFINITY,sendAndApplyDeltaFunc=None):
  23. '''
  24. Applies the transformation as long as matches can be found.
  25. @param LHS: The pre-condition pattern (LHS + NACs).
  26. @param RHS: The post-condition pattern (RHS).
  27. @param max_iterations: The maximum number of times to apply the transformation.
  28. '''
  29. super(SRule, self).__init__(LHS, RHS,sendAndApplyDeltaFunc)
  30. self.I.max_iterations = max_iterations
  31. def packet_in(self, packet):
  32. self.exception = None
  33. self.is_success = False
  34. # Match
  35. packet = self.M.packet_in(packet)
  36. if not self.M.is_success:
  37. self.exception = self.M.exception
  38. return packet
  39. # Choose the first match
  40. packet = self.I.packet_in(packet)
  41. if not self.I.is_success:
  42. self.exception = self.I.exception
  43. return packet
  44. while True:
  45. # Rewrite
  46. packet = self.W.packet_in(packet)
  47. if not self.W.is_success:
  48. self.exception = self.W.exception
  49. return packet
  50. # Rule has been applied once, so it's a success anyway
  51. self.is_success = True
  52. if self.I.iterations == self.I.max_iterations:
  53. return packet
  54. # Re-Match
  55. packet = self.M.packet_in(packet)
  56. if not self.M.is_success:
  57. self.exception = self.M.exception
  58. return packet
  59. # Choose another match
  60. packet = self.I.next_in(packet)
  61. # No more iterations are left
  62. if not self.I.is_success:
  63. if self.I.exception:
  64. self.exception = self.I.exception
  65. return packet
  66. class SRule_r(SRule):
  67. '''
  68. Applies the transformation on one match.
  69. '''
  70. def __init__(self, LHS, RHS, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  71. '''
  72. Applies the transformation as long as matches can be found.
  73. @param LHS: The pre-condition pattern (LHS + NACs).
  74. @param RHS: The post-condition pattern (RHS).
  75. @param max_iterations: The maximum number of times to apply the transformation.
  76. @param external_matches_only: Resolve conflicts ignoring the matches found in this SRule.
  77. @param custom_resolution: Override the default resolution function.
  78. '''
  79. super(SRule_r, self).__init__(LHS, RHS, max_iterations)
  80. self.R = Resolver(external_matches_only=external_matches_only,
  81. custom_resolution=custom_resolution)
  82. def packet_in(self, packet):
  83. self.exception = None
  84. self.is_success = False
  85. # Match
  86. packet = self.M.packet_in(packet)
  87. if not self.M.is_success:
  88. self.exception = self.M.exception
  89. return packet
  90. # Choose the first match
  91. packet = self.I.packet_in(packet)
  92. if not self.I.is_success:
  93. self.exception = self.I.exception
  94. return packet
  95. while True:
  96. # Rewrite
  97. packet = self.W.packet_in(packet)
  98. if not self.W.is_success:
  99. self.exception = self.W.exception
  100. return packet
  101. # Resolve any conflicts if necessary
  102. packet = self.R.packet_in(packet)
  103. if not self.R.is_success:
  104. self.exception = self.R.exception
  105. return packet
  106. # Rule has been applied once, so it's a success anyway
  107. self.is_success = True
  108. if self.I.iterations == self.I.max_iterations:
  109. return packet
  110. # Re-Match
  111. packet = self.M.packet_in(packet)
  112. if not self.M.is_success:
  113. self.exception = self.M.exception
  114. return packet
  115. # Choose another match
  116. packet = self.I.next_in(packet)
  117. # No more iterations are left
  118. if not self.I.is_success:
  119. if self.I.exception:
  120. self.exception = self.I.exception
  121. return packet