dynamic_trafficlight.mvc 13 KB

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