dynamic_trafficlight.mvc 13 KB

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