sequence.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. '''*****************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Eugene Syriani
  4. This file is part of AToMPM.
  5. AToMPM is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU Lesser General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or (at your option) any later
  8. version.
  9. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************'''
  15. from ..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