resolver.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. class Resolver(RulePrimitive):
  18. '''
  19. Detects & resolves any conflict between matches and rewritings.
  20. '''
  21. def __init__(self, external_matches_only=False, custom_resolution=lambda packet: False):
  22. '''
  23. Detects & resolves any conflict between matches.
  24. @param external_matches_only: Whether to only check for matches outside the current scope of the resolver.
  25. By default, this is False.
  26. @param custom_resolution: Function that defines how to resolve any conflict
  27. By default, this returns False.
  28. '''
  29. super(Resolver, self).__init__()
  30. self.external_matches_only = external_matches_only
  31. self.custom_resolution = custom_resolution
  32. def packet_in(self, packet):
  33. '''
  34. Attempts to merge the packets into a single one, only if all threads had succeeded.
  35. '''
  36. self.exception = None
  37. self.is_success = False
  38. for cond in packet.match_sets:
  39. # Ignore the current match set when checking for conflicts with external matches only
  40. if self.external_matches_only and cond == packet.current:
  41. continue
  42. for match in packet.match_sets[cond].matches:
  43. if match.is_dirty(packet):
  44. # First try the custom resolution function
  45. if not self._custom_resolution(packet, match):
  46. # Then try the default resolution function
  47. if not self._default_resolution(packet, match):
  48. self.is_success = False
  49. # TODO: This should be an InconsistentUseException
  50. self.exception = TransformationException()
  51. self.exception.packet = packet
  52. self.exception.transformation_unit = self
  53. return packet
  54. # No conflicts are to be reported
  55. self.is_success = True
  56. return packet
  57. def _custom_resolution(self, packet, match):
  58. '''
  59. Applies the user-defined resolution function
  60. '''
  61. return self.custom_resolution(packet)
  62. def _default_resolution(self, packet, match):
  63. '''
  64. Attempts to resolve conservatively any conflicts
  65. '''
  66. return False