arule.py 3.5 KB

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