iterator.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.util.seeded_random import Random
  16. from pytcore.util.infinity import INFINITY
  17. from pytcore.tcore.rule_primitive import RulePrimitive
  18. #from messages import TransformationException
  19. class Iterator(RulePrimitive):
  20. '''
  21. Chooses randomly one match from the packet.
  22. '''
  23. def __init__(self, condition=None, max_iterations=INFINITY, rng=None):
  24. '''
  25. Selects one match from the packet.
  26. @param condition: The pre-condition pattern.
  27. @param max_iterations: The maximum number of times to select.
  28. By default, this is +INFINITY.
  29. '''
  30. super(Iterator, self).__init__()
  31. self.max_iterations = max_iterations
  32. self.iterations = 0
  33. self.rng = rng
  34. if condition:
  35. self.condition = condition.get_id()
  36. def cancelIn(self, cancel):
  37. if self.condition not in cancel.exclusions:
  38. super(Iterator, self).cancelIn(cancel)
  39. self.iterations = 0
  40. def packet_in(self, packet):
  41. self.exception = None
  42. self.is_success = False
  43. if packet.current in packet.match_sets:
  44. self.condition = packet.current
  45. # Promote the selected match to be the match to rewrite
  46. packet.match_sets[packet.current].match2rewrite = self._choose(packet)
  47. self._globalize_pivots(packet)
  48. self.iterations = 1
  49. self.is_success = True
  50. return packet
  51. def next_in(self, packet):
  52. self.exception = None
  53. self.is_success = False
  54. packet.current = self.condition
  55. if self.iterations < self.max_iterations and packet.current in packet.match_sets:
  56. if len(packet.match_sets[self.condition].matches) == 0:
  57. del packet.match_sets[self.condition]
  58. return packet
  59. # Promote the selected match to be the match to rewrite
  60. packet.match_sets[packet.current].match2rewrite = self._choose(packet)
  61. self._globalize_pivots(packet)
  62. self.iterations += 1
  63. self.is_success = True
  64. return packet
  65. def _choose(self, packet):
  66. # Choose a match form the current match set and remove it from the list of matches
  67. return packet.match_sets[packet.current].matches.pop((self.rng if self.rng != None else Random).randint(0, len(packet.match_sets[packet.current].matches) - 1))
  68. def _globalize_pivots(self, packet):
  69. """
  70. Puts all local pivots of the current match in the global pivots of the packet.
  71. Of course, local pivots have priority over global pivots.
  72. """
  73. local_pivots = packet.match_sets[packet.current].match2rewrite.local_pivots
  74. for p in local_pivots:
  75. packet.global_pivots[p] = local_pivots[p]