matcher.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 copy import deepcopy
  5. from ..util.infinity import INFINITY
  6. from ..core.match_algo import HimesisMatcher
  7. from ..core.himesis import HConstants as HC
  8. from rule_primitive import RulePrimitive
  9. from messages import MatchSet, Match, TransformationException
  10. class Matcher(RulePrimitive):
  11. '''
  12. Binds the source graph according to the pre-condition pattern.
  13. '''
  14. def __init__(self, condition, max=INFINITY):
  15. '''
  16. Binds the source graph according to the pre-condition pattern.
  17. @param condition: The pre-condition pattern.
  18. @param max: The maximum number of matches.
  19. '''
  20. super(Matcher, self).__init__()
  21. self.max = max
  22. self.condition = condition
  23. def __str__(self):
  24. s = super(Matcher, self).__str__()
  25. s = s.split(' ')
  26. s.insert(1, '[%s]' % self.condition.name)
  27. return reduce(lambda x, y: '%s %s' % (x,y), s)
  28. def packet_in(self, packet):
  29. self.exception = None
  30. self.is_success = False
  31. if self.condition[HC.GUID] in packet.match_sets:
  32. matchSet = packet.match_sets[self.condition[HC.GUID]]
  33. else:
  34. matchSet = MatchSet()
  35. # Find the matches
  36. try:
  37. i = 1
  38. if i <= self.max:
  39. for mapping in self._match(packet.graph, packet.global_pivots):
  40. # Convert the mapping to a Match object
  41. match = Match()
  42. match.from_mapping(mapping, packet.graph, self.condition)
  43. matchSet.matches.append(match)
  44. i += 1
  45. if i > self.max:
  46. # We don't need any more matches
  47. break
  48. except Exception, e:
  49. self.is_success = False
  50. self.exception = TransformationException(e)
  51. self.exception.packet = packet
  52. self.exception.transformation_unit = self
  53. return packet
  54. # Don't forget to add the match set to the packet, even if no matches were found
  55. if len(matchSet.matches) > 0:
  56. packet.match_sets[self.condition[HC.GUID]] = matchSet
  57. # Identify that this is the condition we are currently processing
  58. packet.current = self.condition[HC.GUID]
  59. # Success only if matches were found
  60. self.is_success = len(matchSet.matches) > 0
  61. return packet
  62. def _match(self, graph, pivots) :
  63. '''
  64. Matcher with pivots and (possibly) multiple NACs
  65. 1. Verify that no unbound NAC has a match
  66. 2. Let the "bridge" denote the biggest graph that is the intersection of the LHS and a NAC, among all NACs
  67. 3. Match the common part between the LHS & the NAC, i.e., the "bridge"
  68. 3.1 Continue the matching ensuring no occurrence of the NAC
  69. 3.2. If a NAC is found, ignore the current bridge mapping
  70. 3.3. Continue to find complete matches of the LHS,
  71. given each partial match found in 3.1.
  72. 3.4. For each valid match, verify that no occurrence of any remaining bound NAC is found,
  73. given the mapping found in 3.3.
  74. '''
  75. pred1 = {} # To optimize the matcher, since otherwise matcher will compute the predecessors of the source graph many times
  76. succ1 = {} # To optimize the matcher, since otherwise matcher will compute the successors of the source graph many times
  77. # Cache the pivot nodes of the source graph
  78. pivots = deepcopy(pivots)
  79. pivots.to_source_node_indices(graph)
  80. #===================================================================
  81. # First process the NACs that are not bound to the LHS
  82. #===================================================================
  83. for NAC in self.condition.getUnboundNACs():
  84. # Look for a NAC match
  85. nacMatcher = HimesisMatcher(source_graph=graph, pattern_graph=NAC)
  86. # Convert the pivots
  87. nac_pivots = pivots.to_mapping(graph, NAC)
  88. try:
  89. for mapping in nacMatcher.match_iter(context=nac_pivots):
  90. # Make the mapping into {...,NAClabel:graphIndex,...}
  91. match = Match()
  92. match.from_mapping(mapping, graph, self.condition)
  93. if NAC[HC.MT_CONSTRAINT](match.to_label_mapping(graph), graph):
  94. # An unbound NAC has been found: this pattern can never match
  95. return
  96. except: raise
  97. finally: nacMatcher.reset_recursion_limit()
  98. # For further matching optimizations
  99. pred1 = nacMatcher.pred1
  100. succ1 = nacMatcher.succ1
  101. # Either there are no NACs, or there were only unbound NACs that do not match, so match the LHS now
  102. if not self.condition.hasBoundNACs():
  103. lhsMatcher = HimesisMatcher(source_graph=graph, pattern_graph=self.condition, pred1=pred1, succ1=succ1)
  104. # Convert the pivots
  105. lhs_pivots = pivots.to_mapping(graph, self.condition)
  106. try:
  107. for mapping in lhsMatcher.match_iter(context=lhs_pivots):
  108. # Make the mapping into {...,LHSlabel:graphIndex,...}
  109. match = Match()
  110. match.from_mapping(mapping, graph, self.condition)
  111. if self.condition[HC.MT_CONSTRAINT](match.to_label_mapping(graph), graph):
  112. yield mapping
  113. except: raise
  114. finally: lhsMatcher.reset_recursion_limit()
  115. # The matching is complete
  116. return
  117. #===================================================================
  118. # Now process the NACs that have some nodes bound to the LHS
  119. #===================================================================
  120. # Continue the matching looking for the LHS now
  121. lhsMatcher = HimesisMatcher(source_graph=graph, pattern_graph=self.condition, pred1=pred1, succ1=succ1)
  122. # Augment the bridge mapping with the pivot mappings
  123. lhs_pivots = pivots.to_mapping(graph, self.condition)
  124. try:
  125. for mapping in lhsMatcher.match_iter(context=lhs_pivots):
  126. # Make the mapping into {...,LHSlabel:graphIndex,...}
  127. match = Match()
  128. match.from_mapping(mapping, graph, self.condition)
  129. if self.condition[HC.MT_CONSTRAINT](match.to_label_mapping(graph), graph):
  130. # A match of the LHS is found: ensure that no remaining NAC do match
  131. invalid = False
  132. for NAC in self.condition.getBoundNACs():
  133. # This mapping represents the mapping of the bridge of this NAC with the LHS
  134. bridgeMapping = match.to_mapping(graph, NAC)
  135. # Now continue the matching looking for a match of the corresponding NAC
  136. nacMatcher = HimesisMatcher(source_graph=graph, pattern_graph=NAC, pred1=pred1, succ1=succ1)
  137. for nac_mapping in nacMatcher.match_iter(context=bridgeMapping):
  138. # Make the mapping into {...,NAClabel:graphIndex,...}
  139. match = Match()
  140. match.from_mapping(nac_mapping, graph, NAC)
  141. if NAC[HC.MT_CONSTRAINT](match.to_label_mapping(graph), graph):
  142. # An occurrence of the NAC is found: current mapping is not valid
  143. invalid = True
  144. break
  145. if invalid:
  146. # An occurrence of the NAC was found: current mapping is not valid
  147. break
  148. else:
  149. # Either there are no bound NACs or no occurrence of any bound NAC was found: current mapping is valid
  150. yield mapping
  151. except: raise
  152. finally: lhsMatcher.reset_recursion_limit()