resolver.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. '''*****************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Eugene Syriani
  4. This file is part of AToMPM.
  5. AToMPM is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU Lesser General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or (at your option) any later
  8. version.
  9. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************'''
  15. from rule_primitive import RulePrimitive
  16. from 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