rewriter.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 rule_primitive import RulePrimitive
  16. from messages import TransformationException
  17. from ..core.himesis import Himesis
  18. from ...tconstants import TConstants as TC
  19. from ...utils import Utilities as utils
  20. class Rewriter(RulePrimitive):
  21. '''
  22. Transforms the matched source model elements according to the specified post-condition pattern.
  23. '''
  24. def __init__(self, condition,sendAndApplyDeltaFunc):
  25. '''
  26. Transforms the bound graph of the source graph into what the specification of the post-condition pattern.
  27. @param condition: The the post-condition pattern.
  28. '''
  29. super(Rewriter, self).__init__()
  30. self.condition = condition
  31. self.sendAndApplyDeltaFunc = sendAndApplyDeltaFunc
  32. def __str__(self):
  33. s = super(Rewriter, self).__str__()
  34. s = s.split(' ')
  35. s.insert(1, '[%s]' % self.condition.name)
  36. return reduce(lambda x, y: x + ' ' + y, s)
  37. def packet_in(self, packet):
  38. self.exception = None
  39. self.is_success = False
  40. if self.condition.pre[Himesis.Constants.GUID] not in packet.match_sets:
  41. self.is_success = False
  42. # TODO: This should be a TransformationLanguageSpecificException
  43. self.exception = TransformationException()
  44. self.exception.packet = packet
  45. return packet
  46. else:
  47. match = packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].match2rewrite
  48. mapping = match.to_label_mapping(packet.graph)
  49. # Apply the transformation on the match
  50. try:
  51. self.condition.execute(packet, mapping) # Sets dirty nodes as well
  52. except Exception, e:
  53. self.is_success = False
  54. self.exception = TransformationException(e)
  55. self.exception.packet = packet
  56. self.exception.transformation_unit = self
  57. return packet
  58. # Remove the match
  59. packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].match2rewrite = None
  60. if len(packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].matches) == 0:
  61. del packet.match_sets[self.condition.pre[Himesis.Constants.GUID]]
  62. #print self.condition
  63. ''' hergin :: motif-integration :: start '''
  64. self.sendAndApplyDeltaFunc(packet.deltas)
  65. ''' hergin :: motif-integration :: end '''
  66. self.is_success = True
  67. return packet