bsrule.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 ..util.infinity import INFINITY
  5. from ..tcore.composer import Composer
  6. from .brule import BRule
  7. class BSRule(Composer):
  8. '''
  9. Selects a branch in which the matcher succeeds, as long as matches can be found.
  10. '''
  11. def __init__(self, branches, max_iterations=INFINITY):
  12. '''
  13. Selects a branch in which the matcher succeeds, as long as matches can be found.
  14. @param branches: A list of ARules.
  15. @param max_iterations: The maximum number of times to apply the transformation.
  16. '''
  17. super(BSRule, self).__init__()
  18. self.brule = BRule(branches)
  19. self.max_iterations = max_iterations
  20. self.iterations = 0
  21. def packet_in(self, packet):
  22. self.exception = None
  23. self.is_success = False
  24. while self.iterations < self.max_iterations:
  25. # Re-apply the BRule
  26. packet = self.brule.packet_in(packet)
  27. if not self.brule.is_success:
  28. self.exception = self.brule.exception
  29. return packet
  30. else:
  31. self.is_success = True
  32. self.iterations += 1
  33. return packet