frule.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.infinity import INFINITY
  16. from pytcore.rules.arule import ARule
  17. from pytcore.tcore.resolver import Resolver
  18. class FRule(ARule):
  19. '''
  20. Applies the transformation on all matches found.
  21. '''
  22. def __init__(self, LHS, RHS, max_iterations=INFINITY,sendAndApplyDeltaFunc=None):
  23. '''
  24. Applies the transformation on all matches found.
  25. @param LHS: The pre-condition pattern (LHS + NACs).
  26. @param RHS: The post-condition pattern (RHS).
  27. @param max_iterations: The maximum number of times to apply the rule.
  28. '''
  29. super(FRule, self).__init__(LHS, RHS,sendAndApplyDeltaFunc)
  30. # Matcher needs to find many matches
  31. self.M.max = max_iterations
  32. self.I.max_iterations = max_iterations
  33. def packet_in(self, packet):
  34. self.exception = None
  35. self.is_success = False
  36. # Match
  37. packet = self.M.packet_in(packet)
  38. if not self.M.is_success:
  39. self.exception = self.M.exception
  40. return packet
  41. # Choose the first match
  42. packet = self.I.packet_in(packet)
  43. if not self.I.is_success:
  44. self.exception = self.I.exception
  45. return packet
  46. while True:
  47. # Rewrite
  48. packet = self.W.packet_in(packet)
  49. if not self.W.is_success:
  50. self.exception = self.W.exception
  51. return packet
  52. # Choose another match
  53. packet = self.I.next_in(packet)
  54. # No more iterations are left
  55. if not self.I.is_success:
  56. if self.I.exception:
  57. self.exception = self.I.exception
  58. else:
  59. # Output success packet
  60. self.is_success = True
  61. return packet
  62. class FRule_r(ARule):
  63. '''
  64. Applies the transformation on all matches found.
  65. '''
  66. def __init__(self, LHS, RHS, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  67. '''
  68. Applies the transformation on all matches found.
  69. @param LHS: The pre-condition pattern (LHS + NACs).
  70. @param RHS: The post-condition pattern (RHS).
  71. @param max_iterations: The maximum number of times to apply the rule.
  72. @param external_matches_only: Resolve conflicts ignoring the matches found in this FRule.
  73. @param custom_resolution: Override the default resolution function.
  74. '''
  75. super(FRule_r, self).__init__(LHS, RHS, max_iterations)
  76. self.R = Resolver(external_matches_only=external_matches_only,
  77. custom_resolution=custom_resolution)
  78. def packet_in(self, packet):
  79. self.exception = None
  80. self.is_success = False
  81. # Match
  82. packet = self.M.packet_in(packet)
  83. if not self.M.is_success:
  84. self.exception = self.M.exception
  85. return packet
  86. # Choose the first match
  87. packet = self.I.packet_in(packet)
  88. if not self.I.is_success:
  89. self.exception = self.I.exception
  90. return packet
  91. while True:
  92. # Rewrite
  93. packet = self.W.packet_in(packet)
  94. if not self.W.is_success:
  95. self.exception = self.W.exception
  96. return packet
  97. # Resolve any conflicts if necessary
  98. packet = self.R.packet_in(packet)
  99. if not self.R.is_success:
  100. self.exception = self.R.exception
  101. return packet
  102. # Choose another match
  103. packet = self.I.next_in(packet)
  104. # No more iterations are left
  105. if not self.I.is_success:
  106. if self.I.exception:
  107. self.exception = self.I.exception
  108. else:
  109. # Output success packet
  110. self.is_success = True
  111. return packet