| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- # 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.tcore.matcher import Matcher
- from pytcore.tcore.iterator import Iterator
- class LRule(Composer):
- '''
- Applies an inner rule for each match of the LHS.
- '''
- def __init__(self, LHS, inner_rule, max_iterations=INFINITY):
- '''
- Applies an inner rule for each match of the LHS.
- @param LHS: The pre-condition pattern (LHS + NACs).
- @param inner_rule: The rule to apply in the loop.
- @param max_iterations: The maximum number of matches of the LHS.
- '''
- super(LRule, self).__init__()
- self.M = Matcher(condition=LHS, max=max_iterations)
- self.I = Iterator(max_iterations=max_iterations)
- self.inner_rule = inner_rule
- def packet_in(self, packet):
- self.exception = None
- self.is_success = False
- # Match
- packet = self.M.packet_in(packet)
- if not self.M.is_success:
- self.exception = self.M.exception
- return packet
- # Choose the first match
- packet = self.I.packet_in(packet)
- if not self.I.is_success:
- self.exception = self.I.exception
- return packet
- while True:
- # Apply the inner rule
- packet = self.inner_rule.packet_in(packet)
- if not self.inner_rule.is_success:
- if self.inner_rule.exception:
- self.exception = self.inner_rule.exception
- return packet
- # Clean the packet: required since there is no Rewriter in a Query
- if len(packet.match_sets[self.I.condition].matches) == 0:
- del packet.match_sets[self.I.condition]
- # Choose another match
- packet = self.I.next_in(packet)
- # No more iterations are left
- if not self.I.is_success:
- if self.I.exception:
- self.exception = self.I.exception
- else:
- # Output success packet
- self.is_success = True
- return packet
|