rewriter.py 2.7 KB

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