brule.py 1.6 KB

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