frule.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 FRule(ARule):
  19. '''
  20. Applies the transformation on all matches found.
  21. '''
  22. def __init__(self, LHS, RHS, max_iterations=INFINITY,sendAndApplyDeltaFunc=None):
  23. '''
  24. Applies the transformation on all matches 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 rule.
  28. '''
  29. super(FRule, self).__init__(LHS, RHS,sendAndApplyDeltaFunc)
  30. # Matcher needs to find many matches
  31. self.M.max = max_iterations
  32. self.I.max_iterations = max_iterations
  33. def packet_in(self, packet):
  34. self.exception = None
  35. self.is_success = False
  36. # Match
  37. packet = self.M.packet_in(packet)
  38. if not self.M.is_success:
  39. self.exception = self.M.exception
  40. return packet
  41. # Choose the first match
  42. packet = self.I.packet_in(packet)
  43. if not self.I.is_success:
  44. self.exception = self.I.exception
  45. return packet
  46. while True:
  47. # Rewrite
  48. packet = self.W.packet_in(packet)
  49. if not self.W.is_success:
  50. self.exception = self.W.exception
  51. return packet
  52. # Choose another match
  53. packet = self.I.next_in(packet)
  54. # No more iterations are left
  55. if not self.I.is_success:
  56. if self.I.exception:
  57. self.exception = self.I.exception
  58. else:
  59. # Output success packet
  60. self.is_success = True
  61. return packet
  62. class FRule_r(ARule):
  63. '''
  64. Applies the transformation on all matches found.
  65. '''
  66. def __init__(self, LHS, RHS, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  67. '''
  68. Applies the transformation on all matches found.
  69. @param LHS: The pre-condition pattern (LHS + NACs).
  70. @param RHS: The post-condition pattern (RHS).
  71. @param max_iterations: The maximum number of times to apply the rule.
  72. @param external_matches_only: Resolve conflicts ignoring the matches found in this FRule.
  73. @param custom_resolution: Override the default resolution function.
  74. '''
  75. super(FRule_r, self).__init__(LHS, RHS, max_iterations)
  76. self.R = Resolver(external_matches_only=external_matches_only,
  77. custom_resolution=custom_resolution)
  78. def packet_in(self, packet):
  79. self.exception = None
  80. self.is_success = False
  81. # Match
  82. packet = self.M.packet_in(packet)
  83. if not self.M.is_success:
  84. self.exception = self.M.exception
  85. return packet
  86. # Choose the first match
  87. packet = self.I.packet_in(packet)
  88. if not self.I.is_success:
  89. self.exception = self.I.exception
  90. return packet
  91. while True:
  92. # Rewrite
  93. packet = self.W.packet_in(packet)
  94. if not self.W.is_success:
  95. self.exception = self.W.exception
  96. return packet
  97. # Resolve any conflicts if necessary
  98. packet = self.R.packet_in(packet)
  99. if not self.R.is_success:
  100. self.exception = self.R.exception
  101. return packet
  102. # Choose another match
  103. packet = self.I.next_in(packet)
  104. # No more iterations are left
  105. if not self.I.is_success:
  106. if self.I.exception:
  107. self.exception = self.I.exception
  108. else:
  109. # Output success packet
  110. self.is_success = True
  111. return packet