rewriter.py 2.7 KB

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