lqsrule.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class LQSRule(LRule):
  18. '''
  19. Applies an inner rule for each match of the LHS as long as matches can be found.
  20. '''
  21. def __init__(self, LHS, inner_rule, max_iterations=INFINITY):
  22. '''
  23. Applies an inner rule for each match of the LHS as long as matches can be found.
  24. @param LHS: The pre-condition pattern (LHS + NACs).
  25. @param inner_rule: The rule to apply in the loop.
  26. @param max_iterations: The maximum number of matches of the LHS.
  27. '''
  28. super(LQSRule, self).__init__(LHS, inner_rule, max_iterations)
  29. def packet_in(self, packet):
  30. self.exception = None
  31. self.is_success = False
  32. # Match
  33. packet = self.M.packet_in(packet)
  34. if not self.M.is_success:
  35. self.exception = self.M.exception
  36. return packet
  37. # Choose the first match
  38. packet = self.I.packet_in(packet)
  39. if not self.I.is_success:
  40. self.exception = self.I.exception
  41. return packet
  42. while True:
  43. # Apply the inner rule
  44. packet = self.inner_rule.packet_in(packet)
  45. if not self.inner_rule.is_success:
  46. if self.inner_rule.exception:
  47. self.exception = self.inner_rule.exception
  48. return packet
  49. # Rule has been applied once, so it's a success anyway
  50. self.is_success = True
  51. if self.I.iterations == self.I.max_iterations:
  52. return packet
  53. ''' hergin :: motif-integration : clean Matchset before rematch because only LHS doesnot have a rewriter '''
  54. #packet.match_sets = {}
  55. #try:
  56. # if len(packet.match_sets[self.I.condition].matches) == 0:
  57. # del packet.match_sets[self.I.condition]
  58. #except KeyError:
  59. # pass
  60. # Re-Match
  61. packet = self.M.packet_in(packet)
  62. if not self.M.is_success:
  63. self.exception = self.M.exception
  64. return packet
  65. # Choose another match
  66. packet = self.I.next_in(packet)
  67. # No more iterations are left
  68. if not self.I.is_success:
  69. if self.I.exception:
  70. self.exception = self.I.exception
  71. return packet