ndarule.py 2.0 KB

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