| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
- # at the University of Antwerp (http://msdl.uantwerpen.be/).
- #
- # Licensed under the GNU Public License v3 (the "License");
- # you may not use this file except in compliance with the
- # License. You may obtain a copy of the License at
- # https://www.gnu.org/licenses/gpl-3.0.en.html
- #
- # Unless required by applicable law or agreed to in writing,
- # software distributed under the License is distributed on an
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- # either express or implied. See the License for the specific
- # language governing permissions and limitations under the
- # License.
- from pytcore.tcore.composer import Composer
- from pytcore.util.seeded_random import Random
- class BRule(Composer):
- '''
- Selects a branch in which the matcher succeeds.
- '''
- def __init__(self, branches):
- '''
- Selects a branch in which the matcher succeeds.
- @param branches: A list of ARules.
- '''
- super(BRule, self).__init__()
- self.branches = branches
- def packet_in(self, packet):
- self.exception = None
- self.is_success = False
- remaining_branches = list(range(len(self.branches)))
- ''' hergin motif-integration ::: clone commented in observance of not need
- report bugs if have '''
- #original = packet.clone()
- # Success on the first branch that is in success
- while len(remaining_branches) > 0:
- branch_no = Random.choice(remaining_branches)
- branch = self.branches[branch_no]
- packet = branch.packet_in(packet)
- if not branch.is_success:
- if branch.exception is not None:
- self.exception = branch.exception
- break
- else:
- # Ignore this branch for next try
- remaining_branches.remove(branch_no)
- #packet = original.clone()
- else:
- self.is_success = True
- break
- return packet
-
|