crule.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.util.tconstants import TConstants as TC
  17. class CRule(Composer):
  18. def __init__(self,motifContext):
  19. self._mtContexts = []
  20. self._mtContexts.append(motifContext)
  21. self.cruleEnd = False
  22. self.packet = None
  23. def packet_in(self, packet):
  24. self.cruleEnd = False
  25. self._mtContexts[-1]._expired=False
  26. self._mtContexts[-1]._lastStep={}
  27. self.exception = None
  28. self.is_success = False
  29. self.packet = packet
  30. while not self.cruleEnd:
  31. result = self._step()
  32. self.is_success = result if result != None else False
  33. return self.packet
  34. def _nextRule(self) :
  35. mtc = self._mtContexts[-1]
  36. ns = mtc.nextStep()
  37. if 'id' in ns :
  38. fulltype = mtc.t['nodes'][ns['id']]['$type']
  39. if fulltype == mtc.metamodel+"CRule":
  40. #self._loadTransform(ns['rule'])
  41. return self._nextRule()
  42. elif fulltype.startswith('/Formalisms/__Transformations__/Transformation/T-Core') or \
  43. fulltype.startswith('/Formalisms/__Transformations__/Transformation/MoTif'):
  44. return ns
  45. elif 'trafoResult' in ns:
  46. return ns;
  47. def _step(self) :
  48. def runRule(r) :
  49. ar = r['rule']
  50. self.packet = ar.packet_in(self.packet)
  51. if ar.is_success :
  52. return (self.packet.deltas,TC.SUCCEEDED)
  53. elif not ar.is_success :
  54. if ar.exception :
  55. return (str(ar.exception),TC.EXCEPTION)
  56. else :
  57. return (None,TC.FAILED)
  58. try :
  59. nr = self._nextRule()
  60. except Exception :
  61. self.cruleEnd = True
  62. return
  63. if 'trafoResult' in nr:
  64. self.cruleEnd = True
  65. return nr['trafoResult']
  66. if nr.__class__ != {}.__class__ :
  67. self.cruleEnd = True
  68. return
  69. elif '$err' in nr :
  70. self.cruleEnd = True
  71. return
  72. else :
  73. (res,ai) = runRule(nr)
  74. self._mtContexts[-1].setLastStepApplicationInfo(ai)
  75. if ai == TC.FAILED :
  76. pass
  77. #self._aswPrintReq(TC.RULE_FAILURE_MSG+" ("+self._mtContexts[-1]._lastStep['alias']+":"+self._mtContexts[-1]._lastStep['name']+")")
  78. elif ai == TC.EXCEPTION :
  79. pass
  80. #self._aswPrintReq(TC.RULE_EXCEPTION_MSG + res)
  81. else :
  82. return True
  83. #self._mtContexts[-1].setLastStepFeedbackReceived()