bsrule.py 2.0 KB

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