rewriter.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
  2. # at the University of Antwerp (http://msdl.uantwerpen.be/).
  3. #
  4. # Licensed under the GNU Public License v3 (the "License");
  5. # you may not use this file except in compliance with the
  6. # License. You may obtain a copy of the License at
  7. # https://www.gnu.org/licenses/gpl-3.0.en.html
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an
  11. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the
  14. # License.
  15. from pytcore.tcore.rule_primitive import RulePrimitive
  16. from pytcore.tcore.messages import TransformationException
  17. from pytcore.core.himesis import Himesis
  18. import sys
  19. if sys.version_info[0] >= 3:
  20. from functools import reduce
  21. class Rewriter(RulePrimitive):
  22. """
  23. Transforms the matched source model elements according to the specified post-condition pattern.
  24. """
  25. def __init__(self, condition,sendAndApplyDeltaFunc):
  26. """
  27. Transforms the bound graph of the source graph into what the specification of the post-condition pattern.
  28. @param condition: The the post-condition pattern.
  29. """
  30. super(Rewriter, self).__init__()
  31. self.condition = condition
  32. self.sendAndApplyDeltaFunc = sendAndApplyDeltaFunc
  33. def __str__(self):
  34. s = super(Rewriter, self).__str__()
  35. s = s.split(' ')
  36. s.insert(1, '[%s]' % self.condition.name)
  37. return reduce(lambda x, y: x + ' ' + y, s)
  38. def packet_in(self, packet):
  39. self.exception = None
  40. self.is_success = False
  41. if self.condition.pre[Himesis.Constants.GUID] not in packet.match_sets:
  42. self.is_success = False
  43. # TODO: This should be a TransformationLanguageSpecificException
  44. self.exception = TransformationException()
  45. self.exception.packet = packet
  46. return packet
  47. else:
  48. match = packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].match2rewrite
  49. mapping = match.to_label_mapping(packet.graph)
  50. # Apply the transformation on the match
  51. try:
  52. self.condition.execute(packet, mapping) # Sets dirty nodes as well
  53. except Exception as e:
  54. self.is_success = False
  55. self.exception = TransformationException(e)
  56. self.exception.packet = packet
  57. self.exception.transformation_unit = self
  58. return packet
  59. # Remove the match
  60. packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].match2rewrite = None
  61. if len(packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].matches) == 0:
  62. del packet.match_sets[self.condition.pre[Himesis.Constants.GUID]]
  63. #print self.condition
  64. ''' hergin :: motif-integration :: start '''
  65. self.sendAndApplyDeltaFunc(packet.deltas)
  66. ''' hergin :: motif-integration :: end '''
  67. self.is_success = True
  68. return packet