dynamic_trafficlight.mvc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. include "primitives.alh"
  2. Diagram dynamic_trafficlight {
  3. name = "Dynamic Traffic Light"
  4. author = "Yentl Van Tendeloo"
  5. }
  6. Class manager {
  7. name = "Manager"
  8. default = True
  9. constructor_body = $
  10. Void function constructor(attributes : Element, parameters : Element):
  11. dict_add(attributes, "counter", 0)
  12. return !
  13. $
  14. {behaviour} CompositeState manager_main {
  15. name = "main"
  16. isInitial = True
  17. {composite_children} BasicState manager_main_start {
  18. name = "start"
  19. isInitial = True
  20. }
  21. }
  22. }
  23. transition (manager_main_start, manager_main_start) {
  24. name = "create"
  25. event = "create"
  26. {transition_raises} Raise {
  27. scope = "cd"
  28. event = "create_instance"
  29. parameter = $
  30. Element function raise(attributes : Element, parameters : Element):
  31. Element result
  32. result = create_node()
  33. list_append(result, "TrafficLight")
  34. list_append(result, attributes["counter"])
  35. list_append(result, read_root())
  36. dict_overwrite(attributes, "counter", integer_addition(attributes["counter"], 1))
  37. return result!
  38. $
  39. }
  40. }
  41. transition (manager_main_start, manager_main_start) {
  42. name = "delete"
  43. event = "delete"
  44. {transition_raises} Raise {
  45. scope = "cd"
  46. event = "delete_instance"
  47. parameter = $
  48. Element function raise(attributes : Element, parameters : Element):
  49. Element result
  50. result = create_node()
  51. list_append(result, parameters["id"])
  52. return result!
  53. $
  54. }
  55. }
  56. Class trafficlight {
  57. name = "TrafficLight"
  58. default = False
  59. constructor_body = $
  60. Void function constructor(attributes : Element, parameters : Element):
  61. dict_add(attributes, "counter", 0)
  62. dict_add(attributes, "colour", "")
  63. return!
  64. $
  65. {behaviour} CompositeState trafficlight_main {
  66. name = "main"
  67. isInitial = True
  68. {composite_children} BasicState trafficlight_main_off {
  69. name = "off"
  70. isInitial = True
  71. }
  72. {composite_children} ParallelState trafficlight_main_main {
  73. name = "main"
  74. isInitial = False
  75. {onExitRaise} Raise {
  76. event = "displayNone"
  77. }
  78. {parallel_children} CompositeState trafficlight_main_main_trafficlight {
  79. name = "trafficlight"
  80. isInitial = False
  81. {composite_children} CompositeState trafficlight_main_main_trafficlight_normal {
  82. name = "normal"
  83. isInitial = True
  84. onExitScript = $
  85. Void function onexit(attributes : Element):
  86. dict_overwrite(attributes, "colour", "")
  87. return!
  88. $
  89. {composite_children} BasicState trafficlight_main_main_trafficlight_normal_red {
  90. name = "red"
  91. isInitial = True
  92. onEntryScript = $
  93. Void function onentry(attributes : Element):
  94. dict_overwrite(attributes, "counter", 60)
  95. dict_overwrite(attributes, "colour", "red")
  96. return!
  97. $
  98. {onEntryRaise} Raise {
  99. event = "displayRed"
  100. }
  101. {onEntryRaise} Raise {
  102. event = "resetTimer"
  103. }
  104. }
  105. {composite_children} BasicState trafficlight_main_main_trafficlight_normal_green {
  106. name = "green"
  107. isInitial = False
  108. onEntryScript = $
  109. Void function onentry(attributes : Element):
  110. dict_overwrite(attributes, "counter", 55)
  111. dict_overwrite(attributes, "colour", "green")
  112. return!
  113. $
  114. {onEntryRaise} Raise {
  115. event = "displayGreen"
  116. }
  117. {onEntryRaise} Raise {
  118. event = "resetTimer"
  119. }
  120. }
  121. {composite_children} BasicState trafficlight_main_main_trafficlight_normal_yellow {
  122. name = "yellow"
  123. isInitial = False
  124. onEntryScript = $
  125. Void function onentry(attributes : Element):
  126. dict_overwrite(attributes, "colour", "")
  127. return!
  128. $
  129. {onEntryRaise} Raise {
  130. event = "displayYellow"
  131. }
  132. {onEntryRaise} Raise {
  133. event = "disableTimer"
  134. }
  135. }
  136. {composite_children} HistoryState trafficlight_main_main_trafficlight_normal_hist {
  137. name = "hist"
  138. }
  139. }
  140. {composite_children} CompositeState trafficlight_main_main_trafficlight_interrupted {
  141. {composite_children} BasicState trafficlight_main_main_trafficlight_interrupted_yellow {
  142. name = "yellow"
  143. isInitial = True
  144. {onEntryRaise} Raise {
  145. event = "displayYellow"
  146. }
  147. }
  148. {composite_children} BasicState trafficlight_main_main_trafficlight_interrupted_black {
  149. name = "black"
  150. isInitial = False
  151. {onEntryRaise} Raise {
  152. event = "displayNone"
  153. }
  154. }
  155. }
  156. }
  157. {parallel_children} CompositeState trafficlight_main_main_timer {
  158. name = "timer"
  159. isInitial = False
  160. {composite_children} BasicState trafficlight_main_main_timer_disabled {
  161. name = "disabled"
  162. isInitial = False
  163. }
  164. {composite_children} CompositeState trafficlight_main_main_timer_running {
  165. name = "running"
  166. isInitial = True
  167. {onExitRaise} Raise {
  168. event = "updateTimerValue"
  169. parameter = $
  170. Element function parameter(attributes : Element):
  171. return -1!
  172. $
  173. }
  174. {composite_children} BasicState trafficlight_main_main_timer_running_decidingcolor {
  175. name = "decidingcolor"
  176. isInitial = True
  177. }
  178. {composite_children} BasicState trafficlight_main_main_timer_running_green {
  179. name = "green"
  180. isInitial = False
  181. {onEntryRaise} Raise {
  182. event = "updateTimerValue"
  183. parameter = $
  184. Element function raise(attributes : Element):
  185. return attributes["counter"]!
  186. $
  187. }
  188. }
  189. {composite_children} BasicState trafficlight_main_main_timer_running_red {
  190. name = "red"
  191. isInitial = False
  192. {onEntryRaise} Raise {
  193. event = "updateTimerValue"
  194. parameter = $
  195. Element function raise(attributes : Element):
  196. return attributes["counter"]!
  197. $
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. transition (trafficlight_main_off, trafficlight_main_main) {
  206. name = "toggle"
  207. event = "toggle"
  208. }
  209. transition (trafficlight_main_main, trafficlight_main_off) {
  210. name = "toggle"
  211. event = "toggle"
  212. }
  213. transition (trafficlight_main_main_trafficlight_normal, trafficlight_main_main_trafficlight_interrupted) {
  214. name = "police_interrupt / disableTimer"
  215. event = "police_interrupt"
  216. {transition_raises} Raise {
  217. event = "disableTimer"
  218. }
  219. }
  220. transition (trafficlight_main_main_trafficlight_interrupted, trafficlight_main_main_trafficlight_normal_hist) {
  221. name = "police_interrupt / enableTimer"
  222. event = "police_interrupt"
  223. {transition_raises} Raise {
  224. event = "enableTimer"
  225. }
  226. }
  227. transition (trafficlight_main_main_trafficlight_normal_red, trafficlight_main_main_trafficlight_normal_green) {
  228. name = "after 60s"
  229. after = $
  230. Float function after(attributes : Element, parameters : Element):
  231. return 60.0!
  232. $
  233. }
  234. transition (trafficlight_main_main_trafficlight_normal_green, trafficlight_main_main_trafficlight_normal_yellow) {
  235. name = "after 55s"
  236. after = $
  237. Float function after(attributes : Element, parameters : Element):
  238. return 55.0!
  239. $
  240. }
  241. transition (trafficlight_main_main_trafficlight_normal_yellow, trafficlight_main_main_trafficlight_normal_red) {
  242. name = "after 5s"
  243. after = $
  244. Float function after(attributes : Element, parameters : Element):
  245. return 5.0!
  246. $
  247. {transition_raises} Raise {
  248. event = "enableTimer"
  249. }
  250. }
  251. transition (trafficlight_main_main_trafficlight_interrupted_yellow, trafficlight_main_main_trafficlight_interrupted_black) {
  252. name = "after 500ms"
  253. after = $
  254. Float function after(attributes : Element, parameters : Element):
  255. return 0.5!
  256. $
  257. }
  258. transition (trafficlight_main_main_trafficlight_interrupted_black, trafficlight_main_main_trafficlight_interrupted_yellow) {
  259. name = "after 500ms"
  260. after = $
  261. Float function after(attributes : Element, parameters : Element):
  262. return 0.5!
  263. $
  264. }
  265. transition (trafficlight_main_main_timer_disabled, trafficlight_main_main_timer_running) {
  266. name = "enableTimer"
  267. event = "enableTimer"
  268. }
  269. transition (trafficlight_main_main_timer_running, trafficlight_main_main_timer_disabled) {
  270. name = "disableTimer"
  271. event = "disableTimer"
  272. }
  273. transition (trafficlight_main_main_timer_running, trafficlight_main_main_timer_running) {
  274. name = "resetTimer"
  275. event = "resetTimer"
  276. }
  277. transition (trafficlight_main_main_timer_running_decidingcolor, trafficlight_main_main_timer_running_green) {
  278. name = "[green] / updateTimerColour"
  279. cond = $
  280. Boolean function cond(attributes : Element, parameters : Element):
  281. return value_eq(attributes["colour"], "green")!
  282. $
  283. {transition_raises} Raise {
  284. event = "updateTimerColour"
  285. parameter = $
  286. Element function param(attributes : Element, parameters : Element):
  287. return "green"!
  288. $
  289. }
  290. }
  291. transition (trafficlight_main_main_timer_running_decidingcolor, trafficlight_main_main_timer_running_red) {
  292. name = "[red] / updateTimerColour"
  293. cond = $
  294. Boolean function cond(attributes : Element, parameters : Element):
  295. return value_eq(attributes["colour"], "red")!
  296. $
  297. {transition_raises} Raise {
  298. event = "updateTimerColour"
  299. parameter = $
  300. Element function param(attributes : Element, parameters : Element):
  301. return "red"!
  302. $
  303. }
  304. }
  305. transition (trafficlight_main_main_timer_running_red, trafficlight_main_main_timer_running_red) {
  306. name = "after 1s / counter -= 1"
  307. after = $
  308. Float function after(attributes : Element, parameters : Element):
  309. return 1.0!
  310. $
  311. script = $
  312. Void function script(attributes : Element, parameters : Element):
  313. dict_overwrite(attributes, "counter", integer_subtraction(attributes["counter"], 1))
  314. return!
  315. $
  316. }
  317. transition (trafficlight_main_main_timer_running_green, trafficlight_main_main_timer_running_green) {
  318. name = "after 1s / counter -= 1"
  319. after = $
  320. Float function after(attributes : Element, parameters : Element):
  321. return 1.0!
  322. $
  323. script = $
  324. Void function script(attributes : Element, parameters : Element):
  325. dict_overwrite(attributes, "counter", integer_subtraction(attributes["counter"], 1))
  326. return!
  327. $
  328. }