frule.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 .arule import ARule
  6. from ..tcore.resolver import Resolver
  7. class FRule(ARule):
  8. '''
  9. Applies the transformation on all matches found.
  10. '''
  11. def __init__(self, LHS, RHS, max_iterations=INFINITY,sendAndApplyDeltaFunc=None):
  12. '''
  13. Applies the transformation on all matches found.
  14. @param LHS: The pre-condition pattern (LHS + NACs).
  15. @param RHS: The post-condition pattern (RHS).
  16. @param max_iterations: The maximum number of times to apply the rule.
  17. '''
  18. super(FRule, self).__init__(LHS, RHS,sendAndApplyDeltaFunc)
  19. # Matcher needs to find many matches
  20. self.M.max = max_iterations
  21. self.I.max_iterations = max_iterations
  22. def packet_in(self, packet):
  23. self.exception = None
  24. self.is_success = False
  25. # Match
  26. packet = self.M.packet_in(packet)
  27. if not self.M.is_success:
  28. self.exception = self.M.exception
  29. return packet
  30. # Choose the first match
  31. packet = self.I.packet_in(packet)
  32. if not self.I.is_success:
  33. self.exception = self.I.exception
  34. return packet
  35. while True:
  36. # Rewrite
  37. packet = self.W.packet_in(packet)
  38. if not self.W.is_success:
  39. self.exception = self.W.exception
  40. return packet
  41. # Choose another match
  42. packet = self.I.next_in(packet)
  43. # No more iterations are left
  44. if not self.I.is_success:
  45. if self.I.exception:
  46. self.exception = self.I.exception
  47. else:
  48. # Output success packet
  49. self.is_success = True
  50. return packet
  51. class FRule_r(ARule):
  52. '''
  53. Applies the transformation on all matches found.
  54. '''
  55. def __init__(self, LHS, RHS, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  56. '''
  57. Applies the transformation on all matches found.
  58. @param LHS: The pre-condition pattern (LHS + NACs).
  59. @param RHS: The post-condition pattern (RHS).
  60. @param max_iterations: The maximum number of times to apply the rule.
  61. @param external_matches_only: Resolve conflicts ignoring the matches found in this FRule.
  62. @param custom_resolution: Override the default resolution function.
  63. '''
  64. super(FRule_r, self).__init__(LHS, RHS, max_iterations)
  65. self.R = Resolver(external_matches_only=external_matches_only,
  66. custom_resolution=custom_resolution)
  67. def packet_in(self, packet):
  68. self.exception = None
  69. self.is_success = False
  70. # 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 the first match
  76. packet = self.I.packet_in(packet)
  77. if not self.I.is_success:
  78. self.exception = self.I.exception
  79. return packet
  80. while True:
  81. # Rewrite
  82. packet = self.W.packet_in(packet)
  83. if not self.W.is_success:
  84. self.exception = self.W.exception
  85. return packet
  86. # Resolve any conflicts if necessary
  87. packet = self.R.packet_in(packet)
  88. if not self.R.is_success:
  89. self.exception = self.R.exception
  90. return packet
  91. # Choose another match
  92. packet = self.I.next_in(packet)
  93. # No more iterations are left
  94. if not self.I.is_success:
  95. if self.I.exception:
  96. self.exception = self.I.exception
  97. else:
  98. # Output success packet
  99. self.is_success = True
  100. return packet