ndarule.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 ..tcore.composer import Composer
  5. from ..tcore.matcher import Matcher
  6. from ..tcore.iterator import Iterator
  7. from ..tcore.rewriter import Rewriter
  8. from ..tcore.resolver import Resolver
  9. class NDARule(Composer):
  10. '''
  11. Applies the transformation on one match.
  12. '''
  13. def __init__(self, LHS, RHS, rng, sendAndApplyDeltaFunc,ignore_resolver=False, external_matches_only=False,
  14. custom_resolution=lambda packet: False):
  15. '''
  16. Applies the transformation on one match.
  17. @param LHS: The pre-condition pattern (LHS + NACs).
  18. @param RHS: The post-condition pattern (RHS).
  19. @param ignore_resolver: Specifies whether or not a resolver is needed.
  20. @param external_matches_only: Resolve conflicts ignoring the matches found in this ARule.
  21. @param custom_resolution: Override the default resolution function.
  22. '''
  23. super(NDARule, self).__init__()
  24. self.ignore_resolver = ignore_resolver
  25. self.M = Matcher(condition=LHS)
  26. self.I = Iterator(max_iterations=1, rng=rng)
  27. self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
  28. self.R = Resolver(external_matches_only=external_matches_only,
  29. custom_resolution=custom_resolution)
  30. def packet_in(self, packet):
  31. self.exception = None
  32. self.is_success = False
  33. # Match
  34. packet = self.M.packet_in(packet)
  35. if not self.M.is_success:
  36. self.exception = self.M.exception
  37. return packet
  38. # Choose the only match
  39. packet = self.I.packet_in(packet)
  40. if not self.I.is_success:
  41. self.exception = self.I.exception
  42. return packet
  43. # Rewrite
  44. packet = self.W.packet_in(packet)
  45. if not self.W.is_success:
  46. self.exception = self.W.exception
  47. return packet
  48. # Output success packet
  49. self.is_success = True
  50. return packet