sequence.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. class Sequence(Composer):
  17. '''
  18. Applies each rule in the order provided.
  19. '''
  20. def __init__(self, rules):
  21. '''
  22. Applies each rule in the order provided.
  23. @param rules: The rules to apply.
  24. '''
  25. super(Sequence, self).__init__()
  26. self.rules = rules
  27. def packet_in(self, packet):
  28. self.exception = None
  29. self.is_success = False
  30. for rule in self.rules:
  31. packet = rule.packet_in(packet)
  32. packet.clean()
  33. if not rule.is_success:
  34. if rule.exception is not None:
  35. self.exception = rule.exception
  36. return packet
  37. self.is_success = True
  38. return packet