lfrule.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.lrule import LRule
  17. from pytcore.tcore.rewriter import Rewriter
  18. from pytcore.tcore.resolver import Resolver
  19. class LFRule(LRule):
  20. '''
  21. Applies an inner rule for each application of the outer rule.
  22. '''
  23. def __init__(self, LHS, RHS, inner_rule, outer_first, sendAndApplyDeltaFunc, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  24. '''
  25. Applies an inner rule for each application of the outer rule.
  26. @param LHS: The pre-condition pattern (LHS + NACs).
  27. @param RHS: The post-condition pattern (RHS).
  28. @param inner_rule: The rule to apply in the loop.
  29. @param outer_first: Whether the outer rule should be applied before the inner rule.
  30. @param max_iterations: The maximum number of matches of the LHS.
  31. @param external_matches_only: Resolve conflicts ignoring the matches found in this FRule.
  32. @param custom_resolution: Override the default resolution function.
  33. '''
  34. super(LFRule, self).__init__(LHS, inner_rule, max_iterations)
  35. self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
  36. self.outer_first = outer_first
  37. self.R = Resolver(external_matches_only=external_matches_only,
  38. custom_resolution=custom_resolution)
  39. def packet_in(self, packet):
  40. self.exception = None
  41. self.is_success = False
  42. # Match
  43. packet = self.M.packet_in(packet)
  44. if not self.M.is_success:
  45. self.exception = self.M.exception
  46. return packet
  47. # Choose the first match
  48. packet = self.I.packet_in(packet)
  49. if not self.I.is_success:
  50. self.exception = self.I.exception
  51. return packet
  52. while True:
  53. if self.outer_first:
  54. # Rewrite
  55. packet = self.W.packet_in(packet)
  56. if not self.W.is_success:
  57. self.exception = self.W.exception
  58. return packet
  59. # Resolve any conflicts if necessary
  60. ''' hergin :: motif-integration commented '''
  61. #packet = self.R.packet_in(packet)
  62. #if not self.R.is_success:
  63. # self.exception = self.R.exception
  64. # return packet
  65. # Apply the inner rule
  66. packet = self.inner_rule.packet_in(packet)
  67. if not self.inner_rule.is_success:
  68. self.exception = self.inner_rule.exception
  69. return packet
  70. if not self.outer_first:
  71. # Rewrite
  72. packet = self.W.packet_in(packet)
  73. if not self.W.is_success:
  74. self.exception = self.W.exception
  75. return packet
  76. # Resolve any conflicts if necessary
  77. ''' hergin :: motif-integration commented '''
  78. #packet = self.R.packet_in(packet)
  79. #if not self.R.is_success:
  80. # self.exception = self.R.exception
  81. # return packet
  82. # Choose another match
  83. packet = self.I.next_in(packet)
  84. # No more iterations are left
  85. if not self.I.is_success:
  86. if self.I.exception:
  87. self.exception = self.I.exception
  88. else:
  89. # Output success packet
  90. self.is_success = True
  91. return packet