05_advanced_transformation.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # In this tutorial, we implement the semantics of Petri Nets by means of model transformation.
  2. # Compared to the previous tutorial, it only introduces one more feature: pivots.
  3. # Consider the following Petri Net language meta-model:
  4. mm_cs = """
  5. Place:Class
  6. Transition:Class
  7. Place_tokens:AttributeLink (Place -> Integer) {
  8. optional = False;
  9. name = "tokens";
  10. constraint = `get_value(get_target(this)) >= 0`;
  11. }
  12. P2T:Association (Place -> Transition)
  13. T2P:Association (Transition -> Place)
  14. P2T_weight:AttributeLink (P2T -> Integer) {
  15. optional = False;
  16. name = "weight";
  17. constraint = `get_value(get_target(this)) >= 0`;
  18. }
  19. T2P_weight:AttributeLink (T2P -> Integer) {
  20. optional = False;
  21. name = "weight";
  22. constraint = `get_value(get_target(this)) >= 0`;
  23. }
  24. """
  25. # We now create the following Petri Net:
  26. # https://upload.wikimedia.org/wikipedia/commons/4/4d/Two-boundedness-cb.png
  27. m_cs = """
  28. p1:Place { tokens = 0; }
  29. p2:Place { tokens = 0; }
  30. cp1:Place { tokens = 2; }
  31. cp2:Place { tokens = 2; }
  32. t1:Transition
  33. t2:Transition
  34. t3:Transition
  35. :T2P (t1 -> p1) { weight = 1; }
  36. :P2T (p1 -> t2) { weight = 1; }
  37. :T2P (t2 -> cp1) { weight = 1; }
  38. :P2T (cp1 -> t1) { weight = 1; }
  39. :T2P (t2 -> p2) { weight = 1; }
  40. :P2T (p2 -> t3) { weight = 1; }
  41. :T2P (t3 -> cp2) { weight = 1; }
  42. :P2T (cp2 -> t2) { weight = 1; }
  43. """
  44. # The usual...
  45. from state.devstate import DevState
  46. from bootstrap.scd import bootstrap_scd
  47. from util import loader
  48. from transformation.ramify import ramify
  49. from transformation.matcher import match_od
  50. from transformation.cloner import clone_od
  51. from transformation import rewriter
  52. from concrete_syntax.textual_od.renderer import render_od
  53. from concrete_syntax.common import indent
  54. from api.od import ODAPI
  55. state = DevState()
  56. mmm = bootstrap_scd(state)
  57. mm = loader.parse_and_check(state, mm_cs, mmm, "mm")
  58. m = loader.parse_and_check(state, m_cs, mm, "m")
  59. mm_ramified = ramify(state, mm)
  60. # We will now implement Petri Net operational semantics by means of model transformation.
  61. # Look for any transition:
  62. lhs_transition_cs = """
  63. t:RAM_Transition
  64. """
  65. # But, if that transition has an incoming arc (P2T) from a place with not enough tokens, the transition cannot fire. We can express this as a pattern:
  66. lhs_transition_disabled_cs = """
  67. t:RAM_Transition
  68. p:RAM_Place
  69. :RAM_P2T (p -> t) {
  70. condition = ```
  71. place = get_source(this)
  72. tokens = get_slot_value(place, "tokens")
  73. weight = get_slot_value(this, "weight")
  74. tokens < weight # True means: cannot fire
  75. ```;
  76. }
  77. """
  78. # Parse these patterns:
  79. lhs_transition = loader.parse_and_check(state, lhs_transition_cs, mm_ramified, "lhs_transition")
  80. lhs_transition_disabled = loader.parse_and_check(state, lhs_transition_disabled_cs, mm_ramified, "lhs_transition_disabled")
  81. # To find enabled transitions, we first match our first pattern (looking for a transition), and then we try to 'grow' that match with our second, "Negative Application Condition" (NAC) pattern. If growing the match with the second pattern is possible, we abort and look for another transition.
  82. # To grow a match, we use the 'pivot'-argument of the match-function. A pivot is a partial match that needs to be grown.
  83. # This results in the following generator function:
  84. def find_enabled_transitions(m):
  85. for match in match_od(state, m, mm, lhs_transition, mm_ramified):
  86. for match_nac in match_od(state, m, mm, lhs_transition_disabled, mm_ramified, pivot=match): # <-- notice the pivot :)
  87. # transition is disabled
  88. break # find next transition
  89. else:
  90. # we've found an enabled transition:
  91. yield match
  92. # Let's see if it works:
  93. enabled = list(find_enabled_transitions(m))
  94. print("enabled PN transitions:", enabled)
  95. # Next, to fire a transition:
  96. # - we decrement the number of tokens of every incoming place
  97. # - we increment the number of tokens of every outgoing place
  98. # We do this also by growing our match: given an enabled transition (already matched), we match for *any* incoming place, and rewrite that place to reduce its tokens. Next, we look for *any* outgoing place, and increment its tokens.
  99. # Decrement incoming
  100. lhs_incoming_cs = """
  101. t:RAM_Transition # <-- we already know this transition is enabled
  102. inplace:RAM_Place {
  103. RAM_tokens = `True`; # this needs to be here, otherwise, the rewriter will try to create a new attribute rather than update the existing one
  104. }
  105. inarc:RAM_P2T (inplace -> t)
  106. """
  107. rhs_incoming_cs = """
  108. t:RAM_Transition
  109. inplace:RAM_Place {
  110. RAM_tokens = ```
  111. weight = get_slot_value(matched("inarc"), "weight")
  112. print("adding", weight, "tokens to", get_name(this))
  113. get_value(this) - weight
  114. ```;
  115. }
  116. inarc:RAM_P2T (inplace -> t)
  117. """
  118. # Increment outgoing
  119. lhs_outgoing_cs = """
  120. t:RAM_Transition
  121. outplace:RAM_Place {
  122. RAM_tokens = `True`; # this needs to be here, otherwise, the rewriter will try to create a new attribute rather than update the existing one
  123. }
  124. outarc:RAM_T2P (t -> outplace)
  125. """
  126. rhs_outgoing_cs = """
  127. t:RAM_Transition
  128. outplace:RAM_Place {
  129. RAM_tokens = ```
  130. weight = get_slot_value(matched("outarc"), "weight")
  131. print("removing", weight, "tokens from", get_name(this))
  132. get_value(this) + weight
  133. ```;
  134. }
  135. outarc:RAM_T2P (t -> outplace)
  136. """
  137. # Parse all the patterns
  138. lhs_incoming = loader.parse_and_check(state, lhs_incoming_cs, mm_ramified, "lhs_incoming")
  139. rhs_incoming = loader.parse_and_check(state, rhs_incoming_cs, mm_ramified, "rhs_incoming")
  140. lhs_outgoing = loader.parse_and_check(state, lhs_outgoing_cs, mm_ramified, "lhs_outgoing")
  141. rhs_outgoing = loader.parse_and_check(state, rhs_outgoing_cs, mm_ramified, "rhs_outgoing")
  142. # Firing is really simple:
  143. def fire_transition(m, transition_match):
  144. for match_incoming in match_od(state, m, mm, lhs_incoming, mm_ramified, pivot=transition_match):
  145. rewriter.rewrite(state, rhs_incoming, mm_ramified, match_incoming, m, mm)
  146. for match_outgoing in match_od(state, m, mm, lhs_outgoing, mm_ramified, pivot=transition_match):
  147. rewriter.rewrite(state, rhs_outgoing, mm_ramified, match_outgoing, m, mm)
  148. def show_petri_net(m):
  149. odapi = ODAPI(state, m, mm)
  150. p1 = odapi.get_slot_value(odapi.get("p1"), "tokens")
  151. p2 = odapi.get_slot_value(odapi.get("p2"), "tokens")
  152. cp1 = odapi.get_slot_value(odapi.get("cp1"), "tokens")
  153. cp2 = odapi.get_slot_value(odapi.get("cp2"), "tokens")
  154. return f"""
  155. t1 t2 t3
  156. ███ p1 ███ p2 ███
  157. ███ ███ ███
  158. ███─────► ( {p1} )─────►███─────► ( {p2} )─────►███
  159. ███ ███ ███
  160. ▲ │ ▲ │
  161. │ │ │ │
  162. │ │ │ │
  163. │ │ │ │
  164. │ │ │ │
  165. └───────( {cp1} )◄──────┘ └──────( {cp2} )◄───────┘
  166. cp1 cp2 """
  167. # Let's see if it works:
  168. while len(enabled) > 0:
  169. print(show_petri_net(m))
  170. print("\nenabled PN transitions:", enabled)
  171. to_fire = enabled[0]['t']
  172. print("press ENTER to fire", to_fire)
  173. input()
  174. print("firing transition:", to_fire)
  175. fire_transition(m, enabled[0])
  176. enabled = list(find_enabled_transitions(m))
  177. # That's it!