lqsrule.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 lrule import LRule
  6. from symbol import try_stmt
  7. class LQSRule(LRule):
  8. '''
  9. Applies an inner rule for each match of the LHS as long as matches can be found.
  10. '''
  11. def __init__(self, LHS, inner_rule, max_iterations=INFINITY):
  12. '''
  13. Applies an inner rule for each match of the LHS as long as matches can be found.
  14. @param LHS: The pre-condition pattern (LHS + NACs).
  15. @param inner_rule: The rule to apply in the loop.
  16. @param max_iterations: The maximum number of matches of the LHS.
  17. '''
  18. super(LQSRule, self).__init__(LHS, inner_rule, max_iterations)
  19. def packet_in(self, packet):
  20. self.exception = None
  21. self.is_success = False
  22. # Match
  23. packet = self.M.packet_in(packet)
  24. if not self.M.is_success:
  25. self.exception = self.M.exception
  26. return packet
  27. # Choose the first match
  28. packet = self.I.packet_in(packet)
  29. if not self.I.is_success:
  30. self.exception = self.I.exception
  31. return packet
  32. while True:
  33. # Apply the inner rule
  34. packet = self.inner_rule.packet_in(packet)
  35. if not self.inner_rule.is_success:
  36. if self.inner_rule.exception:
  37. self.exception = self.inner_rule.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. ''' hergin :: motif-integration : clean Matchset before rematch because only LHS doesnot have a rewriter '''
  44. #packet.match_sets = {}
  45. #try:
  46. # if len(packet.match_sets[self.I.condition].matches) == 0:
  47. # del packet.match_sets[self.I.condition]
  48. #except KeyError:
  49. # pass
  50. # Re-Match
  51. packet = self.M.packet_in(packet)
  52. if not self.M.is_success:
  53. self.exception = self.M.exception
  54. return packet
  55. # Choose another match
  56. packet = self.I.next_in(packet)
  57. # No more iterations are left
  58. if not self.I.is_success:
  59. if self.I.exception:
  60. self.exception = self.I.exception
  61. return packet