arule.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from pytcore.tcore.matcher import Matcher
  17. from pytcore.tcore.iterator import Iterator
  18. from pytcore.tcore.rewriter import Rewriter
  19. from pytcore.tcore.resolver import Resolver
  20. class ARule(Composer):
  21. '''
  22. Applies the transformation on one match.
  23. '''
  24. def __init__(self, LHS, RHS,sendAndApplyDeltaFunc):
  25. '''
  26. Applies the transformation on one match.
  27. @param LHS: The pre-condition pattern (LHS + NACs).
  28. @param RHS: The post-condition pattern (RHS).
  29. '''
  30. super(ARule, self).__init__()
  31. self.M = Matcher(condition=LHS, max=1)
  32. self.I = Iterator(max_iterations=1)
  33. self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
  34. def packet_in(self, packet):
  35. self.exception = None
  36. self.is_success = False
  37. # Match
  38. packet = self.M.packet_in(packet)
  39. if not self.M.is_success:
  40. self.exception = self.M.exception
  41. self.M.packet_in(packet)
  42. return packet
  43. # Choose the only match
  44. packet = self.I.packet_in(packet)
  45. if not self.I.is_success:
  46. self.exception = self.I.exception
  47. return packet
  48. # Rewrite
  49. packet = self.W.packet_in(packet)
  50. if not self.W.is_success:
  51. self.exception = self.W.exception
  52. return packet
  53. # Output success packet
  54. self.is_success = True
  55. return packet
  56. class ARule_r(ARule):
  57. '''
  58. Applies the transformation on one match.
  59. '''
  60. def __init__(self, LHS, RHS, external_matches_only=False, custom_resolution=lambda packet: False):
  61. '''
  62. Applies the transformation on one match.
  63. @param LHS: The pre-condition pattern (LHS + NACs).
  64. @param RHS: The post-condition pattern (RHS).
  65. @param external_matches_only: Resolve conflicts ignoring the matches found in this ARule.
  66. @param custom_resolution: Override the default resolution function.
  67. '''
  68. super(ARule_r, self).__init__(LHS, RHS)
  69. self.R = Resolver(external_matches_only=external_matches_only,
  70. custom_resolution=custom_resolution)
  71. def packet_in(self, packet):
  72. packet = super(ARule_r, self).packet_in(packet)
  73. # is_success is True
  74. if self.exception is None:
  75. # Resolve any conflicts if necessary
  76. packet = self.R.packet_in(packet)
  77. if not self.R.is_success:
  78. self.exception = self.R.exception
  79. return packet
  80. # Output success packet
  81. else:
  82. self.is_success = False
  83. return packet