lfrule.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. '''This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. See COPYING.lesser and README.md in the root of this project for full details'''
  4. from ..util.infinity import INFINITY
  5. from lrule import LRule
  6. from ..tcore.rewriter import Rewriter
  7. from ..tcore.resolver import Resolver
  8. class LFRule(LRule):
  9. '''
  10. Applies an inner rule for each application of the outer rule.
  11. '''
  12. def __init__(self, LHS, RHS, inner_rule, outer_first, sendAndApplyDeltaFunc, max_iterations=INFINITY, external_matches_only=False, custom_resolution=lambda packet: False):
  13. '''
  14. Applies an inner rule for each application of the outer rule.
  15. @param LHS: The pre-condition pattern (LHS + NACs).
  16. @param RHS: The post-condition pattern (RHS).
  17. @param inner_rule: The rule to apply in the loop.
  18. @param outer_first: Whether the outer rule should be applied before the inner rule.
  19. @param max_iterations: The maximum number of matches of the LHS.
  20. @param external_matches_only: Resolve conflicts ignoring the matches found in this FRule.
  21. @param custom_resolution: Override the default resolution function.
  22. '''
  23. super(LFRule, self).__init__(LHS, inner_rule, max_iterations)
  24. self.W = Rewriter(condition=RHS,sendAndApplyDeltaFunc=sendAndApplyDeltaFunc)
  25. self.outer_first = outer_first
  26. self.R = Resolver(external_matches_only=external_matches_only,
  27. custom_resolution=custom_resolution)
  28. def packet_in(self, packet):
  29. self.exception = None
  30. self.is_success = False
  31. # Match
  32. packet = self.M.packet_in(packet)
  33. if not self.M.is_success:
  34. self.exception = self.M.exception
  35. return packet
  36. # Choose the first match
  37. packet = self.I.packet_in(packet)
  38. if not self.I.is_success:
  39. self.exception = self.I.exception
  40. return packet
  41. while True:
  42. if self.outer_first:
  43. # Rewrite
  44. packet = self.W.packet_in(packet)
  45. if not self.W.is_success:
  46. self.exception = self.W.exception
  47. return packet
  48. # Resolve any conflicts if necessary
  49. ''' hergin :: motif-integration commented '''
  50. #packet = self.R.packet_in(packet)
  51. #if not self.R.is_success:
  52. # self.exception = self.R.exception
  53. # return packet
  54. # Apply the inner rule
  55. packet = self.inner_rule.packet_in(packet)
  56. if not self.inner_rule.is_success:
  57. self.exception = self.inner_rule.exception
  58. return packet
  59. if not self.outer_first:
  60. # Rewrite
  61. packet = self.W.packet_in(packet)
  62. if not self.W.is_success:
  63. self.exception = self.W.exception
  64. return packet
  65. # Resolve any conflicts if necessary
  66. ''' hergin :: motif-integration commented '''
  67. #packet = self.R.packet_in(packet)
  68. #if not self.R.is_success:
  69. # self.exception = self.R.exception
  70. # return packet
  71. # Choose another match
  72. packet = self.I.next_in(packet)
  73. # No more iterations are left
  74. if not self.I.is_success:
  75. if self.I.exception:
  76. self.exception = self.I.exception
  77. else:
  78. # Output success packet
  79. self.is_success = True
  80. return packet