traffic.js.xml 4.5 KB

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