query.py 5.2 KB

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