lsrule.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.rules.lrule import LRule
  17. from pytcore.tcore.rewriter import Rewriter
  18. from pytcore.tcore.resolver import Resolver
  19. class LSRule(LRule):
  20. '''
  21. Applies an inner rule for each application of the outer rule as long as matches can be found.
  22. '''
  23. def __init__(self, LHS, RHS, inner_rule, outer_first, sendAndApplyDeltaFunc, max_iterations=INFINITY):
  24. '''
  25. Applies an inner rule for each application of the outer rule as long as matches can be found.
  26. @param LHS: The pre-condition pattern (LHS + NACs).
  27. @param RHS: The post-condition pattern (RHS).
  28. @param inner_rule: The rule to apply in the loop.
  29. @param outer_first: Whether the outer rule should be applied before the inner rule.
  30. @param max_iterations: The maximum number of matches of the LHS.
  31. '''
  32. super(LSRule, self).__init__(LHS, inner_rule, max_iterations)
  33. self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
  34. self.outer_first = outer_first
  35. def packet_in(self, packet):
  36. self.exception = None
  37. self.is_success = False
  38. # Match
  39. packet = self.M.packet_in(packet)
  40. if not self.M.is_success:
  41. self.exception = self.M.exception
  42. return packet
  43. # Choose the first match
  44. packet = self.I.packet_in(packet)
  45. if not self.I.is_success:
  46. self.exception = self.I.exception
  47. return packet
  48. while True:
  49. if self.outer_first:
  50. # Rewrite
  51. packet = self.W.packet_in(packet)
  52. if not self.W.is_success:
  53. self.exception = self.W.exception
  54. return packet
  55. # Apply the inner rule
  56. packet = self.inner_rule.packet_in(packet)
  57. if not self.inner_rule.is_success:
  58. self.exception = self.inner_rule.exception
  59. return packet
  60. if not self.outer_first:
  61. # Rewrite
  62. packet = self.W.packet_in(packet)
  63. if not self.W.is_success:
  64. self.exception = self.W.exception
  65. return packet
  66. # Rule has been applied once, so it's a success anyway
  67. self.is_success = True
  68. if self.I.iterations == self.I.max_iterations:
  69. return packet
  70. # Re-Match
  71. packet = self.M.packet_in(packet)
  72. if not self.M.is_success:
  73. self.exception = self.M.exception
  74. return packet
  75. # Choose another match
  76. packet = self.I.next_in(packet)
  77. # No more iterations are left
  78. if not self.I.is_success:
  79. if self.I.exception:
  80. self.exception = self.I.exception
  81. return packet
  82. class LSRule_r(LSRule):
  83. '''
  84. Applies an inner rule for each application of the outer rule as long as matches can be found.
  85. '''
  86. def __init__(self, LHS, RHS, external_matches_only=False, custom_resolution=lambda packet: False):
  87. '''
  88. Applies an inner rule for each application of the outer rule as long as matches can be found.
  89. @param LHS: The pre-condition pattern (LHS + NACs).
  90. @param RHS: The post-condition pattern (RHS).
  91. @param inner_rule: The rule to apply in the loop.
  92. @param outer_first: Whether the outer rule should be applied before the inner rule.
  93. @param max_iterations: The maximum number of matches of the LHS.
  94. @param external_matches_only: Resolve conflicts ignoring the matches found in this FRule.
  95. @param custom_resolution: Override the default resolution function.
  96. '''
  97. super(LSRule_r, self).__init__()
  98. self.R = Resolver(external_matches_only=external_matches_only,
  99. custom_resolution=custom_resolution)
  100. def packet_in(self, packet):
  101. self.exception = None
  102. self.is_success = False
  103. # Match
  104. packet = self.M.packet_in(packet)
  105. if not self.M.is_success:
  106. self.exception = self.M.exception
  107. return packet
  108. # Choose the first match
  109. packet = self.I.packet_in(packet)
  110. if not self.I.is_success:
  111. self.exception = self.I.exception
  112. return packet
  113. while True:
  114. if self.outer_first:
  115. # Rewrite
  116. packet = self.W.packet_in(packet)
  117. if not self.W.is_success:
  118. self.exception = self.W.exception
  119. return packet
  120. # Resolve any conflicts if necessary
  121. packet = self.R.packet_in(packet)
  122. if not self.R.is_success:
  123. self.exception = self.R.exception
  124. return packet
  125. # Apply the inner rule
  126. packet = self.inner_rule.packet_in(packet)
  127. if not self.inner_rule.is_success:
  128. self.exception = self.inner_rule.exception
  129. return packet
  130. if not self.outer_first:
  131. # Rewrite
  132. packet = self.W.packet_in(packet)
  133. if not self.W.is_success:
  134. self.exception = self.W.exception
  135. return packet
  136. # Resolve any conflicts if necessary
  137. packet = self.R.packet_in(packet)
  138. if not self.R.is_success:
  139. self.exception = self.R.exception
  140. return packet
  141. # Rule has been applied once, so it's a success anyway
  142. self.is_success = True
  143. if self.I.iterations == self.I.max_iterations:
  144. return packet
  145. # Re-Match
  146. packet = self.M.packet_in(packet)
  147. if not self.M.is_success:
  148. self.exception = self.M.exception
  149. return packet
  150. # Choose another match
  151. packet = self.I.next_in(packet)
  152. # No more iterations are left
  153. if not self.I.is_success:
  154. if self.I.exception:
  155. self.exception = self.I.exception
  156. return packet