lfrule.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ..util.infinity import INFINITY
  16. from lrule import LRule
  17. from ..tcore.rewriter import Rewriter
  18. from ..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