sccd.xml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?xml version="1.0" ?>
  2. <diagram author="Raphael Mannadiar" name="Traffic_Light_JavaScript_Version">
  3. <inport name="ui" />
  4. <class name="MainApp" default="true">
  5. <relationships>
  6. <association name="trafficlight" class="TrafficLight" />
  7. </relationships>
  8. <method name="MainApp">
  9. <body>
  10. <![CDATA[
  11. this.canvas = ui.append_canvas(ui.window,100,310,{'background':'#eee'});
  12. var police_button = ui.append_button(ui.window, 'Police interrupt');
  13. var quit_button = ui.append_button(ui.window, 'Quit');
  14. ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'police_interrupt_clicked');
  15. ui.bind_event(quit_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'quit_clicked');
  16. ]]>
  17. </body>
  18. </method>
  19. <scxml initial="initializing">
  20. <state id="initializing">
  21. <transition target="../creating">
  22. <raise scope="cd" event="create_instance">
  23. <parameter expr='"trafficlight"' />
  24. <parameter expr='"TrafficLight"' />
  25. <parameter expr="this.canvas" />
  26. </raise>
  27. </transition>
  28. </state>
  29. <state id="creating">
  30. <transition event="instance_created" target="../initialized">
  31. <parameter name="association_name" type="string"/>
  32. <raise scope="cd" event="start_instance">
  33. <parameter expr="association_name" />
  34. </raise>
  35. <raise scope="narrow" event="set_association_name" target="association_name">
  36. <parameter expr="association_name" />
  37. </raise>
  38. </transition>
  39. </state>
  40. <state id="initialized">
  41. </state>
  42. </scxml>
  43. </class>
  44. <class name="TrafficLight">
  45. <relationships>
  46. </relationships>
  47. <method name="TrafficLight">
  48. <parameter name="canvas" />
  49. <body>
  50. <![CDATA[
  51. var size = 100;
  52. var offset = size+5;
  53. this.RED = 0;
  54. this.YELLOW = 1;
  55. this.GREEN = 2;
  56. this.colors = ['#f00','#ff0','#0f0']
  57. this.lights = [canvas.add_rectangle(size/2, size/2, size, size, {'fill':'#000'}),
  58. canvas.add_rectangle(size/2, size/2+offset, size, size, {'fill':'#000'}),
  59. canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
  60. ]]>
  61. </body>
  62. </method>
  63. <method name="clear">
  64. <body>
  65. <![CDATA[
  66. this.lights[this.RED].set_color('#000');
  67. this.lights[this.YELLOW].set_color('#000');
  68. this.lights[this.GREEN].set_color('#000');
  69. ]]>
  70. </body>
  71. </method>
  72. <method name="setGreen">
  73. <body>
  74. <![CDATA[
  75. this.clear();
  76. this.lights[this.GREEN].set_color(this.colors[this.GREEN]);
  77. ]]>
  78. </body>
  79. </method>
  80. <method name="setYellow">
  81. <body>
  82. <![CDATA[
  83. this.clear();
  84. this.lights[this.YELLOW].set_color(this.colors[this.YELLOW]);
  85. ]]>
  86. </body>
  87. </method>
  88. <method name="setRed">
  89. <body>
  90. <![CDATA[
  91. this.clear();
  92. this.lights[this.RED].set_color(this.colors[this.RED]);
  93. ]]>
  94. </body>
  95. </method>
  96. <scxml initial="on">
  97. <state id="on" initial="normal">
  98. <state id="normal" initial="red">
  99. <state id="red">
  100. <onentry>
  101. <script>
  102. <![CDATA[
  103. this.setRed();
  104. ]]>
  105. </script>
  106. </onentry>
  107. <transition after='3' target='../green'/>
  108. </state>
  109. <state id="green">
  110. <onentry>
  111. <script>
  112. <![CDATA[
  113. this.setGreen();
  114. ]]>
  115. </script>
  116. </onentry>
  117. <transition after='2' target='../yellow'/>
  118. </state>
  119. <state id="yellow">
  120. <onentry>
  121. <script>
  122. <![CDATA[
  123. this.setYellow();
  124. ]]>
  125. </script>
  126. </onentry>
  127. <transition after='1' target='../red'/>
  128. </state>
  129. <transition event='police_interrupt_clicked' port='ui' target='../interrupted'/>
  130. <history id="history"/>
  131. </state>
  132. <state id="interrupted" initial="yellow">
  133. <state id="yellow">
  134. <onentry>
  135. <script>
  136. <![CDATA[
  137. this.setYellow();
  138. ]]>
  139. </script>
  140. </onentry>
  141. <transition after='.5' target='../black'/>
  142. </state>
  143. <state id="black">
  144. <onentry>
  145. <script>
  146. <![CDATA[
  147. this.clear();
  148. ]]>
  149. </script>
  150. </onentry>
  151. <transition after='.5' target='../yellow'/>
  152. </state>
  153. <transition event='police_interrupt_clicked' port='ui' target='../normal/history'/>
  154. </state>
  155. <transition event='quit_clicked' port='ui' target='../off'/>
  156. </state>
  157. <state id="off">
  158. <onentry>
  159. <script>
  160. <![CDATA[
  161. this.clear();
  162. ]]>
  163. </script>
  164. </onentry>
  165. </state>
  166. </scxml>
  167. </class>
  168. </diagram>