query.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright 2023 Modelling, Simulation and Design Lab (MSDL)
  2. # at the University of Antwerp (http://msdl.uantwerpen.be/).
  3. #
  4. # Licensed under the GNU Public License v3 (the "License");
  5. # you may not use this file except in compliance with the
  6. # License. You may obtain a copy of the License at
  7. # https://www.gnu.org/licenses/gpl-3.0.en.html
  8. #
  9. # Unless required by applicable law or agreed to in writing,
  10. # software distributed under the License is distributed on an
  11. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. # either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the
  14. # License.
  15. from pytcore.tcore.composer import Composer
  16. from pytcore.tcore.matcher import Matcher
  17. from pytcore.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