| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
- # at the University of Antwerp (http://msdl.uantwerpen.be/).
- #
- # Licensed under the GNU Public License v3 (the "License");
- # you may not use this file except in compliance with the
- # License. You may obtain a copy of the License at
- # https://www.gnu.org/licenses/gpl-3.0.en.html
- #
- # Unless required by applicable law or agreed to in writing,
- # software distributed under the License is distributed on an
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- # either express or implied. See the License for the specific
- # language governing permissions and limitations under the
- # License.
- from pytcore.tcore.rule_primitive import RulePrimitive
- from pytcore.tcore.messages import TransformationException
- from pytcore.core.himesis import Himesis
- import sys
- if sys.version_info[0] >= 3:
- from functools import reduce
- class Rewriter(RulePrimitive):
- """
- Transforms the matched source model elements according to the specified post-condition pattern.
- """
- def __init__(self, condition,sendAndApplyDeltaFunc):
- """
- Transforms the bound graph of the source graph into what the specification of the post-condition pattern.
- @param condition: The the post-condition pattern.
- """
- super(Rewriter, self).__init__()
- self.condition = condition
- self.sendAndApplyDeltaFunc = sendAndApplyDeltaFunc
- def __str__(self):
- s = super(Rewriter, self).__str__()
- s = s.split(' ')
- s.insert(1, '[%s]' % self.condition.name)
- return reduce(lambda x, y: x + ' ' + y, s)
- def packet_in(self, packet):
- self.exception = None
- self.is_success = False
- if self.condition.pre[Himesis.Constants.GUID] not in packet.match_sets:
- self.is_success = False
- # TODO: This should be a TransformationLanguageSpecificException
- self.exception = TransformationException()
- self.exception.packet = packet
- return packet
- else:
- match = packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].match2rewrite
- mapping = match.to_label_mapping(packet.graph)
- # Apply the transformation on the match
- try:
- self.condition.execute(packet, mapping) # Sets dirty nodes as well
- except Exception as e:
- self.is_success = False
- self.exception = TransformationException(e)
- self.exception.packet = packet
- self.exception.transformation_unit = self
- return packet
- # Remove the match
- packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].match2rewrite = None
- if len(packet.match_sets[self.condition.pre[Himesis.Constants.GUID]].matches) == 0:
- del packet.match_sets[self.condition.pre[Himesis.Constants.GUID]]
- #print self.condition
- ''' hergin :: motif-integration :: start '''
- self.sendAndApplyDeltaFunc(packet.deltas)
- ''' hergin :: motif-integration :: end '''
- self.is_success = True
- return packet
|