brule.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
  2. # at the University of Antwerp (http://msdl.uantwerpen.be/).
  3. #
  4. # Licensed under the GNU Public License v3 (the "License");
  5. # you may not use this file except in compliance with the
  6. # License. You may obtain a copy of the License at
  7. # https://www.gnu.org/licenses/gpl-3.0.en.html
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an
  11. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the
  14. # License.
  15. from pytcore.tcore.composer import Composer
  16. from pytcore.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 = list(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