examples.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. Examples
  2. ========
  3. Timer
  4. -----
  5. This example demonstrates the timed behavior of SCCD. It does not have dynamic structure.
  6. We model a clock which prints the current time every 0.05 seconds. Two clocks are printed: the current wall-clock time and the current simulated time. We expect both to (almost) be the same. The user can interrupt the clock by sending an "interrupt" event. The user can resume the clock by sending a "resume" event.
  7. Threads (Python)
  8. ^^^^^^^^^^^^^^^^
  9. In this version, the model sends the current times to an output port, on which the user listens, to print out these times. *self.getSimulatedTime()* and *time()* return the current time in milliseconds, which we have to convert to seconds.
  10. The SCCD model::
  11. <?xml version="1.0" ?>
  12. <diagram author="Simon Van Mierlo" name="Timer (Threaded Version)">
  13. <top>
  14. from sccd.runtime.accurate_time import time
  15. </top>
  16. <inport name="input" />
  17. <outport name="output" />
  18. <class name="MainApp" default="true">
  19. <scxml initial="running">
  20. <state id="running">
  21. <transition target="." after="0.05">
  22. <raise event="time_update" port="output">
  23. <parameter expr="self.getSimulatedTime()" />
  24. <parameter expr="time()" />
  25. </raise>
  26. </transition>
  27. <transition target="../interrupted" event="interrupt" port="input">
  28. <raise event="time_update" port="output">
  29. <parameter expr="self.getSimulatedTime()" />
  30. <parameter expr="time()" />
  31. </raise>
  32. </transition>
  33. </state>
  34. <state id="interrupted">
  35. <transition target="." event="interrupt" port="input">
  36. <raise event="time_update" port="output">
  37. <parameter expr="self.getSimulatedTime()" />
  38. <parameter expr="time()" />
  39. </raise>
  40. </transition>
  41. <transition target="../running" event="continue" port="input">
  42. <raise event="time_update" port="output">
  43. <parameter expr="self.getSimulatedTime()" />
  44. <parameter expr="time()" />
  45. </raise>
  46. </transition>
  47. </state>
  48. </scxml>
  49. </class>
  50. </diagram>
  51. To compile, save this in a file called ``timer.xml`` and run ``python -m sccd.compiler.sccdc -p threads -l python timer.xml``
  52. Then, the following file will run the model::
  53. import timer
  54. from sccd.runtime.statecharts_core import Event
  55. import threading
  56. if __name__ == '__main__':
  57. controller = timer.Controller()
  58. def raw_inputter():
  59. while 1:
  60. controller.addInput(Event(raw_input(), "input", []))
  61. input_thread = threading.Thread(target=raw_inputter)
  62. input_thread.daemon = True
  63. input_thread.start()
  64. output_listener = controller.addOutputListener(["output"])
  65. def outputter():
  66. while 1:
  67. event = output_listener.fetch(-1)
  68. print "SIMTIME: %.2fs" % (event.getParameters()[0] / 1000.0)
  69. print "ACTTIME: %.2fs" % (event.getParameters()[1] / 1000.0)
  70. output_thread = threading.Thread(target=outputter)
  71. output_thread.daemon = True
  72. output_thread.start()
  73. controller.start()
  74. The time will be printed to the console. The user can send events by typing the string "interrupt" or "continue" in the console.
  75. Eventloop (Python)
  76. ^^^^^^^^^^^^^^^^^^
  77. The SCCD model::
  78. <?xml version="1.0" ?>
  79. <diagram author="Simon Van Mierlo" name="Timer (Eventloop Version)">
  80. <top>
  81. from sccd.runtime.libs.ui import ui
  82. from sccd.runtime.accurate_time import time
  83. </top>
  84. <inport name="ui" />
  85. <class name="MainApp" default="true">
  86. <method name="MainApp">
  87. <body>
  88. <![CDATA[
  89. self.canvas = ui.append_canvas(ui.window,100,100,{'background':'#eee'})
  90. self.clock_text = self.canvas.element.create_text(25,25,{'text':'0.0'})
  91. self.actual_clock_text = self.canvas.element.create_text(25,50,{'text':'0.0'})
  92. interrupt_button = ui.append_button(ui.window, 'INTERRUPT');
  93. continue_button = ui.append_button(ui.window, 'CONTINUE');
  94. ui.bind_event(interrupt_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'interrupt_clicked');
  95. ui.bind_event(continue_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'continue_clicked');
  96. ]]>
  97. </body>
  98. </method>
  99. <method name="update_timers">
  100. <body>
  101. self.canvas.element.itemconfigure(self.clock_text, text=str('%.2f' % (self.getSimulatedTime() / 1000.0)))
  102. self.canvas.element.itemconfigure(self.actual_clock_text, text='%.2f' % (time() / 1000.0))
  103. </body>
  104. </method>
  105. <scxml initial="running">
  106. <state id="running">
  107. <transition target="." after="0.05">
  108. <script>
  109. self.update_timers()
  110. </script>
  111. </transition>
  112. <transition target="../interrupted" event="interrupt_clicked" port="ui">
  113. <script>
  114. self.update_timers()
  115. </script>
  116. </transition>
  117. </state>
  118. <state id="interrupted">
  119. <transition target="." event="interrupt_clicked" port="ui">
  120. <script>
  121. self.update_timers()
  122. </script>
  123. </transition>
  124. <transition target="../running" event="continue_clicked" port="ui">
  125. <script>
  126. self.update_timers()
  127. </script>
  128. </transition>
  129. </state>
  130. </scxml>
  131. </class>
  132. </diagram>
  133. To compile, save this in a file called ``timer.xml`` and run ``python -m sccd.compiler.sccdc -p eventloop -l python timer.xml``
  134. Then, the following file will run the model::
  135. import Tkinter as tk
  136. import timer
  137. from sccd.runtime.libs.ui import ui
  138. from sccd.runtime.statecharts_core import Event
  139. from sccd.runtime.tkinter_eventloop import *
  140. if __name__ == '__main__':
  141. ui.window = tk.Tk()
  142. controller = timer.Controller(TkEventLoop(ui.window))
  143. controller.start()
  144. ui.window.mainloop()
  145. Eventloop (Javascript)
  146. ^^^^^^^^^^^^^^^^^^^^^^
  147. The SCCD model::
  148. <?xml version="1.0" ?>
  149. <diagram author="Simon Van Mierlo" name="Timer">
  150. <inport name="ui" />
  151. <class name="MainApp" default="true">
  152. <method name="MainApp">
  153. <body>
  154. <![CDATA[
  155. this.canvas = ui.append_canvas(ui.window,400,150,{'background':'#eee'})
  156. this.clock_text = this.canvas.add_text(25,25,'0.0')
  157. this.actual_clock_text = this.canvas.add_text(25,50,'0.0')
  158. var interrupt_button = ui.append_button(ui.window, 'INTERRUPT');
  159. var continue_button = ui.append_button(ui.window, 'CONTINUE');
  160. ui.bind_event(interrupt_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'interrupt_clicked');
  161. ui.bind_event(continue_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'continue_clicked');
  162. ]]>
  163. </body>
  164. </method>
  165. <method name="update_timers">
  166. <body>
  167. this.clock_text.set_text((this.getSimulatedTime() / 1000).toFixed(2));
  168. this.actual_clock_text.set_text((this.getSimulatedTime() / 1000).toFixed(2));
  169. </body>
  170. </method>
  171. <scxml initial="running">
  172. <state id="running">
  173. <transition target="." after="0.05">
  174. <script>
  175. this.update_timers();
  176. </script>
  177. </transition>
  178. <transition target="../interrupted" event="interrupt_clicked" port="ui">
  179. <script>
  180. this.update_timers();
  181. </script>
  182. </transition>
  183. </state>
  184. <state id="interrupted">
  185. <transition target="." event="interrupt_clicked" port="ui">
  186. <script>
  187. this.update_timers();
  188. </script>
  189. </transition>
  190. <transition target="../running" event="continue_clicked" port="ui">
  191. <script>
  192. this.update_timers();
  193. </script>
  194. </transition>
  195. </state>
  196. </scxml>
  197. </class>
  198. </diagram>
  199. To compile, save this in a file called ``timer.xml`` and run ``python -m sccd.compiler.sccdc -p eventloop -l javascript timer.xml``
  200. Then, the following file will run the model::
  201. <div>
  202. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/HackTimer.js"></script>
  203. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/statecharts_core.js"></script>
  204. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/utils.js"></script>
  205. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/svg.js"></script>
  206. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/ui.js"></script>
  207. <script src="timer.js"></script>
  208. <script>
  209. controller = new Timer.Controller(new JsEventLoop());
  210. controller.start();
  211. </script>
  212. </div>
  213. Traffic Lights
  214. --------------
  215. The traffic lights example demonstrates most functionality of SCCD. There are three lights (green, yellow, and red). The traffic light autonomously switches between them, but also listens for a police interrupt, which will flash the yellow light. When a second interrupt comes in, the light returns to its last configuration (using a history state).
  216. Python
  217. ^^^^^^
  218. The SCCD model::
  219. <?xml version="1.0" ?>
  220. <diagram author="Raphael Mannadiar" name="Traffic_Light_Python_Version">
  221. <top>
  222. from sccd.runtime.libs.ui import ui
  223. </top>
  224. <inport name="ui" />
  225. <class name="MainApp" default="true">
  226. <relationships>
  227. <association name="trafficlight" class="TrafficLight" />
  228. </relationships>
  229. <method name="MainApp">
  230. <body>
  231. <![CDATA[
  232. self.canvas = ui.append_canvas(ui.window,100,310,{'background':'#eee'});
  233. police_button = ui.append_button(ui.window, 'Police interrupt');
  234. quit_button = ui.append_button(ui.window, 'Quit');
  235. ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'police_interrupt_clicked');
  236. ui.bind_event(quit_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'quit_clicked');
  237. ]]>
  238. </body>
  239. </method>
  240. <scxml initial="initializing">
  241. <state id="initializing">
  242. <transition target="../creating">
  243. <raise scope="cd" event="create_instance">
  244. <parameter expr='"trafficlight"' />
  245. <parameter expr='"TrafficLight"' />
  246. <parameter expr="self.canvas" />
  247. </raise>
  248. </transition>
  249. </state>
  250. <state id="creating">
  251. <transition event="instance_created" target="../initialized">
  252. <parameter name="association_name" type="string"/>
  253. <raise scope="cd" event="start_instance">
  254. <parameter expr="association_name" />
  255. </raise>
  256. <raise scope="narrow" event="set_association_name" target="association_name">
  257. <parameter expr="association_name" />
  258. </raise>
  259. </transition>
  260. </state>
  261. <state id="initialized">
  262. </state>
  263. </scxml>
  264. </class>
  265. <class name="TrafficLight">
  266. <relationships>
  267. </relationships>
  268. <method name="TrafficLight">
  269. <parameter name="canvas" />
  270. <body>
  271. <![CDATA[
  272. size = 100;
  273. offset = size+5;
  274. self.RED = 0;
  275. self.YELLOW = 1;
  276. self.GREEN = 2;
  277. self.colors = ['#f00','#ff0','#0f0']
  278. self.lights = [
  279. canvas.add_rectangle(size/2, size/2, size, size, {'fill':'#000'}),
  280. canvas.add_rectangle(size/2, size/2+offset, size, size, {'fill':'#000'}),
  281. canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
  282. ]]>
  283. </body>
  284. </method>
  285. <method name="clear">
  286. <body>
  287. <![CDATA[
  288. self.lights[self.RED].set_color('#000');
  289. self.lights[self.YELLOW].set_color('#000');
  290. self.lights[self.GREEN].set_color('#000');
  291. ]]>
  292. </body>
  293. </method>
  294. <method name="setGreen">
  295. <body>
  296. <![CDATA[
  297. self.clear();
  298. self.lights[self.GREEN].set_color(self.colors[self.GREEN]);
  299. ]]>
  300. </body>
  301. </method>
  302. <method name="setYellow">
  303. <body>
  304. <![CDATA[
  305. self.clear();
  306. self.lights[self.YELLOW].set_color(self.colors[self.YELLOW]);
  307. ]]>
  308. </body>
  309. </method>
  310. <method name="setRed">
  311. <body>
  312. <![CDATA[
  313. self.clear();
  314. self.lights[self.RED].set_color(self.colors[self.RED]);
  315. ]]>
  316. </body>
  317. </method>
  318. <scxml initial="on">
  319. <state id="on" initial="normal">
  320. <state id="normal" initial="red">
  321. <state id="red">
  322. <onentry>
  323. <script>
  324. <![CDATA[
  325. self.setRed();
  326. ]]>
  327. </script>
  328. </onentry>
  329. <transition after='3' target='../green'/>
  330. </state>
  331. <state id="green">
  332. <onentry>
  333. <script>
  334. <![CDATA[
  335. self.setGreen();
  336. ]]>
  337. </script>
  338. </onentry>
  339. <transition after='2' target='../yellow'/>
  340. </state>
  341. <state id="yellow">
  342. <onentry>
  343. <script>
  344. <![CDATA[
  345. self.setYellow();
  346. ]]>
  347. </script>
  348. </onentry>
  349. <transition after='1' target='../red'/>
  350. </state>
  351. <transition event='police_interrupt_clicked' port='ui' target='../interrupted'/>
  352. <history id="history"/>
  353. </state>
  354. <state id="interrupted" initial="yellow">
  355. <state id="yellow">
  356. <onentry>
  357. <script>
  358. <![CDATA[
  359. self.setYellow();
  360. ]]>
  361. </script>
  362. </onentry>
  363. <transition after='.5' target='../black'/>
  364. </state>
  365. <state id="black">
  366. <onentry>
  367. <script>
  368. <![CDATA[
  369. self.clear();
  370. ]]>
  371. </script>
  372. </onentry>
  373. <transition after='.5' target='../yellow'/>
  374. </state>
  375. <transition event='police_interrupt_clicked' port='ui' target='../normal/history'/>
  376. </state>
  377. <transition event='quit_clicked' port='ui' target='../off'/>
  378. </state>
  379. <state id="off">
  380. <onentry>
  381. <script>
  382. <![CDATA[
  383. self.clear();
  384. ]]>
  385. </script>
  386. </onentry>
  387. </state>
  388. </scxml>
  389. </class>
  390. </diagram>
  391. To compile, save this in a file called ``trafficlight.xml`` and run ``python -m sccd.compiler.sccdc -p eventloop -l python trafficlight.xml``
  392. Then, the following file will run the model::
  393. import Tkinter as tk
  394. import trafficlight
  395. from sccd.runtime.libs.ui import ui
  396. from sccd.runtime.statecharts_core import Event
  397. from sccd.runtime.tkinter_eventloop import *
  398. if __name__ == '__main__':
  399. ui.window = tk.Tk()
  400. controller = trafficlight.Controller(TkEventLoop(ui.window))
  401. controller.start()
  402. ui.window.mainloop()
  403. Javascript
  404. ^^^^^^^^^^
  405. The SCCD model::
  406. <?xml version="1.0" ?>
  407. <diagram author="Raphael Mannadiar" name="Traffic_Light_JavaScript_Version">
  408. <inport name="ui" />
  409. <class name="MainApp" default="true">
  410. <relationships>
  411. <association name="trafficlight" class="TrafficLight" />
  412. </relationships>
  413. <method name="MainApp">
  414. <body>
  415. <![CDATA[
  416. this.canvas = ui.append_canvas(ui.window,100,310,{'background':'#eee'});
  417. var police_button = ui.append_button(ui.window, 'Police interrupt');
  418. var quit_button = ui.append_button(ui.window, 'Quit');
  419. ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'police_interrupt_clicked');
  420. ui.bind_event(quit_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'quit_clicked');
  421. ]]>
  422. </body>
  423. </method>
  424. <scxml initial="initializing">
  425. <state id="initializing">
  426. <transition target="../creating">
  427. <raise scope="cd" event="create_instance">
  428. <parameter expr='"trafficlight"' />
  429. <parameter expr='"TrafficLight"' />
  430. <parameter expr="this.canvas" />
  431. </raise>
  432. </transition>
  433. </state>
  434. <state id="creating">
  435. <transition event="instance_created" target="../initialized">
  436. <parameter name="association_name" type="string"/>
  437. <raise scope="cd" event="start_instance">
  438. <parameter expr="association_name" />
  439. </raise>
  440. <raise scope="narrow" event="set_association_name" target="association_name">
  441. <parameter expr="association_name" />
  442. </raise>
  443. </transition>
  444. </state>
  445. <state id="initialized">
  446. </state>
  447. </scxml>
  448. </class>
  449. <class name="TrafficLight">
  450. <relationships>
  451. </relationships>
  452. <method name="TrafficLight">
  453. <parameter name="canvas" />
  454. <body>
  455. <![CDATA[
  456. var size = 100;
  457. var offset = size+5;
  458. this.RED = 0;
  459. this.YELLOW = 1;
  460. this.GREEN = 2;
  461. this.colors = ['#f00','#ff0','#0f0']
  462. this.lights = [canvas.add_rectangle(size/2, size/2, size, size, {'fill':'#000'}),
  463. canvas.add_rectangle(size/2, size/2+offset, size, size, {'fill':'#000'}),
  464. canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
  465. ]]>
  466. </body>
  467. </method>
  468. <method name="clear">
  469. <body>
  470. <![CDATA[
  471. this.lights[this.RED].set_color('#000');
  472. this.lights[this.YELLOW].set_color('#000');
  473. this.lights[this.GREEN].set_color('#000');
  474. ]]>
  475. </body>
  476. </method>
  477. <method name="setGreen">
  478. <body>
  479. <![CDATA[
  480. this.clear();
  481. this.lights[this.GREEN].set_color(this.colors[this.GREEN]);
  482. ]]>
  483. </body>
  484. </method>
  485. <method name="setYellow">
  486. <body>
  487. <![CDATA[
  488. this.clear();
  489. this.lights[this.YELLOW].set_color(this.colors[this.YELLOW]);
  490. ]]>
  491. </body>
  492. </method>
  493. <method name="setRed">
  494. <body>
  495. <![CDATA[
  496. this.clear();
  497. this.lights[this.RED].set_color(this.colors[this.RED]);
  498. ]]>
  499. </body>
  500. </method>
  501. <scxml initial="on">
  502. <state id="on" initial="normal">
  503. <state id="normal" initial="red">
  504. <state id="red">
  505. <onentry>
  506. <script>
  507. <![CDATA[
  508. this.setRed();
  509. ]]>
  510. </script>
  511. </onentry>
  512. <transition after='3' target='../green'/>
  513. </state>
  514. <state id="green">
  515. <onentry>
  516. <script>
  517. <![CDATA[
  518. this.setGreen();
  519. ]]>
  520. </script>
  521. </onentry>
  522. <transition after='2' target='../yellow'/>
  523. </state>
  524. <state id="yellow">
  525. <onentry>
  526. <script>
  527. <![CDATA[
  528. this.setYellow();
  529. ]]>
  530. </script>
  531. </onentry>
  532. <transition after='1' target='../red'/>
  533. </state>
  534. <transition event='police_interrupt_clicked' port='ui' target='../interrupted'/>
  535. <history id="history"/>
  536. </state>
  537. <state id="interrupted" initial="yellow">
  538. <state id="yellow">
  539. <onentry>
  540. <script>
  541. <![CDATA[
  542. this.setYellow();
  543. ]]>
  544. </script>
  545. </onentry>
  546. <transition after='.5' target='../black'/>
  547. </state>
  548. <state id="black">
  549. <onentry>
  550. <script>
  551. <![CDATA[
  552. this.clear();
  553. ]]>
  554. </script>
  555. </onentry>
  556. <transition after='.5' target='../yellow'/>
  557. </state>
  558. <transition event='police_interrupt_clicked' port='ui' target='../normal/history'/>
  559. </state>
  560. <transition event='quit_clicked' port='ui' target='../off'/>
  561. </state>
  562. <state id="off">
  563. <onentry>
  564. <script>
  565. <![CDATA[
  566. this.clear();
  567. ]]>
  568. </script>
  569. </onentry>
  570. </state>
  571. </scxml>
  572. </class>
  573. </diagram>
  574. To compile, save this in a file called ``trafficlight.xml`` and run ``python -m sccd.compiler.sccdc -p eventloop -l javascript trafficlight.xml``
  575. Then, the following file will run the model::
  576. <div>
  577. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/HackTimer.js"></script>
  578. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/statecharts_core.js"></script>
  579. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/utils.js"></script>
  580. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/svg.js"></script>
  581. <script src="https://msdl.uantwerpen.be/git/simon/SCCD/raw/v0.9/src/javascript_sccd_runtime/libs/ui.js"></script>
  582. <script src="trafficlight.js"></script>
  583. <script>
  584. controller = new Traffic_Light_JavaScript_Version.Controller(new JsEventLoop());
  585. controller.start();
  586. </script>
  587. </div>