brule.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 ..tcore.composer import Composer
  16. from ..util.seeded_random import Random
  17. class BRule(Composer):
  18. '''
  19. Selects a branch in which the matcher succeeds.
  20. '''
  21. def __init__(self, branches):
  22. '''
  23. Selects a branch in which the matcher succeeds.
  24. @param branches: A list of ARules.
  25. '''
  26. super(BRule, self).__init__()
  27. self.branches = branches
  28. def packet_in(self, packet):
  29. self.exception = None
  30. self.is_success = False
  31. remaining_branches = range(len(self.branches))
  32. ''' hergin motif-integration ::: clone commented in observance of not need
  33. report bugs if have '''
  34. #original = packet.clone()
  35. # Success on the first branch that is in success
  36. while len(remaining_branches) > 0:
  37. branch_no = Random.choice(remaining_branches)
  38. branch = self.branches[branch_no]
  39. packet = branch.packet_in(packet)
  40. if not branch.is_success:
  41. if branch.exception is not None:
  42. self.exception = branch.exception
  43. break
  44. else:
  45. # Ignore this branch for next try
  46. remaining_branches.remove(branch_no)
  47. #packet = original.clone()
  48. else:
  49. self.is_success = True
  50. break
  51. return packet