| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- # 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.tcore.matcher import Matcher
- from pytcore.tcore.iterator import Iterator
- from pytcore.tcore.rewriter import Rewriter
- from pytcore.tcore.resolver import Resolver
- class ARule(Composer):
- '''
- Applies the transformation on one match.
- '''
- def __init__(self, LHS, RHS,sendAndApplyDeltaFunc):
- '''
- Applies the transformation on one match.
- @param LHS: The pre-condition pattern (LHS + NACs).
- @param RHS: The post-condition pattern (RHS).
- '''
- super(ARule, self).__init__()
- self.M = Matcher(condition=LHS, max=1)
- self.I = Iterator(max_iterations=1)
- self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
- 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
- self.M.packet_in(packet)
- return packet
- # Choose the only match
- packet = self.I.packet_in(packet)
- if not self.I.is_success:
- self.exception = self.I.exception
- return packet
- # Rewrite
- packet = self.W.packet_in(packet)
- if not self.W.is_success:
- self.exception = self.W.exception
- return packet
- # Output success packet
- self.is_success = True
- return packet
- class ARule_r(ARule):
- '''
- Applies the transformation on one match.
- '''
- def __init__(self, LHS, RHS, external_matches_only=False, custom_resolution=lambda packet: False):
- '''
- Applies the transformation on one match.
- @param LHS: The pre-condition pattern (LHS + NACs).
- @param RHS: The post-condition pattern (RHS).
- @param external_matches_only: Resolve conflicts ignoring the matches found in this ARule.
- @param custom_resolution: Override the default resolution function.
- '''
- super(ARule_r, self).__init__(LHS, RHS)
- self.R = Resolver(external_matches_only=external_matches_only,
- custom_resolution=custom_resolution)
- def packet_in(self, packet):
- packet = super(ARule_r, self).packet_in(packet)
- # is_success is True
- if self.exception is None:
- # Resolve any conflicts if necessary
- packet = self.R.packet_in(packet)
- if not self.R.is_success:
- self.exception = self.R.exception
- return packet
- # Output success packet
- else:
- self.is_success = False
- return packet
|