reference.html 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. <?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title>reference</title>
  6. <link type="text/css" rel="stylesheet" href="../style.css"/>
  7. </head>
  8. <body>
  9. <p><link href="../../css/bootstrap.css" rel="stylesheet" /></p>
  10. <h1 id="SCTReference">SCT Reference</h1>
  11. <h2 id="Statechartelements">Statechart elements</h2>
  12. <p>In the following the state chart elements of the YAKINDU SCT 2 editor are described. The meta model of the YAKINDU SCT 2 is the model of finite state machines. It is based on the view of a system that is defined by a finite number of states. The behavior of that system is based on the active states. These states are determined by the history of the state machine. Very important are the theoretical models for state machines by Mealy and Moore. Mealy state machines associate actions with transitions. Moore machines associate actions with states (entry, exit). In the YAKINDU SCT 2 both is possible.</p>
  13. <p>The YAKINDU SCT 2 meta model is designed similar to the UML state chart meta model with the following differences</p>
  14. <ul>
  15. <li>they are self contained with interfaces defined by events and variables</li>
  16. <li>core execution semantics are cycle driven, not event driven
  17. <ul>
  18. <li>this allows to process concurrent events</li>
  19. <li>event driven behavior can be defined on top</li>
  20. </ul>
  21. </li>
  22. <li>time is an abstract concept for state charts</li>
  23. <li>time control is delegated to the environment</li>
  24. </ul>
  25. <p>The model interpreter and different flavors of generated code follow these same core semantics.</p>
  26. <p>Please refer to the description of the
  27. <a href="http://en.wikipedia.org/wiki/UML_state_machine">UML Statecharts</a> for more details.
  28. </p>
  29. <h3 id="Regions">Regions</h3>
  30. <p>As already mentioned the YAKINDU state charts are self contained. They are organized in regions. Due to this it is possible to organize multiple state machines in different regions and to run them concurrently. </p>
  31. <p>
  32. <img border="0" src="images/parallelRegions.jpg"/>
  33. </p>
  34. <h3 id="States">States</h3>
  35. <p>States are the central elements of a state machine. A state has to be placed inside a region and needs a unique name inside this region. During simulation each state can be active or passive. An active state has actions that are accomplished. Either an action is carried out on entering a state, during active state or on exit. </p>
  36. <h3 id="Transitions">Transitions</h3>
  37. <p>A transition is the transfer of one state to another. Transitions are diagrammed as arrows and can carry events and actions but must not. </p>
  38. <p>The syntax of events and actions is defined by a textual description language (#Statechartdescriptionlanguage). Please refer to the documentation section
  39. <a href="#Events">Events</a> for more details. For more details on Actions refer to the chapter
  40. <a href="#ReactionTriggers">Actions</a>.
  41. </p>
  42. <p>If a state has more than one outgoing transition without event that transition is carried out first that was modeled first.</p>
  43. <h3 id="Initialstateandfinalstate">Initial state and final state</h3>
  44. <p>Initial and final states are pseudo states, because the state chart does not rest on them. Pseudo states express characteristics that are impossible to express by simple states. </p>
  45. <p>The initial state is always the first state that is active during interpretation or simulation of the state machine. An initial state can only have one outgoing transition and no incoming. This transition has no events or actions.</p>
  46. <p>Inside a region only one initial state is allowed, but every region can have an initial state.</p>
  47. <h3 id="Choice">Choice</h3>
  48. <p>Choice is also a pseudo state. It can be used to model a conditional path. Choice nodes divide a transition into multiple parts. </p>
  49. <p>Usually the first transition points towards the choice node. One of the choice outgoing transitions can carry a condition. </p>
  50. <h3 id="Junction">Junction</h3>
  51. <p>A junction is a pseudo state do combine transitions. This is very comfortable if a state machine has many similar transitions. Junctions add clear arrangement to the state machine.</p>
  52. <h3 id="CompositeState">Composite State</h3>
  53. <p>A composite state is a state that is composed of other state machines. These are also organized in regions. Besides the simple composite state YAKINDU knows two kinds of composite states: orthogonal state and submachine states.</p>
  54. <p>Composite states contain other state machine branches.</p>
  55. <h4 id="Orthogonalstates">Orthogonal states</h4>
  56. <p>In the context of state machines orthogonal states are states that are independent from each other. The most famous example is the keyboard example:</p>
  57. <p>
  58. <img border="0" src="images/orthogonalState_example.jpg"/>
  59. </p>
  60. <h4 id="Submachinestates">Submachine states</h4>
  61. <p>Submachine states may contain complete state machines. Here the user can chose another state machine to be included or create a new sub statemachine. During simulation only the top level statemachine is interpreted. It is not possible to jump to the sub level state machines and back.</p>
  62. <p>
  63. <img border="0" src="images/substatemachine_example.jpg"/>
  64. </p>
  65. <h3 id="ShallowHistory">Shallow History</h3>
  66. <p>The shallow history state is a pseudo state that is placed inside regions of composite states. It is used to &#8218;remember&#8217; the last active state inside a composite state. So it is possible to jump to this state instead of starting at the inner entry state again. The following example of a questionaire answering will explain this:</p>
  67. <p>
  68. <img border="0" src="images/shallowHistory01.jpg"/>
  69. </p>
  70. <p>The interesting parts in this state chart are the events
  71. <em>checkProgress</em> and
  72. <em>goon</em>. CheckProgress jumps back to the init state while assigning the current progress count to the variable
  73. <em>temp</em>.
  74. <em>goon</em> jumps to the shallow history state that was placed inside the composite state.
  75. </p>
  76. <p>
  77. <img border="0" src="images/shallowHistory02.jpg"/>
  78. </p>
  79. <p>
  80. <img border="0" src="images/shallowHistory03.jpg"/>
  81. </p>
  82. <p>On triggering the
  83. <em>goon</em> event the last active state is activated again.
  84. </p>
  85. <h3 id="DeepHistory">Deep History</h3>
  86. <p>Deep history is similar to shallow history but more complex. With a deep history the latest state of multiple nested states is remembered.</p>
  87. <h2 id="Statechartdescriptionlanguage">Statechart description language</h2>
  88. <p>The textual description language is used to declare and describe behaviors in the state machine. It is case sensitive.</p>
  89. <h3 id="Typesystem">Typesystem</h3>
  90. <p>The language has an integrated small typesystem with the following simple types:</p>
  91. <ul>
  92. <li>integer</li>
  93. <li>real</li>
  94. <li>boolean</li>
  95. <li>string</li>
  96. <li>void</li>
  97. </ul>
  98. <p>So events and variables can be declared with types:</p>
  99. <pre><code>var intVar : integer
  100. var realVar : real
  101. var boolVar : boolean
  102. var stringVar : string
  103. var voidVar : void
  104. event addInt : integer
  105. event checkValidity : boolean
  106. </code></pre>
  107. <h3 id="Expressions">Expressions</h3>
  108. <p>Expressions can be defined similar to other programming languages. The language offers operators to define logical expressions, bitwise arithmetic, and arithmetic expressions and bit shifting.</p>
  109. <p>Logical expressions are similar to other programming languages. The return type is
  110. <strong>boolean</strong>. In the following there are some examples of these:
  111. </p>
  112. <h4 id="LogicalAND">Logical AND</h4>
  113. <pre><code>var1 &amp;&amp; var2
  114. </code></pre>
  115. <h4 id="LogicalOR">Logical OR</h4>
  116. <pre><code>var1 || var2
  117. </code></pre>
  118. <h4 id="LogicalNOT">Logical NOT</h4>
  119. <pre><code>!var1
  120. </code></pre>
  121. <h4 id="Conditionalexpression">Conditional expression</h4>
  122. <pre><code>var1 ? var2 : 23
  123. </code></pre>
  124. <h4 id="BitwiseXOR">Bitwise XOR</h4>
  125. <pre><code>var1 ^ var2
  126. </code></pre>
  127. <h4 id="BitwiseOR">Bitwise OR</h4>
  128. <pre><code>var1 | var2
  129. </code></pre>
  130. <h4 id="BitwiseAND">Bitwise AND</h4>
  131. <pre><code>var1 &amp; var2
  132. </code></pre>
  133. <h4 id="LogicalRelationsandShiftOperators">Logical Relations and Shift Operators</h4>
  134. <table>
  135. <tr>
  136. <td>less than </td>
  137. <td>&lt; </td>
  138. </tr>
  139. <tr>
  140. <td>equal or less than </td>
  141. <td>&lt;= </td>
  142. </tr>
  143. <tr>
  144. <td>greater than </td>
  145. <td>&gt; </td>
  146. </tr>
  147. <tr>
  148. <td>equal or greater than </td>
  149. <td>&gt;= </td>
  150. </tr>
  151. <tr>
  152. <td>equal </td>
  153. <td>== </td>
  154. </tr>
  155. <tr>
  156. <td>not equal </td>
  157. <td>!= </td>
  158. </tr>
  159. <tr>
  160. <td>shift left </td>
  161. <td>&lt;&lt; </td>
  162. </tr>
  163. <tr>
  164. <td>shift right </td>
  165. <td>&gt;&gt; </td>
  166. </tr>
  167. </table>
  168. <h4 id="Binaryarithmeticoperators">Binary arithmetic operators</h4>
  169. <table>
  170. <tr>
  171. <td>plus </td>
  172. <td>+ </td>
  173. </tr>
  174. <tr>
  175. <td>minus </td>
  176. <td>- </td>
  177. </tr>
  178. <tr>
  179. <td>multiply </td>
  180. <td>* </td>
  181. </tr>
  182. <tr>
  183. <td>divide </td>
  184. <td>/ </td>
  185. </tr>
  186. <tr>
  187. <td>modulo </td>
  188. <td>% </td>
  189. </tr>
  190. </table>
  191. <h4 id="Unaryarithmeticoperators">Unary arithmetic operators</h4>
  192. <table>
  193. <tr>
  194. <td>positive </td>
  195. <td>+ </td>
  196. </tr>
  197. <tr>
  198. <td>negative </td>
  199. <td>- </td>
  200. </tr>
  201. <tr>
  202. <td>complement </td>
  203. <td>~ </td>
  204. </tr>
  205. </table>
  206. <h3 id="Statements">Statements</h3>
  207. <p>A statements can be either an assignment, raising an event or call an operation. The language has the following assignment operators:</p>
  208. <ul>
  209. <li>simple assignment: = </li>
  210. <li>multiply and assign: *= </li>
  211. <li>divide and assign: /= </li>
  212. <li>calculate modulo and assign: %= </li>
  213. <li>add and assign: += </li>
  214. <li>subtract and assign: -= </li>
  215. <li>bitshift left and assign: &lt;&lt;= </li>
  216. <li>bitshift right and assign: &gt;&gt;= </li>
  217. <li>bitwise AND and assign: &amp;= </li>
  218. <li>bitwise XOR and assign: ^= </li>
  219. <li>bitwise OR and assign:
  220. <code>|=</code>
  221. </li>
  222. </ul>
  223. <p>An event is raised by the keyword
  224. <em>raise</em> followed by the event name and if it is an interface event the name of the interface.
  225. </p>
  226. <p>An operation is called similar to other programming languages with the operation name and passing concrete parameters. The parameters can be expressions.</p>
  227. <h3 id="Scopes">Scopes</h3>
  228. <p><!-- Start stext_keyword_namespace --></p>
  229. <h4 id="Namespace">Namespace</h4>
  230. <p>The language allows to define unique namespaces, which can be used to qualify references to the statechart.</p>
  231. <pre><code>namespace trafficlights
  232. </code></pre>
  233. <p><!-- End stext_keyword_namespace -->
  234. <br/><!-- Start stext_keyword_interface -->
  235. </p>
  236. <h4 id="interfacescope">interface scope</h4>
  237. <p>Declarations in the interface scope are externally visible. They can be shared within the environment.</p>
  238. <pre><code>interface NamedInterface:
  239. in event event1
  240. out event event3
  241. var variable1 : real
  242. entrypoint entry1
  243. exitpoint exit1
  244. </code></pre>
  245. <p><!-- End stext_keyword_interface -->
  246. <br/><!-- Start stext_keyword_internal -->
  247. </p>
  248. <h4 id="internalscope">internal scope</h4>
  249. <p>Declarations made in an internal scope are only visible for contained states.</p>
  250. <pre><code>internal:
  251. var localVariable1: integer
  252. event localEvent: integer
  253. local event localEvent2: NamedInterface.event1 || localEvent
  254. local event localEvent3: localEvent || localEvent2 : 25
  255. operation localOperation (integer, integer): integer
  256. localEvent3 / raise NamedInterface.event3 :
  257. localOperation(valueOf(localEvent),NamedInterface.variable1);
  258. </code></pre>
  259. <p><!-- End stext_keyword_internal --></p>
  260. <h3 id="Declarations">Declarations</h3>
  261. <p>Within scopes there can be declarations of Events, Variables, Operations, LocalReactions, EntryPoints and ExitPoints.</p>
  262. <p><!-- Start stext_keyword_event --></p>
  263. <h3 id="Events">Events</h3>
  264. <p>Within interface scope events have an direction. They can either be ingoing or outgoing:</p>
  265. <pre><code>interface NamedInterface:
  266. in event event1
  267. out event event2
  268. </code></pre>
  269. <p>Within local scope events can carry variables:</p>
  270. <pre><code>internal:
  271. event localEvent1 : integer
  272. </code></pre>
  273. <p>Local events can be derived from interface events or other local events and can have a value assignment:</p>
  274. <pre><code>internal:
  275. event localEvent1: integer
  276. local event localEvent2 = NamedInterface.event1 || localEvent1
  277. local event localEvent3 = localEvent2 || 25
  278. </code></pre>
  279. <p><!-- End stext_keyword_event --></p>
  280. <p><!-- Start stext_keyword_var --></p>
  281. <h3 id="Variables">Variables</h3>
  282. <p>Variables can have different visibilities. They can be visible for the environment:</p>
  283. <pre><code>var variable1: real
  284. </code></pre>
  285. <p>Variables can be
  286. <strong>readonly</strong> (constants):
  287. </p>
  288. <pre><code>var readonly pi: real = 3.1415
  289. </code></pre>
  290. <p>Variables can be referenced by the environment.</p>
  291. <pre><code>var external variable3: integer = 34
  292. </code></pre>
  293. <p><!-- End stext_keyword_var --></p>
  294. <h3 id="ReactionTriggers">Reaction Triggers</h3>
  295. <p>Actions are key constructs in state machines to model behavior. The YAKINDU SCT 2 knows the following kinds of actions.</p>
  296. <p><!-- Start stext_keyword_after --></p>
  297. <h4 id="after">after</h4>
  298. <p>The
  299. <em>after</em> trigger specifies one-shot time events.
  300. </p>
  301. <p>After the specified time the reaction is triggered. An
  302. <em>after</em> trigger can be used in transitions of states as well in local reactions of states and statecharts. The specified time starts when the state or statechart is entered.
  303. </p>
  304. <pre><code>after 20 s
  305. </code></pre>
  306. <p>Structure: </p>
  307. <p>
  308. <code>after</code>
  309. <em>
  310. <code>time</code>
  311. </em> (
  312. <em>
  313. <code>unit</code>
  314. </em>)?
  315. </p>
  316. <p>The time unit can be:</p>
  317. <ul>
  318. <li><!-- Start stext_keyword_s --> s &#8211; seconds <!-- End stext_keyword_s --></li>
  319. <li><!-- Start stext_keyword_ms --> ms &#8211; milliseconds <!-- End stext_keyword_ms --></li>
  320. <li><!-- Start stext_keyword_us --> us &#8211; microseconds <!-- End stext_keyword_us --></li>
  321. <li><!-- Start stext_keyword_ns --> ns &#8211; nanoseconds <!-- End stext_keyword_ns --></li>
  322. <li>empty &#8211; implies seconds</li>
  323. </ul>
  324. <p><!-- End stext_keyword_after -->
  325. <br/><!-- Start stext_keyword_every -->
  326. </p>
  327. <h4 id="every">every</h4>
  328. <p>The
  329. <em>every</em> trigger specifies periodic time events.
  330. </p>
  331. <p>The reaction is triggered periodically after the specified time. An
  332. <em>every</em> trigger can be used in transitions of states as well in local reactions of states and statecharts. The specified time starts when the state or statechart is entered and repeats periodically.
  333. </p>
  334. <pre><code>every 200 ms
  335. </code></pre>
  336. <p>Structure: </p>
  337. <p>
  338. <code>every</code>
  339. <em>
  340. <code>time</code>
  341. </em> (
  342. <em>
  343. <code>unit</code>
  344. </em>)?
  345. </p>
  346. <p>The time unit can be:</p>
  347. <ul>
  348. <li>s &#8211; seconds</li>
  349. <li>ms &#8211; milliseconds</li>
  350. <li>us &#8211; microseconds</li>
  351. <li>ns &#8211; nanoseconds</li>
  352. <li>empty &#8211; implies seconds</li>
  353. </ul>
  354. <p><!-- End stext_keyword_every -->
  355. <br/><!-- Start stext_keyword_always -->
  356. </p>
  357. <h4 id="always">always</h4>
  358. <p>This trigger is always true and enables a reaction to be executed in every run to completion step (RTS). It is equivalent to
  359. <em>oncycle</em>.
  360. </p>
  361. <p><!-- End stext_keyword_always --></p>
  362. <p><!-- Start stext_keyword_default -->
  363. <br/><!-- Start stext_keyword_else -->
  364. </p>
  365. <h4 id="defaultelse">default, else</h4>
  366. <p>The
  367. <em>default</em> trigger is equivalent to the
  368. <em>else</em> trigger. It is intended for use for the outgoing transitions of
  369. <em>choice</em> pseudo states, to make sure that always an outgoing transition can be taken. It can only be be used in transitions and implies the lowest evaluation priority for that transition.
  370. </p>
  371. <p><!-- End stext_keyword_else -->
  372. <br/><!-- End stext_keyword_default -->
  373. </p>
  374. <p><!-- Start stext_keyword_entry --></p>
  375. <h4 id="entry">entry</h4>
  376. <p>An
  377. <em>entry</em> trigger marks actions that are carried out on entering a state or state machine.
  378. </p>
  379. <p><!-- End stext_keyword_entry -->
  380. <br/><!-- Start stext_keyword_exit -->
  381. </p>
  382. <h4 id="exit">exit</h4>
  383. <p>An
  384. <em>exit</em> trigger marks actions that are carried out on exiting a state or state machine.
  385. </p>
  386. <p><!-- End stext_keyword_exit -->
  387. <br/><!-- Start stext_keyword_oncycle -->
  388. </p>
  389. <h4 id="oncycle">oncycle</h4>
  390. <p>The
  391. <em>oncycle</em> trigger is always true and enables a reaction to be executed in every run to completion step (RTS). It is equivalent to
  392. <em>always</em>.
  393. </p>
  394. <p><!-- End stext_keyword_oncycle -->
  395. <br/><!-- Start stext_keyword_operation -->
  396. </p>
  397. <h3 id="Operations">Operations</h3>
  398. <p>Operations can have none, one or multiple parameters. The parameters are only declarated by their type. An operation can have one return type similar to Java.</p>
  399. <pre><code>operation localOperation (integer, integer):integer
  400. localEvent3/ raise NamedInterface3.event1
  401. </code></pre>
  402. <p>
  403. <br/><!-- End stext_keyword_operation -->
  404. </p>
  405. <h3 id="BuildInFunctions">Build-In Functions</h3>
  406. <p><!-- Start stext_keyword_valueof --></p>
  407. <h4 id="valueofevent">valueof(event)</h4>
  408. <p>Returns the value of an valued event that it passed to the function as parameter.</p>
  409. <pre><code>myVar = valueof(myEvent)
  410. </code></pre>
  411. <p></p>
  412. <p><!-- End stext_keyword_valueof --></p>
  413. <p><!-- Start stext_keyword_active --></p>
  414. <h4 id="activestate">active(state)</h4>
  415. <p>Returns &#8222;true&#8221; if a state is active or &#8222;false&#8221; otherwise.</p>
  416. <pre><code>myBool = active(StateA)
  417. </code></pre>
  418. <p></p>
  419. <p><!-- End stext_keyword_active --></p>
  420. <h3 id="LocalReactions">LocalReactions</h3>
  421. <p>Local reactions describe the internal behavior of a state. So they have internal scope. A local reaction is declared as follows:</p>
  422. <pre><code>LocalReaction: ReactionTrigger '/' ReactionEffect ('#' ReactionProperties)?
  423. ReactionTrigger: (Event ("," Event )* (=&gt; '[' Expression ']')?) | '[' Expression ']'
  424. ReactionEffect: Statement (';' Statement )* (';')?
  425. Statement: Assignment | EventRaising | OperationCall
  426. ReactionProperties: (EntryPoint | ExitPoint)*
  427. </code></pre>
  428. <p>Within a local reaction an interface event can be raised:</p>
  429. <pre><code>internal:
  430. localEvent1 / raise NamedInterface.event3 : localOperation (valueOf(localEvent), NamedInterface.variable1);
  431. </code></pre>
  432. <p>Local reactions can have priority values. These are defined by a following # and the integer number of priority:</p>
  433. <pre><code>localEvent2 / NamedInterface.variable2 += 3; #1
  434. localEvent3 / NamedInterface.variable4 += 2.0; #2
  435. </code></pre>
  436. <p><!-- Start stext_keyword_entrypoint --></p>
  437. <h3 id="EntryPoints">EntryPoints</h3>
  438. <p>Every state chart has an entry point. An entry point can be declared like the following:</p>
  439. <pre><code>entrypoint entry1
  440. </code></pre>
  441. <p><!-- End stext_keyword_entrypoint -->
  442. <br/><!-- Start stext_keyword_exitpoint -->
  443. </p>
  444. <h3 id="ExitPoints">ExitPoints</h3>
  445. <p>Every state chart has an exit point. This exit point can be declared like the following.</p>
  446. <pre><code>exitpoint exit1
  447. </code></pre>
  448. <p><!-- End stext_keyword_exitpoint --></p>
  449. <h2 id="SGenGeneratorFeatures">SGen Generator Features</h2>
  450. <p>All generators can be customized with a generator model. This is a textual model file where generator features, like i.e. the outlet path, can be specified. The following screenshot shows an example configuration for the java code generator. </p>
  451. <p>To get started with the generator model, we included a new Eclipse wizard that creates a basic configuration file with default values.</p>
  452. <p>
  453. <img border="0" src="images/sGenEditor.png"/>
  454. </p>
  455. <p>The generator model is associated with the builder. If
  456. <strong>Project</strong> &gt;
  457. <strong>Build Automatically</strong> is checked the files are generated. In the following the specific customizing features of the generator models are explained.
  458. </p>
  459. <p>The following section describes the
  460. <strong>Core Features</strong> which are available for all code generators:
  461. </p>
  462. <p><!-- Start sgen_feature_outlet --></p>
  463. <h4 id="Outlet">Outlet</h4>
  464. <p>The
  465. <strong>Outlet</strong> feature specifies the target project and folder for the generated artifacts. It is a
  466. <strong>required</strong> feature and consists of the following parameters:
  467. </p>
  468. <ol>
  469. <li>
  470. <i>targetProject</i> (String, required): The project to store the generated artifacts
  471. </li>
  472. <li>
  473. <i>targetFolder</i> (String, required): The folder to store the generated artifacts
  474. </li>
  475. </ol>
  476. <p>Example configuration:</p>
  477. <pre><code>feature Outlet {
  478. targetProject = "ExampleProject"
  479. targetFolder = "src-gen"
  480. }
  481. </code></pre>
  482. <p><!-- End sgen_feature_outlet -->
  483. <br/><!-- Start sgen_feature_licenseheader -->
  484. </p>
  485. <h4 id="LicenseHeader">LicenseHeader</h4>
  486. <p>The
  487. <strong>LicenseHeader</strong> feature specifies the license text that should be added as a header to the generated artifacts. It is an
  488. <strong>optional</strong> feature and consists of the following parameters:
  489. </p>
  490. <ol>
  491. <li>
  492. <i>licenseText</i> (String, required): The license text to add as a header
  493. </li>
  494. </ol>
  495. <p>Example configuration:</p>
  496. <pre><code>feature LicenseHeader {
  497. licenseText = "Copyright (c) 2012 committers of YAKINDU and others."
  498. }
  499. </code></pre>
  500. <p><!-- End sgen_feature_licenseheader -->
  501. <br/><!-- Start sgen_feature_functioninlining -->
  502. </p>
  503. <h4 id="FunctionInlining">FunctionInlining</h4>
  504. <p>The
  505. <strong>FunctionInlining</strong> feature allows the inlining of expressions instead of generating separate functions or methods. This might reduce the readability of the generated code, but increases performance because less operation calls are necessary.
  506. <br/>It is an
  507. <strong>optinal</strong> feature and consists of the following parameters:
  508. </p>
  509. <ol>
  510. <li>
  511. <i>inlineReactions</i> (Boolean, optional): Inlines the expression for reactions
  512. </li>
  513. <li>
  514. <i>inlineEntryActions</i> (Boolean, optional): Inlines the expression for entry actions
  515. </li>
  516. <li>
  517. <i>inlineExitActions</i> (Boolean, optional): Inlines the expression for exit actions
  518. </li>
  519. <li>
  520. <i>inlineEnterSequences</i> (Boolean, optional): Inlines the expression for enter sequences
  521. </li>
  522. <li>
  523. <i>inlineExitSequences</i> (Boolean, optional): Inlines the expression for exit sequences
  524. </li>
  525. <li>
  526. <i>inlineChoices</i> (Boolean, optional): Inlines the expression for choices
  527. </li>
  528. <li>
  529. <i>inlineEnterRegion</i> (Boolean, optional): Inlines the expression for enter regions
  530. </li>
  531. <li>
  532. <i>inlineExitRegion</i> (Boolean, optional): Inlines the expression for exit regions
  533. </li>
  534. <li>
  535. <i>inlineEntries</i> (Boolean, optional): Inlines the expression for entries
  536. </li>
  537. </ol>
  538. <p>Example configuration:</p>
  539. <pre><code>feature FunctionInlining {
  540. inlineChoices = false
  541. inlineEnterRegion = true
  542. inlineEntries = true
  543. }
  544. </code></pre>
  545. <p><!-- End sgen_feature_functioninlining -->
  546. <br/><!-- Start sgen_feature_debug -->
  547. </p>
  548. <h4 id="Debug">Debug</h4>
  549. <p>The
  550. <strong>Debug</strong> feature dumps the Execution Model to the target folder as xmi model. It is an
  551. <strong>optional</strong> feature and consists of the following parameters:
  552. </p>
  553. <ol>
  554. <li>
  555. <i>dumpSexec</i> (Boolean, required): The license text to add as a header
  556. </li>
  557. </ol>
  558. <p>Example configuration:</p>
  559. <pre><code>feature Debug {
  560. dumpSexec = true
  561. }
  562. </code></pre>
  563. <p><!-- End sgen_feature_debug --></p>
  564. <h2 id="JavaGeneratorFeatures">Java Generator Features</h2>
  565. <p><!-- Start sgen_feature_naming --></p>
  566. <h4 id="Naming">Naming</h4>
  567. <p>The
  568. <strong>Naming</strong> feature allows the configuration of package names as well as class name prefix / suffix.
  569. <br/>It is an
  570. <strong>optional</strong> feature and consists of the following parameters:
  571. </p>
  572. <ol>
  573. <li>
  574. <i>basePackage</i> (Boolean, required): The package to create for the generated java classes
  575. </li>
  576. <li>
  577. <i>implementationSuffix</i> (Boolean, optional): The suffix for the implementing classes
  578. </li>
  579. </ol>
  580. <p>Example configuration:</p>
  581. <pre><code>feature Naming {
  582. basePackage = "org.yakindu.sct"
  583. implementationSuffix = "Impl"
  584. }
  585. </code></pre>
  586. <p><!-- End sgen_feature_naming -->
  587. <br/><!-- Start sgen_feature_generalfeatures -->
  588. </p>
  589. <h4 id="GeneralFeatures">GeneralFeatures</h4>
  590. <p>The
  591. <strong>GeneralFeatures</strong> feature allows to configure additional services to generate with the statemachine. Per default, all parameters are configured to
  592. <i>false</i> It is an
  593. <strong>optional</strong> feature and consists of the following parameters:
  594. </p>
  595. <p><!-- # __EventBasedStatemachine__ (Boolean, optional): Enables/disables the generation of a cycle based statemachine implementation --></p>
  596. <ol>
  597. <li>
  598. <i>InterfaceObserverSupport</i> (Boolean, optional): Enables/disables the generation of listener interfaces for the statemachine
  599. </li>
  600. <li>
  601. <i>RuntimeService</i> (Boolean, optional): Enables/disables the generation of a runtime service that triggers the runcycle of a cycle based statemachine
  602. </li>
  603. <li>
  604. <i>TimerService</i> (Boolean, optional): Enables/disables the generation of a timer service implementation using
  605. <i>java.util.Timer</i>
  606. </li>
  607. </ol>
  608. <p><!-- # __GenericInterfaceSupport__ (Boolean, optional): Enables/disables the generation of generic interfaces -->
  609. <br/><!-- # __StatemachineFactorySupport__ (Boolean, optional): Enables/disables the generation of a factory class -->
  610. </p>
  611. <p>Example configuration:</p>
  612. <pre><code>feature GeneralFeatures {
  613. InterfaceObserverSupport = true
  614. RuntimeService = true
  615. TimerService = true
  616. }
  617. </code></pre>
  618. <p><!-- End sgen_feature_generalfeatures --> </p>
  619. <h2 id="CGeneratorFeatures">C Generator Features</h2>
  620. <p><!-- Start sgen_feature_ccodefeature --> </p>
  621. <h4 id="CCodeFeature">CCodeFeature</h4>
  622. <p>The
  623. <strong>CCodeFeature</strong> feature allows to configure c code specific generator properties. It is an
  624. <strong>optional</strong> feature and consists of the following parameters:
  625. </p>
  626. <ol>
  627. <li>
  628. <i>InterfaceEventListener</i> (Boolean, mandatory): generates listener interface for notification of outgoing events.
  629. </li>
  630. <li>
  631. <i>Singleton</i> (Boolean, mandatory): generates a statically allocated singleton variant of the state machine that does not allow multiple instances.
  632. </li>
  633. <li>
  634. <i>DebugType</i> (String, optional): don&#8217;t use this property &#8211; it will be removed
  635. </li>
  636. </ol>
  637. <p>Example configuration:</p>
  638. <pre><code>feature CCodeFeature {
  639. InterfaceEventListerner = true
  640. Singleton = true
  641. }
  642. </code></pre>
  643. <p><!-- End sgen_feature_ccodefeature --> </p>
  644. <h2 id="CreateCustomCodeGenerators">Create Custom Code Generators</h2>
  645. <p>YAKINDU Statechart Tools provides a rich feature set to supports custom code generators out of the box. These code generators can be either written in Java,
  646. <a href="http://www.eclipse.org/xtend/">Xtend</a> or in
  647. <a href="http://www.eclipse.org/modeling/m2t/?project=xpand">Xpand</a>
  648. </p>
  649. <h3 id="WritingacustomcodegeneratorwithXtend2Java">Writing a custom code generator with Xtend2/Java</h3>
  650. <p>First, you have to create a new Xtend2 generator project. Click
  651. <strong>File</strong> &gt;
  652. <strong>New</strong> &gt;
  653. <strong>Other...</strong> &gt;
  654. <strong>YAKINDU</strong> &gt;
  655. <strong>YAKINDU Xtend2/Java Generator Project</strong> to create a new Xtend2 Generator Project.
  656. </p>
  657. <p>
  658. <img border="0" src="images/xtendGenerator.png"/>
  659. </p>
  660. <p>The wizards asks for a
  661. <strong>Project name</strong> and the name of the
  662. <strong>Generator class</strong>, where you have to specify a full qualified class name. If you check the
  663. <strong>Use Xtend</strong> checkbox, the Generator class will be initially created as an
  664. <a href="http://www.eclipse.org/xtend/">Xtend</a> class. Otherwise, Java will be used for the generator.
  665. </p>
  666. <p>The check box
  667. <strong>Configure for Plugin Export</strong> adds all required extension point registrations to the new project for exporting as a plugin The Generator Model can refer to the new Generator Plugin via its unique
  668. <strong>Generator ID</strong>. If you want to contribute custom generator features for your code generator, check the
  669. <strong>Create Feature Library</strong> check box.
  670. </p>
  671. <p>After click on
  672. <strong>Finish</strong> a new project is created in your workspace. All required plugin dependencies and extension points are registered and you can start to write your code generator based on the ExecutionFlow meta model [Link].
  673. </p>
  674. <h3 id="ExecutingacustomXtend2Javacodegenerator">Executing a custom Xtend2/Java code generator</h3>
  675. <p>YAKINDU Statechart Tools provide a convenient way to execute your generator while you are developing it.
  676. <br/>Therefore, you have to create a new
  677. <strong>Generator Model</strong> with the generator id
  678. <strong>yakindu::generic</strong>, either by using the
  679. <strong>New Statechart Generator Model</strong> wizard or by simple creating a new text file with the file extension
  680. <strong>.sgen</strong>. the following feature allows to configure your code generator.
  681. </p>
  682. <p><!-- Start sgen_feature_generator --></p>
  683. <h4 id="Generator">Generator</h4>
  684. <p>The
  685. <strong>Generator</strong> feature allows the configuration of a custom code generator located in the workspace and written in Java or another JVM language. It is a
  686. <strong>required</strong> feature and consists of the following parameters:
  687. </p>
  688. <ol>
  689. <li>
  690. <i>generatorProject</i> (String, required): The name of the generator project
  691. </li>
  692. <li>
  693. <i>generatorClass</i> (String, required): The full qualified class name of the code generator class.
  694. </li>
  695. <li>
  696. <i>configurationModule</i> (String, optional): The full qualified class name for a guice module to configure the code generator
  697. </li>
  698. </ol>
  699. <p>Example configuration:</p>
  700. <pre><code>feature Generator {
  701. generatorProject = "org.yakindu.sct.mygenerator"
  702. generatorClass = "org.yakindu.sct.MyGenerator"
  703. }
  704. </code></pre>
  705. <p><!-- End sgen_feature_generator --></p>
  706. <h3 id="ExecutingacustomXpandcodegenerator">Executing a custom Xpand code generator</h3>
  707. <p>to execute an xpand based custom code generator, you have to create a new
  708. <strong>Generator Model</strong> with the generator id
  709. <strong>yakindu::xpand</strong>, either by using the
  710. <strong>New Statechart Generator Model</strong> wizard or by simple creating a new text file with the file extension
  711. <strong>.sgen</strong>. the following feature allows to configure your code generator.
  712. </p>
  713. <p><!-- Start sgen_feature_template --></p>
  714. <h4 id="Template">Template</h4>
  715. <p>The
  716. <strong>Generator</strong> feature allows the configuration of a custom code generator located in the workspace and written in Java or another JVM language. It is a
  717. <strong>required</strong> feature and consists of the following parameters:
  718. </p>
  719. <ol>
  720. <li>
  721. <i>templateProject</i> (String, required): The name of the generator project
  722. </li>
  723. <li>
  724. <i>templatePath</i> (String, required): The full qualified template path to the main template.
  725. </li>
  726. </ol>
  727. <p>Example configuration:</p>
  728. <pre><code>feature Template {
  729. templateProject = "ExampleProject"
  730. templatePath = "org::yakindu::sct::generator::xpand::Main::main"
  731. }
  732. </code></pre>
  733. <p><!-- End sgen_feature_template --></p>
  734. <h2 id="APIspecificationsofthegeneratedcode">API specifications of the generated code</h2>
  735. <p>In the following comments the TrafficLight example statemachine is used to describe the API specifications of the code generated by the Yakindu C and Java code generators. The following image shows the statechart. It is a model of a simple pedestrian crossing with a traffic light for pedestrians and a traffic light for the cars.</p>
  736. <p>
  737. <img border="0" src="images/TrafficLight.png"/>
  738. </p>
  739. <h3 id="SpecificationsofJavacode">Specifications of Java code</h3>
  740. <p>For Java you can checkout the project &#8218;org.yakindusct.examples.trafficlight&#8217; from the Yakindu google code repository (
  741. <a href="http://svn.codespot.com/a/eclipselabs.org/yakindu/SCT2/trunk/examples">Google code link</a> ). The Java example contains the statechart, sgen model, graphical widgets and some glue code to connect the generated code with the widgets. The graphical widgets are based on SWT. To execute the Java example you can run the file &#8218;CrossingDemoCycleBased.java&#8217; as &#8218;Java Application&#8217; from the eclipse &#8218;Run As&#8217; context menu.
  742. </p>
  743. <p>You find the generated code in the &#8218;src-gen&#8217; folder of the traffic light example.</p>
  744. <p>In the package &#8218;org.yakindu.sct.examples.trafficlight.cyclebased&#8217; are the most basic statemachine interfaces and classes located. These are needed by each statemachine and are independend from the concrete statemachine design.</p>
  745. <h4 id="InterfaceIStatemachine">Interface IStatemachine</h4>
  746. <p>The interface
  747. <code>IStatemachine</code> is implemented by each generated statemachine:
  748. </p>
  749. <pre><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
  750. /**
  751. * Basic interface for statemachines.
  752. *
  753. *
  754. */
  755. public interface IStatemachine {
  756. /**
  757. * Initializes the statemachine. Use to init internal variables etc.
  758. */
  759. public void init();
  760. /**
  761. * Enters the statemachine. Sets the statemachine in a defined state.
  762. */
  763. public void enter();
  764. /**
  765. * Exits the statemachine. Leaves the statemachine with a defined state.
  766. */
  767. public void exit();
  768. /**
  769. * Start a run-to-completion cycle.
  770. */
  771. public void runCycle();
  772. }
  773. </code></pre>
  774. <p>It contains the four methods
  775. <code>init()</code>,
  776. <code>enter()</code>,
  777. <code>exit()</code> and
  778. <code>runCycle()</code>. The
  779. <code>init()</code> method is used to initialize the internal objects of the statemachine after instantiation. Variables are initialized to a default value. If you have initialized variables in the statechart definition these initializations are done in the init method too. The
  780. <code>enter()</code> method should be called if the statemachine is entered. It sets the statemachine into a defined state. The
  781. <code>exit()</code> method is used to leave a statemachine statefully. If for example a history state is used in one of the top regions the last active state is stored and the statemachine is leaved via
  782. <code>exit()</code> and reentered via
  783. <code>enter()</code> it continues working with this state. The
  784. <code>runCycle()</code> method is used to trigger a run to completion step in which the statemachine evaluates arising events and computes possible state changes. A run to completion step consists in a simplified view of the following steps:
  785. </p>
  786. <ul>
  787. <li>Clear list of outgoing events.</li>
  788. <li>Check whether events have occurred which lead to a state change</li>
  789. <li>If a state change has to be done:
  790. <ul>
  791. <li>Execute exit actions of leaving state.</li>
  792. <li>Save history state if necessary.</li>
  793. <li>Set the new State active.</li>
  794. <li>Execute entry action of the new state.</li>
  795. </ul>
  796. </li>
  797. <li>Clear list of incoming events.</li>
  798. </ul>
  799. <h4 id="Timedstatemachines">Timed statemachines</h4>
  800. <p>In the traffic light example timing is used (after clauses). To support this the interfaces &#8218;ITimedStatemachine&#8217;, &#8218;ITimerService&#8217; and the class &#8218;TimeEvent&#8217; are generated.</p>
  801. <pre><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
  802. /**
  803. * Interface for state machines which use timed event triggers.
  804. */
  805. public interface ITimedStatemachine {
  806. /**
  807. * Set the {@link ITimerService} for the state machine. It must be set
  808. * externally on a timed state machine before a run cycle can be correct
  809. * executed.
  810. *
  811. * @param timerService
  812. */
  813. public void setTimerService(ITimerService timerService);
  814. /**
  815. * Returns the currently used timer service.
  816. *
  817. * @return {@link ITimerService}
  818. */
  819. public ITimerService getTimerService();
  820. /**
  821. * Callback method if a {@link TimeEvent} occurred.
  822. *
  823. * @param timeEvent
  824. */
  825. public void onTimeEventRaised(TimeEvent timeEvent);
  826. }
  827. </code></pre>
  828. <p>
  829. <code>ITimedStatemachine</code> extends the generated statemachine to set an
  830. <code>ITimerService</code> and provides a callback method
  831. <code>onTimeEventRaised(TimeEvent timeEvent)</code> to let the timer service raise
  832. <code>TimeEvents</code>.
  833. </p>
  834. <p>Basically the correct handling of time has to be implemented by the developer because timer functions generally depend on the hardware target used. So for every hardware target a timer service class which implements the
  835. <code>ITimerService</code> interface has to be developed. Let&#8217;s have a look at the
  836. <code>ITimerService</code> interface:
  837. </p>
  838. <pre><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
  839. /**
  840. * Interface a timer service has to implement. Use to implement your own timer
  841. * service. A timer service has to be added to a timed state machine.
  842. *
  843. */
  844. public interface ITimerService {
  845. /**
  846. * Starts the timing for a given {@link TimeEvent}.
  847. *
  848. * @param event
  849. * : The TimeEvent the timer service should throw if timed out.
  850. * @param time
  851. * : Time in milliseconds after the given time event should be
  852. * triggered
  853. * @param cycleStartTime
  854. * : The absolute start time in milliseconds at which the last
  855. * run cycle was called. Can be used to produce a more accurate
  856. * timing behavior.
  857. */
  858. public void setTimer(TimeEvent event, long time, long cycleStartTime);
  859. /**
  860. * Unset the given {@link TimeEvent}. Use to unset cyclic repeated time
  861. * events.
  862. *
  863. * @param event
  864. */
  865. public void resetTimer(TimeEvent event);
  866. /**
  867. * Cancel timer service. Use this to end possible timing threads and free
  868. * memory resources.
  869. */
  870. public void cancel();
  871. /**
  872. * Returns the system time in milliseconds.
  873. *
  874. * @return the difference, measured in milliseconds, between the current
  875. * time and a defined point of time in the past.
  876. */
  877. public long getSystemTimeMillis();
  878. }
  879. </code></pre>
  880. <p>The
  881. <code>ITimerService</code> interface defines four methods. The
  882. <code>public void setTimer(TimeEvent event, long time, long cycleStartTime)</code> method is called by a statemachine to tell the timer service that it has to start a timer for the given time event and raise it after the time given by the same named parameter is expired. It is important that within the &#8218;setTimer&#8217; method only a timer thread or a hardware timer interrupt is started and no long time taking operations like Thread.sleep(...) or waiting are done. Otherwise the statemachine execution may hang within the timer service or it shows a not expected runtime behavior.
  883. </p>
  884. <p>To raise a
  885. <code>TimeEvent</code> after the time is expired the method
  886. <code>onTimeEventRaised(TimeEvent timeEvent)</code> has to be called on the statemachine (should be a
  887. <code>ITimedStatemachine</code>). So the time event is recognized by the statemachine and will be processed by the next run cycle. You can conclude that the runtime environment has to call the statemachines &#8218;runCycle&#8217; method as often as needed to process time events raised by the timing service as fast as possible. If you have in example a time event which should be raised by the timer service after 500 ms and the runtime environment only calls the statemachines
  888. <code>runCycle</code> method with a time period of 1000 ms the event could not be processed in the correct time.
  889. </p>
  890. <p>The parameter &#8218;cycleStartTime&#8217; holds the absolute start time of the last run cycle. It can be used to reduce or remove the time offset used by the statemachines runtime operations to get a more precise timing behavior.</p>
  891. <p>The method
  892. <code>resetTimer(TimeEvent event)</code> is called by the statemachine to unset the timer.
  893. </p>
  894. <p>The last class used by timed statemachine is
  895. <code>TimeEvent</code>:
  896. </p>
  897. <pre><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
  898. /**
  899. * Event that reflects a time event. It's internally used by
  900. * {@link ITimedStatemachine}.
  901. *
  902. * @author muehlbrandt
  903. *
  904. * @param &lt;T&gt;
  905. */
  906. public class TimeEvent {
  907. private boolean periodic;
  908. private ITimedStatemachine statemachine;
  909. int index;
  910. /**
  911. * Constructor for a time event.
  912. *
  913. * @param periodic
  914. * : Set to {@code true} if event should be repeated
  915. * periodically.
  916. *
  917. * @param index
  918. * : Index position within the state machine's timeEvent array.
  919. */
  920. public TimeEvent(boolean periodic, int index) {
  921. this.periodic = periodic;
  922. this.index = index;
  923. }
  924. /**
  925. * Returns the state machine reference of the event.
  926. *
  927. */
  928. public ITimedStatemachine getStatemachine() {
  929. return statemachine;
  930. }
  931. /**
  932. * Sets the state machine reference of the event.
  933. *
  934. * @param statemachine
  935. */
  936. public void setStatemachine(ITimedStatemachine statemachine) {
  937. this.statemachine = statemachine;
  938. }
  939. public boolean isPeriodic() {
  940. return periodic;
  941. }
  942. public int getIndex() {
  943. return index;
  944. }
  945. }
  946. </code></pre>
  947. <p>A
  948. <code>TimeEvent</code> holds a reference to the statemachine it belongs to and a flag if it should be raised periodically. These informations are needed by the timer service implementation to raise the time event on the corresponding statemachine. The index field is used by the timed statemachine internally.
  949. </p>
  950. <h4 id="DefaultimplementationofITimerService">Default implementation of ITimerService</h4>
  951. <p>The java code generator generates a default implementation of
  952. <code>ITimerService</code> interface if the TimerService feature is set to &#8218;true&#8217; in the sgen model. This implementation is based on
  953. <code>java.util.Timer</code> and
  954. <code>java.util.TimerTask</code> and should be compatible with the normal Oracle VM or Open JDK VM:
  955. </p>
  956. <pre><code>
  957. package org.yakindu.sct.examples.trafficlight.cyclebased;
  958. import java.util.HashMap;
  959. import java.util.Map;
  960. import java.util.Timer;
  961. import java.util.TimerTask;
  962. /**
  963. * Default timer service implementation.
  964. *
  965. */
  966. public class TimerService implements ITimerService {
  967. private final Timer timer = new Timer();
  968. private final Map&lt;TimeEvent, TimerTask&gt; timerTaskMap = new HashMap&lt;TimeEvent, TimerTask&gt;();
  969. public void setTimer(final TimeEvent event, long time,
  970. long cycleStartTime) {
  971. // Reset existing TimerTask for event. This step isn't necessary if
  972. // timer tasks are properly reset by sexec model.
  973. if (timerTaskMap.containsKey(event)) {
  974. resetTimer(event);
  975. }
  976. // Create a new TimerTask for given event.
  977. timerTaskMap.put(event, new TimerTask() {
  978. @Override
  979. public void run() {
  980. event.getStatemachine().onTimeEventRaised(event);
  981. }
  982. });
  983. // start scheduling the timer
  984. if (event.isPeriodic()) {
  985. timer.scheduleAtFixedRate(timerTaskMap.get(event),
  986. time - (System.currentTimeMillis() - cycleStartTime), time);
  987. } else {
  988. timer.schedule(timerTaskMap.get(event),
  989. time - (System.currentTimeMillis() - cycleStartTime));
  990. }
  991. }
  992. public void resetTimer(TimeEvent event) {
  993. if (timerTaskMap.containsKey(event) &amp;&amp; timerTaskMap.get(event) != null) {
  994. timerTaskMap.get(event).cancel();
  995. timer.purge();
  996. }
  997. timerTaskMap.remove(event);
  998. }
  999. public void cancel() {
  1000. timer.cancel();
  1001. timer.purge();
  1002. }
  1003. public long getSystemTimeMillis() {
  1004. return System.currentTimeMillis();
  1005. }
  1006. }
  1007. </code></pre>
  1008. <h4 id="Runtimeservice">Runtime service</h4>
  1009. <p>The runtime service class can be used by client implementations to execute a run to completion step of a statemachine periodically.</p>
  1010. <h4 id="Integrationofgeneratedcode">Integration of generated code</h4>
  1011. <p>To get a clue how to integrate the generated java statemachines into your existing projects have a look at the
  1012. <br/>
  1013. <code>CrossingDemoCycleBased</code> class and it&#8217;s abstract super class
  1014. <code>CrossingDemoBase</code>. The code starts with the main method in
  1015. <code>CrossingDemoCycleBased</code> class:
  1016. </p>
  1017. <pre><code>public static void main(String[] args) {
  1018. new CrossingDemoCycleBased().runTrafficLight();
  1019. }
  1020. </code></pre>
  1021. <p>A new instance of the class is created and the method
  1022. <code>runTrafficLight()</code> is directly called. This method consist in the super class:
  1023. </p>
  1024. <pre><code>public void runTrafficLight() {
  1025. setUpAndRunStatemachine();
  1026. createUIContent();
  1027. shell.open();
  1028. while (!shell.isDisposed()) {
  1029. // update traffic lights
  1030. readStatemachineOutput();
  1031. crossing.repaint();
  1032. if (!display.readAndDispatch()) {
  1033. display.sleep();
  1034. }
  1035. }
  1036. tearDownStatemachine();
  1037. }
  1038. </code></pre>
  1039. <p>This method setups the statemachine and creates the UI content. In a while loop the content of the statemachine is read and the ui is repainted. If the ui shell is closed the loop is left and the statemachine is teared down. The really interresting methods are
  1040. <code>setUpAndRunStatemachine()</code>,
  1041. <code>readStatemachinOutput</code> and
  1042. <code>tearDownStatemachine()</code>:
  1043. </p>
  1044. <pre><code>protected void setUpAndRunStatemachine() {
  1045. statemachine = new TrafficLightWaitingStatemachine();
  1046. statemachine.setTimerService(new TimerService());
  1047. statemachine.init();
  1048. statemachine.enter();
  1049. RuntimeService.getInstance().registerStatemachine(statemachine, 100);
  1050. }
  1051. </code></pre>
  1052. <p>First a new instance of the generated statemachine is created. Because the traffic light statechart uses timing clauses the default implementation of the
  1053. <code>TimerService</code> is set. In the next steps the statemachine is initialized and entered. After the enter method ist executed the machine stays in a defined state. Finally the statemachine is passed to the runtime service. This service executes the
  1054. <code>runCycle()</code> method of the statemachine every 100 ms. So the statemachine executes a run to completion step every 100 ms.
  1055. </p>
  1056. <pre><code>protected void readStatemachineOutput() {
  1057. trafficLightFigure.setRed(statemachine.getSCITrafficLight()
  1058. .getRed());
  1059. trafficLightFigure.setYellow(statemachine.getSCITrafficLight()
  1060. .getYellow());
  1061. trafficLightFigure.setGreen(statemachine.getSCITrafficLight()
  1062. .getGreen());
  1063. pedestrianLightFigure.setWhite(statemachine.getSCIPedestrian()
  1064. .getRequest());
  1065. pedestrianLightFigure.setRed(statemachine.getSCIPedestrian()
  1066. .getRed());
  1067. pedestrianLightFigure.setGreen(statemachine.getSCIPedestrian()
  1068. .getGreen());
  1069. }
  1070. </code></pre>
  1071. <p>The generated code contains getters and setters for each variable and event. So it&#8217;s easy to read values from and write values to a statemachine or raise events and ask the statemachine if outgoing events were raised during the last run to completion step. Within
  1072. <code>readStatemachineOutput()</code> method these methods are used to read the light values from the statemachine and set them to the ui elements. Within the methods
  1073. <code>pedestrianRequestButtonClicked()</code> and
  1074. <code>onOffButtonClicked()</code> some events are raised.
  1075. </p>
  1076. <p>Hint:
  1077. <br/>If outgoing events are raised within the statemachine they remain active until the next run to completion step is started.
  1078. </p>
  1079. <pre><code>@Override
  1080. protected void tearDownStatemachine() {
  1081. // End TimerHandler and RuntimeService.
  1082. statemachine.getTimerService().cancel();
  1083. RuntimeService.getInstance().cancelTimer();
  1084. }
  1085. </code></pre>
  1086. <p>If the UI thread has been terminated by the user, the state machine will be shut down. It is necessary to end the timer service. Finally the runtime service is cancelled.</p>
  1087. <h3 id="SpecificationsofCcode">Specifications of C code</h3>
  1088. <p>For C you can checkout the project &#8218;org.yakindu.examples.c.trafficlight&#8217; from the Yakindu google code repository (
  1089. <a href="http://svn.codespot.com/a/eclipselabs.org/yakindu/SCT2/trunk/examples">Google code link</a> ). The C example contains the statechart, sgen model, graphical widgets and some glue code to connect the generated code with the widgets. The graphical widgets are based on Qt. To execute the c example you can run the file org_yakindu_sct_examples_c_trafficlight as &#8218;Local C/C++ application&#8217; from the eclipse &#8218;Run As&#8217; context menu.
  1090. </p>
  1091. </body>
  1092. </html>