lqsrule.py 3.3 KB

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