traffic_gen_javascript.xml 4.5 KB

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