lqsrule.py 2.6 KB

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