query.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ..tcore.composer import Composer
  16. from ..tcore.matcher import Matcher
  17. from ..tcore.iterator import Iterator
  18. class Query(Composer):
  19. '''
  20. Finds a match for the LHS.
  21. '''
  22. def __init__(self, LHS):
  23. '''
  24. Finds a match for the LHS.
  25. @param LHS: The pre-condition pattern (LHS + NACs).
  26. '''
  27. super(Query, self).__init__()
  28. self.M = Matcher(condition=LHS, max=1)
  29. self.I = Iterator(max_iterations=1)
  30. def packet_in(self, packet):
  31. self.exception = None
  32. self.is_success = False
  33. # Match
  34. packet = self.M.packet_in(packet)
  35. if not self.M.is_success:
  36. self.exception = self.M.exception
  37. return packet
  38. # Choose the only match
  39. packet = self.I.packet_in(packet)
  40. if not self.I.is_success:
  41. # Clean the packet: required since there is no Rewriter in a Query
  42. if len(packet.match_sets[self.I.condition].matches) == 0:
  43. del packet.match_sets[self.I.condition]
  44. self.exception = self.I.exception
  45. return packet
  46. # Output success packet
  47. self.is_success = True
  48. return packet
  49. class CQuery2(Composer):
  50. '''
  51. Finds a match for the LHS.
  52. '''
  53. def __init__(self, LHS, innerQuery):
  54. '''
  55. Finds a match for the LHS.
  56. @param LHS: The pre-condition pattern (LHS + NACs).
  57. '''
  58. super(CQuery2, self).__init__()
  59. self.M = Matcher(condition=LHS)
  60. self.I = Iterator()
  61. self.innerQuery=innerQuery
  62. def packet_in(self, packet):
  63. self.exception = None
  64. self.is_success = False
  65. # Match
  66. packet = self.M.packet_in(packet)
  67. if not self.M.is_success:
  68. self.exception = self.M.exception
  69. return packet
  70. # Choose the first match
  71. packet = self.I.packet_in(packet)
  72. if not self.I.is_success:
  73. self.exception = self.I.exception
  74. return packet
  75. while True:
  76. packet = self.innerQuery.packet_in(packet)
  77. if self.innerQuery.is_success:
  78. if self.innerQuery.exception:
  79. self.exception = self.innerQuery.exception
  80. return packet
  81. # Choose another match
  82. packet = self.I.next_in(packet)
  83. # No more iterations are left
  84. if not self.I.is_success:
  85. if self.I.exception:
  86. self.exception = self.I.exception
  87. else:
  88. # Output success packet
  89. self.is_success = False
  90. return packet
  91. else:
  92. self.is_success=True
  93. return packet
  94. class CQuery3(Composer):
  95. '''
  96. Finds a match for the LHS.
  97. '''
  98. def __init__(self, LHS, innerQuery, secondInnerQuery):
  99. '''
  100. Finds a match for the LHS.
  101. @param LHS: The pre-condition pattern (LHS + NACs).
  102. '''
  103. super(CQuery3, self).__init__()
  104. self.M = Matcher(condition=LHS)
  105. self.I = Iterator()
  106. self.innerQuery=innerQuery
  107. self.secondInnerQuery=secondInnerQuery
  108. def packet_in(self, packet):
  109. self.exception = None
  110. self.is_success = False
  111. # Match
  112. packet = self.M.packet_in(packet)
  113. if not self.M.is_success:
  114. self.exception = self.M.exception
  115. return packet
  116. # Choose the first match
  117. packet = self.I.packet_in(packet)
  118. if not self.I.is_success:
  119. self.exception = self.I.exception
  120. return packet
  121. while True:
  122. packet = self.innerQuery.packet_in(packet)
  123. if self.innerQuery.is_success:
  124. if self.innerQuery.exception:
  125. self.exception = self.innerQuery.exception
  126. return packet
  127. # Choose another match
  128. packet = self.I.next_in(packet)
  129. # No more iterations are left
  130. if not self.I.is_success:
  131. if self.I.exception:
  132. self.exception = self.I.exception
  133. else:
  134. self.is_success = False
  135. return packet
  136. else:
  137. packet = self.secondInnerQuery.packet_in(packet)
  138. if self.secondInnerQuery.is_success:
  139. if self.secondInnerQuery.exception:
  140. self.exception = self.secondInnerQuery.exception
  141. return packet
  142. packet = self.I.next_in(packet)
  143. if not self.I.is_success:
  144. if self.I.exception:
  145. self.exception = self.I.exception
  146. else:
  147. self.is_success = False
  148. return packet
  149. else:
  150. self.is_success=True
  151. return packet