| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- # 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.util.infinity import INFINITY
- from pytcore.tcore.composer import Composer
- from pytcore.rules.brule import BRule
- class BSRule(Composer):
- '''
- Selects a branch in which the matcher succeeds, as long as matches can be found.
- '''
- def __init__(self, branches, max_iterations=INFINITY):
- '''
- Selects a branch in which the matcher succeeds, as long as matches can be found.
- @param branches: A list of ARules.
- @param max_iterations: The maximum number of times to apply the transformation.
- '''
- super(BSRule, self).__init__()
- self.brule = BRule(branches)
- self.max_iterations = max_iterations
- self.iterations = 0
- def packet_in(self, packet):
- self.exception = None
- self.is_success = False
- while self.iterations < self.max_iterations:
- # Re-apply the BRule
- packet = self.brule.packet_in(packet)
- if not self.brule.is_success:
- self.exception = self.brule.exception
- return packet
- else:
- self.is_success = True
- self.iterations += 1
- return packet
|