arule.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ..tcore.composer import Composer
  5. from ..tcore.matcher import Matcher
  6. from ..tcore.iterator import Iterator
  7. from ..tcore.rewriter import Rewriter
  8. from ..tcore.resolver import Resolver
  9. class ARule(Composer):
  10. '''
  11. Applies the transformation on one match.
  12. '''
  13. def __init__(self, LHS, RHS,sendAndApplyDeltaFunc):
  14. '''
  15. Applies the transformation on one match.
  16. @param LHS: The pre-condition pattern (LHS + NACs).
  17. @param RHS: The post-condition pattern (RHS).
  18. '''
  19. super(ARule, self).__init__()
  20. self.M = Matcher(condition=LHS, max=1)
  21. self.I = Iterator(max_iterations=1)
  22. self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
  23. def packet_in(self, packet):
  24. self.exception = None
  25. self.is_success = False
  26. # Match
  27. packet = self.M.packet_in(packet)
  28. if not self.M.is_success:
  29. self.exception = self.M.exception
  30. return packet
  31. # Choose the only match
  32. packet = self.I.packet_in(packet)
  33. if not self.I.is_success:
  34. self.exception = self.I.exception
  35. return packet
  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. # Output success packet
  42. self.is_success = True
  43. return packet
  44. class ARule_r(ARule):
  45. '''
  46. Applies the transformation on one match.
  47. '''
  48. def __init__(self, LHS, RHS, external_matches_only=False, custom_resolution=lambda packet: False):
  49. '''
  50. Applies the transformation on one match.
  51. @param LHS: The pre-condition pattern (LHS + NACs).
  52. @param RHS: The post-condition pattern (RHS).
  53. @param external_matches_only: Resolve conflicts ignoring the matches found in this ARule.
  54. @param custom_resolution: Override the default resolution function.
  55. '''
  56. super(ARule_r, self).__init__(LHS, RHS)
  57. self.R = Resolver(external_matches_only=external_matches_only,
  58. custom_resolution=custom_resolution)
  59. def packet_in(self, packet):
  60. packet = super(ARule_r, self).packet_in(packet)
  61. # is_success is True
  62. if self.exception is None:
  63. # Resolve any conflicts if necessary
  64. packet = self.R.packet_in(packet)
  65. if not self.R.is_success:
  66. self.exception = self.R.exception
  67. return packet
  68. # Output success packet
  69. else:
  70. self.is_success = False
  71. return packet