123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668 |
- <?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">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>reference</title>
- <link type="text/css" rel="stylesheet" href="../style.css"/>
- </head>
- <body>
- <h1 id="YAKINDUSCT2Reference">SCT Reference</h1>
- <h2 id="Statechartelements">Statechart elements</h2>
- <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>
- <p>The YAKINDU SCT 2 meta model is designed similar to the UML state chart meta model with the following differences</p>
- <ul>
- <li>they are self contained with interfaces defined by events and variables</li>
- <li>core execution semantics are cycle driven, not event driven
- <ul>
- <li>this allows to process concurrent events</li>
- <li>event driven behavior can be defined on top</li>
- </ul>
- </li>
- <li>time is an abstract concept for state charts</li>
- <li>time control is delegated to the environment</li>
- </ul>
- <p>The model interpreter and different flavors of generated code follow these same core semantics.</p>
- <p>Please refer to the description of the
- <a href="http://en.wikipedia.org/wiki/UML_state_machine">UML Statecharts</a> for more details.
- </p>
- <h3 id="Regions">Regions</h3>
- <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>
- <p>
- <img border="0" src="images/parallelRegions.jpg"/>
- </p>
- <h3 id="States">States</h3>
- <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>
- <h3 id="Transitions">Transitions</h3>
- <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>
- <p>The syntax of events and actions is defined by a textual description language (#Statechartdescriptionlanguage). Please refer to the documentation section
- <a href="#Events">Events</a> for more details. For more details on Actions refer to the chapter
- <a href="#ReactionTriggers">Actions</a>.
- </p>
- <p>If a state has more than one outgoing transition without event that transition is carried out first that was modeled first.</p>
- <h3 id="Initialstateandfinalstate">Initial state and final state</h3>
- <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>
- <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>
- <p>Inside a region only one initial state is allowed, but every region can have an initial state.</p>
- <h3 id="Choice">Choice</h3>
- <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>
- <p>Usually the first transition points towards the choice node. One of the choice outgoing transitions can carry a condition. </p>
- <h3 id="Junction">Junction</h3>
- <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>
- <h3 id="CompositeState">Composite State</h3>
- <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>
- <p>Composite states contain other state machine branches.</p>
- <h4 id="Orthogonalstates">Orthogonal states</h4>
- <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>
- <p>
- <img border="0" src="images/orthogonalState_example.jpg"/>
- </p>
- <h4 id="Submachinestates">Subdiagrams </h4>
- <p>When using Composite States, the statechart model often becomes too big to get a complete overview of the whole diagram. Although it is possible to collapse and expand a state’s figure compartment, these actions spoil the diagram layout each time they are executed. Therefore, we introduced so-called Subdiagrams.
- <br/>
- <img border="0" src="images/extract_subdiagram.png"/>
- <br/>If the ‚Extract Subdiagram’ refactoring is executed on a Composite State, all containing Regions are extracted into a separate diagram. A small decorator in the lower right corner of the State indicates the existence of such a subdiagram. If you hover with the mouse cursor over the decorator, you get a small preview image of the subdiagrams content. The refactoring also creates required Entry and Exit Points for you.
- <br/>
- <img border="0" src="images/extract_subdiagram2.png"/>
- <br/>With a click on the decorator, the subdiagram opens in a separate editor tab. The breadcrumb on the top allows easy navigation throughout the different hierachy levels.
- <br/>
- <img border="0" src="images/extract_subdiagram3.png"/>
- </p>
- <h3 id="ShallowHistory">Shallow History</h3>
- <p>The shallow history state is a pseudo state that is placed inside regions of composite states. It is used to ‚remember’ 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>
- <p>
- <img border="0" src="images/shallowHistory01.jpg"/>
- </p>
- <p>The interesting parts in this state chart are the events
- <em>checkProgress</em> and
- <em>goon</em>. CheckProgress jumps back to the init state while assigning the current progress count to the variable
- <em>temp</em>.
- <em>goon</em> jumps to the shallow history state that was placed inside the composite state.
- </p>
- <p>
- <img border="0" src="images/shallowHistory02.jpg"/>
- </p>
- <p>
- <img border="0" src="images/shallowHistory03.jpg"/>
- </p>
- <p>On triggering the
- <em>goon</em> event the last active state is activated again.
- </p>
- <h3 id="DeepHistory">Deep History</h3>
- <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>
- <h2 id="Statechartdescriptionlanguage">Statechart description language</h2>
- <p>The textual description language is used to declare and describe behaviors in the state machine. It is case sensitive.</p>
- <h3 id="Typesystem">Typesystem</h3>
- <p>The language has an integrated small typesystem with the following simple types:</p>
- <ul>
- <li>integer</li>
- <li>real</li>
- <li>boolean</li>
- <li>string</li>
- <li>void</li>
- </ul>
- <p>So events and variables can be declared with types:</p>
- <pre class="prettyprint"><code>var intVar : integer
- var realVar : real
- var boolVar : boolean
- var stringVar : string
- event addInt : integer
- event addReal : real
- event checkValidity : boolean
- event stringEvent : string
- event voidEvent : void
-
- </code></pre>
- <h3 id="Expressions">Expressions</h3>
- <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>
- <p>Logical expressions are similar to other programming languages. The return type is
- <strong>boolean</strong>. In the following there are some examples of these:
- </p>
- <h4 id="LogicalAND">Logical AND</h4>
- <pre class="prettyprint"><code>var1 && var2
- </code></pre>
- <h4 id="LogicalOR">Logical OR</h4>
- <pre class="prettyprint"><code>var1 || var2
- </code></pre>
- <h4 id="LogicalNOT">Logical NOT</h4>
- <pre class="prettyprint"><code>!var1
- </code></pre>
- <h4 id="Conditionalexpression">Conditional expression</h4>
- <pre class="prettyprint"><code>var1 ? var2 : 23
- </code></pre>
- <h4 id="BitwiseXOR">Bitwise XOR</h4>
- <pre class="prettyprint"><code>var1 ^ var2
- </code></pre>
- <h4 id="BitwiseOR">Bitwise OR</h4>
- <pre class="prettyprint"><code>var1 | var2
- </code></pre>
- <h4 id="BitwiseAND">Bitwise AND</h4>
- <pre class="prettyprint"><code>var1 & var2
- </code></pre>
- <h4 id="LogicalRelationsandShiftOperators">Logical Relations and Shift Operators</h4>
- <table>
- <tr>
- <td>less than </td>
- <td>< </td>
- </tr>
- <tr>
- <td>equal or less than </td>
- <td><= </td>
- </tr>
- <tr>
- <td>greater than </td>
- <td>> </td>
- </tr>
- <tr>
- <td>equal or greater than </td>
- <td>>= </td>
- </tr>
- <tr>
- <td>equal </td>
- <td>== </td>
- </tr>
- <tr>
- <td>not equal </td>
- <td>!= </td>
- </tr>
- <tr>
- <td>shift left </td>
- <td><< </td>
- </tr>
- <tr>
- <td>shift right </td>
- <td>>> </td>
- </tr>
- </table>
- <h4 id="Binaryarithmeticoperators">Binary arithmetic operators</h4>
- <table>
- <tr>
- <td>plus </td>
- <td>+ </td>
- </tr>
- <tr>
- <td>minus </td>
- <td>- </td>
- </tr>
- <tr>
- <td>multiply </td>
- <td>* </td>
- </tr>
- <tr>
- <td>divide </td>
- <td>/ </td>
- </tr>
- <tr>
- <td>modulo </td>
- <td>% </td>
- </tr>
- </table>
- <h4 id="Unaryarithmeticoperators">Unary arithmetic operators</h4>
- <table>
- <tr>
- <td>positive </td>
- <td>+ </td>
- </tr>
- <tr>
- <td>negative </td>
- <td>- </td>
- </tr>
- <tr>
- <td>complement </td>
- <td>~ </td>
- </tr>
- </table>
- <h3 id="Statements">Statements</h3>
- <p>A statements can be either an assignment, raising an event or call an operation. The language has the following assignment operators:</p>
- <ul>
- <li>simple assignment: = </li>
- <li>multiply and assign: *= </li>
- <li>divide and assign: /= </li>
- <li>calculate modulo and assign: %= </li>
- <li>add and assign: += </li>
- <li>subtract and assign: -= </li>
- <li>bitshift left and assign: <<= </li>
- <li>bitshift right and assign: >>= </li>
- <li>bitwise AND and assign: &= </li>
- <li>bitwise XOR and assign: ^= </li>
- <li>bitwise OR and assign:
- <code>|=</code>
- </li>
- </ul>
- <p>An event is raised by the keyword
- <em>raise</em> followed by the event name and if it is an interface event the name of the interface.
- </p>
- <p>An operation is called similar to other programming languages with the operation name and passing concrete parameters. The parameters can be expressions.</p>
- <h3 id="Scopes">Scopes</h3>
- <p><!-- Start stext_keyword_namespace --></p>
- <h4 id="Namespace">Namespace</h4>
- <p>The language allows to define unique namespaces, which can be used to qualify references to the statechart.</p>
- <pre class="prettyprint"><code>namespace trafficlights
- </code></pre>
- <p><!-- End stext_keyword_namespace -->
- <br/><!-- Start stext_keyword_interface -->
- </p>
- <h4 id="interfacescope">interface scope</h4>
- <p>Declarations in the interface scope are externally visible. They can be shared within the environment.</p>
- <pre class="prettyprint"><code>interface NamedInterface:
- in event event1
- out event event3 : integer
- var variable1 : integer
- </code></pre>
- <p><!-- End stext_keyword_interface -->
- <br/><!-- Start stext_keyword_internal -->
- </p>
- <h4 id="internalscope" class="'internalscope">internal scope</h4>
- <p>Declarations made in an internal scope are only visible for contained states.</p>
- <pre class="prettyprint"><code>internal:
- var localVariable1: integer
- event localEvent: integer
- local event localEvent2
- operation localOperation (int1 : integer, int2 : integer): integer
- localEvent2 / raise NamedInterface.event3 :
- localOperation(valueof(localEvent) , NamedInterface.variable1)
- </code></pre>
- <p><!-- End stext_keyword_internal --></p>
- <h3 id="Declarations">Declarations</h3>
- <p>Within scopes there can be declarations of Events, Variables, Operations, LocalReactions.</p>
- <p><!-- Start stext_keyword_event --></p>
- <h3 id="Events">Events</h3>
- <p>Within interface scope events have an direction. They can either be ingoing or outgoing:</p>
- <pre class="prettyprint"><code>interface NamedInterface:
- in event event1
- out event event2
- </code></pre>
- <p>Within local scope events can carry variables:</p>
- <pre class="prettyprint"><code>internal:
- event localEvent1 : integer
- </code></pre>
- <p>Local events can have a value assignment:</p>
- <pre class="prettyprint"><code>internal:
- event localEvent1: integer = 25
- </code></pre>
- <p><!-- End stext_keyword_event --></p>
- <p><!-- Start stext_keyword_var --></p>
- <h3 id="Variables">Variables</h3>
- <p>Variables can have different visibilities. They can be visible for the environment:</p>
- <pre class="prettyprint"><code>var variable1: real
- </code></pre>
- <p>Variables can be
- <strong>readonly</strong> (constants):
- </p>
- <pre class="prettyprint"><code>var readonly pi: real = 3.1415
- </code></pre>
- <p>Variables can be referenced by the environment.</p>
- <pre class="prettyprint"><code>var external variable3: integer = 34
- </code></pre>
- <p><!-- End stext_keyword_var --></p>
- <h3 id="ReactionTriggers">Reaction Triggers</h3>
- <p>Actions are key constructs in state machines to model behavior. The YAKINDU SCT 2 knows the following kinds of actions.</p>
- <p><!-- Start stext_keyword_after --></p>
- <h4 id="after">after</h4>
- <p>The
- <em>after</em> trigger specifies one-shot time events.
- </p>
- <p>After the specified time the reaction is triggered. An
- <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.
- </p>
- <pre class="prettyprint"><code>after 20 s
- </code></pre>
- <p>Structure: </p>
- <p>
- <code>after</code>
- <em>
- <code>time</code>
- </em>
- <em>
- <code>unit</code>
- </em>
- </p>
- <p>The time value may be a constant or an expression that returns an integer value.</p>
- <p>The time unit can be:</p>
- <ul>
- <li><!-- Start stext_keyword_s --> s – seconds <!-- End stext_keyword_s --></li>
- <li><!-- Start stext_keyword_ms --> ms – milliseconds <!-- End stext_keyword_ms --></li>
- <li><!-- Start stext_keyword_us --> us – microseconds <!-- End stext_keyword_us --></li>
- <li><!-- Start stext_keyword_ns --> ns – nanoseconds <!-- End stext_keyword_ns --></li>
- </ul>
- <p><!-- End stext_keyword_after -->
- <br/><!-- Start stext_keyword_every -->
- </p>
- <h4 id="every">every</h4>
- <p>The
- <em>every</em> trigger specifies periodic time events.
- </p>
- <p>The reaction is triggered periodically after the specified time. An
- <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.
- </p>
- <pre class="prettyprint"><code>every 200 ms
- </code></pre>
- <p>Structure: </p>
- <p>
- <code>every</code>
- <em>
- <code>time</code>
- </em>
- <em>
- <code>unit</code>
- </em>
- </p>
- <p>The time value may be a constant or an expression that returns an integer value.</p>
- <p>The time unit can be:</p>
- <ul>
- <li>s – seconds</li>
- <li>ms – milliseconds</li>
- <li>us – microseconds</li>
- <li>ns – nanoseconds</li>
- </ul>
- <p><!-- End stext_keyword_every -->
- <br/><!-- Start stext_keyword_always -->
- </p>
- <h4 id="always">always</h4>
- <p>This trigger is always true and enables a reaction to be executed in every run to completion step (RTS). It is equivalent to
- <em>oncycle</em>.
- </p>
- <p><!-- End stext_keyword_always --></p>
- <p><!-- Start stext_keyword_default -->
- <br/><!-- Start stext_keyword_else -->
- </p>
- <h4 id="defaultelse">default, else</h4>
- <p>The
- <em>default</em> trigger is equivalent to the
- <em>else</em> trigger. It is intended for use for the outgoing transitions of
- <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.
- </p>
- <p><!-- End stext_keyword_else -->
- <br/><!-- End stext_keyword_default -->
- </p>
- <p><!-- Start stext_keyword_entry --></p>
- <h4 id="entry">entry</h4>
- <p>An
- <em>entry</em> trigger marks actions that are carried out on entering a state or state machine.
- </p>
- <p><!-- End stext_keyword_entry -->
- <br/><!-- Start stext_keyword_exit -->
- </p>
- <h4 id="exit">exit</h4>
- <p>An
- <em>exit</em> trigger marks actions that are carried out on exiting a state or state machine.
- </p>
- <p><!-- End stext_keyword_exit -->
- <br/><!-- Start stext_keyword_oncycle -->
- </p>
- <h4 id="oncycle">oncycle</h4>
- <p>The
- <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
- <em>always</em>.
- </p>
- <p><!-- End stext_keyword_oncycle -->
- <br/><!-- Start stext_keyword_operation -->
- </p>
- <h3 id="Operations">Operations</h3>
- <p>Operations can have none, one or multiple parameters. The parameters are declarated by a name and a type. An operation can have one return type similar to Java.</p>
- <pre class="prettyprint"><code>operation localOperation (xValue : integer, yValue : integer):integer
- </code></pre>
- <p>
- <br/><!-- End stext_keyword_operation -->
- </p>
- <h3 id="BuildInFunctions">Build-In Functions</h3>
- <p><!-- Start stext_keyword_valueof --></p>
- <h4 id="valueofevent">valueof(event)</h4>
- <p>Returns the value of an valued event that it passed to the function as parameter.</p>
- <pre class="prettyprint"><code>myVar = valueof(myEvent)
- </code></pre>
- <p></p>
- <p><!-- End stext_keyword_valueof --></p>
- <p><!-- Start stext_keyword_active --></p>
- <h4 id="activestate">active(state)</h4>
- <p>Returns „true” if a state is active or „false” otherwise.</p>
- <pre class="prettyprint"><code>myBool = active(StateA)
- </code></pre>
- <p></p>
- <p><!-- End stext_keyword_active --></p>
- <h3 id="LocalReactions">LocalReactions</h3>
- <p>Local reactions describe the internal behavior of a state. So they have internal scope. A local reaction is declared as follows:</p>
- <pre class="prettyprint"><code>LocalReaction: ReactionTrigger '/' ReactionEffect ('#' ReactionProperties)?
- ReactionTrigger: (Event ("," Event )* (=> '[' Expression ']')?) | '[' Expression ']'
- ReactionEffect: Statement (';' Statement )* (';')?
- Statement: Assignment | EventRaising | OperationCall
- ReactionProperties: (EntryPoint | ExitPoint)*
- </code></pre>
- <p>Within a local reaction an interface event can be raised:</p>
- <pre class="prettyprint"><code>internal:
- localEvent1 / raise NamedInterface.event3 : localOperation (valueof(localEvent), NamedInterface.variable1);
- </code></pre>
- <p>Local reactions can have priority values. These are defined by a following # and the integer number of priority:</p>
- <pre id="GeneratorFeatures" class="prettyprint"><code>localEvent2 / NamedInterface.variable2 += 3; #1
- localEvent3 / NamedInterface.variable4 += 2.0; #2
- </code></pre>
- <h2 id="SGenGeneratorFeatures">SGen Generator Features</h2>
- <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>
- <p>To get started with the generator model, we included a new Eclipse wizard that creates a basic configuration file with default values.</p>
- <p>
- <img border="0" src="images/sGenEditor.png"/>
- </p>
- <p>The generator model is associated with the builder. If
- <strong>Project</strong> >
- <strong>Build Automatically</strong> is checked the files are generated. In the following the specific customizing features of the generator models are explained.
- </p>
- <p>The following section describes the
- <strong>Core Features</strong> which are available for all code generators:
- </p>
- <p><!-- Start sgen_feature_outlet --></p>
- <h4 id="Outlet">Outlet</h4>
- <p>The
- <strong>Outlet</strong> feature specifies the target project and folder for the generated artifacts. It is a
- <strong>required</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>targetProject</i> (String, required): The project to store the generated artifacts
- </li>
- <li>
- <i>targetFolder</i> (String, required): The folder to store the generated artifacts
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature Outlet {
- targetProject = "ExampleProject"
- targetFolder = "src-gen"
- }
- </code></pre>
- <p><!-- End sgen_feature_outlet -->
- <br/><!-- Start sgen_feature_licenseheader -->
- </p>
- <h4 id="LicenseHeader">LicenseHeader</h4>
- <p>The
- <strong>LicenseHeader</strong> feature specifies the license text that should be added as a header to the generated artifacts. It is an
- <strong>optional</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>licenseText</i> (String, required): The license text to add as a header
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature LicenseHeader {
- licenseText = "Copyright (c) 2012 committers of YAKINDU and others."
- }
- </code></pre>
- <p><!-- End sgen_feature_licenseheader -->
- <br/><!-- Start sgen_feature_functioninlining -->
- </p>
- <h4 id="FunctionInlining">FunctionInlining</h4>
- <p>The
- <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.
- <br/>It is an
- <strong>optinal</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>inlineReactions</i> (Boolean, optional): Inlines the expression for reactions
- </li>
- <li>
- <i>inlineEntryActions</i> (Boolean, optional): Inlines the expression for entry actions
- </li>
- <li>
- <i>inlineExitActions</i> (Boolean, optional): Inlines the expression for exit actions
- </li>
- <li>
- <i>inlineEnterSequences</i> (Boolean, optional): Inlines the expression for enter sequences
- </li>
- <li>
- <i>inlineExitSequences</i> (Boolean, optional): Inlines the expression for exit sequences
- </li>
- <li>
- <i>inlineChoices</i> (Boolean, optional): Inlines the expression for choices
- </li>
- <li>
- <i>inlineEnterRegion</i> (Boolean, optional): Inlines the expression for enter regions
- </li>
- <li>
- <i>inlineExitRegion</i> (Boolean, optional): Inlines the expression for exit regions
- </li>
- <li>
- <i>inlineEntries</i> (Boolean, optional): Inlines the expression for entries
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature FunctionInlining {
- inlineChoices = false
- inlineEnterRegion = true
- inlineEntries = true
- }
- </code></pre>
- <p><!-- End sgen_feature_functioninlining -->
- <br/><!-- Start sgen_feature_debug -->
- </p>
- <h4 id="Debug">Debug</h4>
- <p>The
- <strong>Debug</strong> feature dumps the Execution Model to the target folder as xmi model. It is an
- <strong>optional</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>dumpSexec</i> (Boolean, required): Dumps the execution model as xmi model
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature Debug {
- dumpSexec = true
- }
- </code></pre>
- <p><!-- End sgen_feature_debug --></p>
- <h2 id="JavaGeneratorFeatures">Java Generator Features</h2>
- <p><!-- Start sgen_feature_naming --></p>
- <h4 id="Naming">Naming</h4>
- <p>The
- <strong>Naming</strong> feature allows the configuration of package names as well as class name prefix / suffix.
- <br/>It is an
- <strong>optional</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>basePackage</i> (Boolean, required): The package to create for the generated java classes
- </li>
- <li>
- <i>implementationSuffix</i> (Boolean, optional): The suffix for the implementing classes
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature Naming {
- basePackage = "org.yakindu.sct"
- implementationSuffix = "Impl"
- }
- </code></pre>
- <p><!-- End sgen_feature_naming -->
- <br/><!-- Start sgen_feature_generalfeatures -->
- </p>
- <h4 id="GeneralFeatures">GeneralFeatures</h4>
- <p>The
- <strong>GeneralFeatures</strong> feature allows to configure additional services to generate with the statemachine. Per default, all parameters are configured to
- <i>false</i> It is an
- <strong>optional</strong> feature and consists of the following parameters:
- </p>
- <p><!-- # __EventBasedStatemachine__ (Boolean, optional): Enables/disables the generation of a cycle based statemachine implementation --></p>
- <ol>
- <li>
- <i>InterfaceObserverSupport</i> (Boolean, optional): Enables/disables the generation of listener interfaces for the statemachine
- </li>
- <li>
- <i>RuntimeService</i> (Boolean, optional): Enables/disables the generation of a runtime service that triggers the runcycle of a cycle based statemachine
- </li>
- <li>
- <i>TimerService</i> (Boolean, optional): Enables/disables the generation of a timer service implementation using
- <i>java.util.Timer</i>
- </li>
- </ol>
- <p><!-- # __GenericInterfaceSupport__ (Boolean, optional): Enables/disables the generation of generic interfaces -->
- <br/><!-- # __StatemachineFactorySupport__ (Boolean, optional): Enables/disables the generation of a factory class -->
- </p>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature GeneralFeatures {
- InterfaceObserverSupport = true
- RuntimeService = true
- TimerService = true
- }
- </code></pre>
- <p><!-- End sgen_feature_generalfeatures --> </p>
- <h2 id="CreateCustomCodeGenerators">Create Custom Code Generators</h2>
- <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,
- <a href="http://www.eclipse.org/xtend/">Xtend</a> or in
- <a href="http://www.eclipse.org/modeling/m2t/?project=xpand">Xpand</a>
- </p>
- <h3 id="WritingacustomcodegeneratorwithXtend2Java">Writing a custom code generator with Xtend2/Java</h3>
- <p>First, you have to create a new Xtend2 generator project. Click
- <strong>File</strong> >
- <strong>New</strong> >
- <strong>Other...</strong> >
- <strong>YAKINDU</strong> >
- <strong>YAKINDU Xtend2/Java Generator Project</strong> to create a new Xtend2 Generator Project.
- </p>
- <p>
- <img border="0" src="images/xtendGenerator.png"/>
- </p>
- <p>The wizards asks for a
- <strong>Project name</strong> and the name of the
- <strong>Generator class</strong>, where you have to specify a full qualified class name. If you check the
- <strong>Use Xtend</strong> checkbox, the Generator class will be initially created as an
- <a href="http://www.eclipse.org/xtend/">Xtend</a> class. Otherwise, Java will be used for the generator.
- </p>
- <p>The check box
- <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
- <strong>Generator ID</strong>. If you want to contribute custom generator features for your code generator, check the
- <strong>Create Feature Library</strong> check box.
- </p>
- <p>After click on
- <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].
- </p>
- <h3 id="ExecutingacustomXtend2Javacodegenerator">Executing a custom Xtend2/Java code generator</h3>
- <p>YAKINDU Statechart Tools provide a convenient way to execute your generator while you are developing it.
- <br/>Therefore, you have to create a new
- <strong>Generator Model</strong> with the generator id
- <strong>yakindu::generic</strong>, either by using the
- <strong>New Statechart Generator Model</strong> wizard or by simple creating a new text file with the file extension
- <strong>.sgen</strong>. the following feature allows to configure your code generator.
- </p>
- <p><!-- Start sgen_feature_generator --></p>
- <h4 id="Generator">Generator</h4>
- <p>The
- <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
- <strong>required</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>generatorProject</i> (String, required): The name of the generator project
- </li>
- <li>
- <i>generatorClass</i> (String, required): The full qualified class name of the code generator class.
- </li>
- <li>
- <i>configurationModule</i> (String, optional): The full qualified class name for a guice module to configure the code generator
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature Generator {
- generatorProject = "org.yakindu.sct.mygenerator"
- generatorClass = "org.yakindu.sct.MyGenerator"
- }
-
- </code></pre>
- <p><!-- End sgen_feature_generator --></p>
- <h3 id="ExecutingacustomXpandcodegenerator">Executing a custom Xpand code generator</h3>
- <p>to execute an xpand based custom code generator, you have to create a new
- <strong>Generator Model</strong> with the generator id
- <strong>yakindu::xpand</strong>, either by using the
- <strong>New Statechart Generator Model</strong> wizard or by simple creating a new text file with the file extension
- <strong>.sgen</strong>. the following feature allows to configure your code generator.
- </p>
- <p><!-- Start sgen_feature_template --></p>
- <h4 id="Template">Template</h4>
- <p>The
- <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
- <strong>required</strong> feature and consists of the following parameters:
- </p>
- <ol>
- <li>
- <i>templateProject</i> (String, required): The name of the generator project
- </li>
- <li>
- <i>templatePath</i> (String, required): The full qualified template path to the main template.
- </li>
- </ol>
- <p>Example configuration:</p>
- <pre class="prettyprint"><code>feature Template {
- templateProject = "ExampleProject"
- templatePath = "org::yakindu::sct::generator::xpand::Main::main"
- }
- </code></pre>
- <p><!-- End sgen_feature_template --></p>
- <h2 id="APISecification">API specifications of the generated code</h2>
- <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>
- <p>
- <img id="SpecJava" class="img-rounded shadowed" border="0" src="images/TrafficLight.png"/>
- </p>
- <h3 id="SpecificationsofJavacode">Specifications of Java code</h3>
- <p>For Java you can checkout the project ‚org.yakindusct.examples.trafficlight’ from the Yakindu google code repository (
- <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 ‚CrossingDemoCycleBased.java’ as ‚Java Application’ from the eclipse ‚Run As’ context menu.
- </p>
- <p>You find the generated code in the ‚src-gen’ folder of the traffic light example.</p>
- <p>In the package ‚org.yakindu.sct.examples.trafficlight.cyclebased’ are the most basic statemachine interfaces and classes located. These are needed by each statemachine and are independend from the concrete statemachine design.</p>
- <h4 id="InterfaceIStatemachine">Interface IStatemachine</h4>
- <p>The interface
- <code>IStatemachine</code> is implemented by each generated statemachine:
- </p>
- <pre class="prettyprint"><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
- /**
- * Basic interface for statemachines.
- *
- *
- */
- public interface IStatemachine {
- /**
- * Initializes the statemachine. Use to init internal variables etc.
- */
- public void init();
- /**
- * Enters the statemachine. Sets the statemachine in a defined state.
- */
- public void enter();
- /**
- * Exits the statemachine. Leaves the statemachine with a defined state.
- */
- public void exit();
- /**
- * Start a run-to-completion cycle.
- */
- public void runCycle();
- }
- </code></pre>
- <p>It contains the four methods
- <code>init()</code>,
- <code>enter()</code>,
- <code>exit()</code> and
- <code>runCycle()</code>. The
- <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
- <code>enter()</code> method should be called if the statemachine is entered. It sets the statemachine into a defined state. The
- <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
- <code>exit()</code> and reentered via
- <code>enter()</code> it continues working with this state. The
- <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:
- </p>
- <ul>
- <li>Clear list of outgoing events.</li>
- <li>Check whether events have occurred which lead to a state change</li>
- <li>If a state change has to be done:
- <ul>
- <li>Execute exit actions of leaving state.</li>
- <li>Save history state if necessary.</li>
- <li>Set the new State active.</li>
- <li>Execute entry action of the new state.</li>
- </ul>
- </li>
- <li>Clear list of incoming events.</li>
- </ul>
- <h4 id="Timedstatemachines">Timed statemachines</h4>
- <p>In the traffic light example timing is used (after clauses). To support this the interfaces ‚ITimedStatemachine’, ‚ITimerService’ and the class ‚TimeEvent’ are generated.</p>
- <pre class="prettyprint"><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
- /**
- * Interface for state machines which use timed event triggers.
- */
- public interface ITimedStatemachine {
- /**
- * Set the {@link ITimerService} for the state machine. It must be set
- * externally on a timed state machine before a run cycle can be correct
- * executed.
- *
- * @param timerService
- */
- public void setTimerService(ITimerService timerService);
- /**
- * Returns the currently used timer service.
- *
- * @return {@link ITimerService}
- */
- public ITimerService getTimerService();
- /**
- * Callback method if a {@link TimeEvent} occurred.
- *
- * @param timeEvent
- */
- public void onTimeEventRaised(TimeEvent timeEvent);
- }
- </code></pre>
- <p>
- <code>ITimedStatemachine</code> extends the generated statemachine to set an
- <code>ITimerService</code> and provides a callback method
- <code>onTimeEventRaised(TimeEvent timeEvent)</code> to let the timer service raise
- <code>TimeEvents</code>.
- </p>
- <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
- <code>ITimerService</code> interface has to be developed. Let’s have a look at the
- <code>ITimerService</code> interface:
- </p>
- <pre class="prettyprint"><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
- /**
- * Interface a timer service has to implement. Use to implement your own timer
- * service. A timer service has to be added to a timed state machine.
- *
- */
- public interface ITimerService {
- /**
- * Starts the timing for a given {@link TimeEvent}.
- *
- * @param event
- * : The TimeEvent the timer service should throw if timed out.
- * @param time
- * : Time in milliseconds after the given time event should be
- * triggered
- * @param cycleStartTime
- * : The absolute start time in milliseconds at which the last
- * run cycle was called. Can be used to produce a more accurate
- * timing behavior.
- */
- public void setTimer(TimeEvent event, long time, long cycleStartTime);
- /**
- * Unset the given {@link TimeEvent}. Use to unset cyclic repeated time
- * events.
- *
- * @param event
- */
- public void resetTimer(TimeEvent event);
- /**
- * Cancel timer service. Use this to end possible timing threads and free
- * memory resources.
- */
- public void cancel();
- /**
- * Returns the system time in milliseconds.
- *
- * @return the difference, measured in milliseconds, between the current
- * time and a defined point of time in the past.
- */
- public long getSystemTimeMillis();
- }
- </code></pre>
- <p>The
- <code>ITimerService</code> interface defines four methods. The
- <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 ‚setTimer’ 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.
- </p>
- <p>To raise a
- <code>TimeEvent</code> after the time is expired the method
- <code>onTimeEventRaised(TimeEvent timeEvent)</code> has to be called on the statemachine (should be a
- <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 ‚runCycle’ 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
- <code>runCycle</code> method with a time period of 1000 ms the event could not be processed in the correct time.
- </p>
- <p>The parameter ‚cycleStartTime’ 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>
- <p>The method
- <code>resetTimer(TimeEvent event)</code> is called by the statemachine to unset the timer.
- </p>
- <p>The last class used by timed statemachine is
- <code>TimeEvent</code>:
- </p>
- <pre class="prettyprint"><code>package org.yakindu.sct.examples.trafficlight.cyclebased;
- /**
- * Event that reflects a time event. It's internally used by
- * {@link ITimedStatemachine}.
- *
- * @author muehlbrandt
- *
- * @param <T>
- */
- public class TimeEvent {
- private boolean periodic;
- private ITimedStatemachine statemachine;
-
- int index;
- /**
- * Constructor for a time event.
- *
- * @param periodic
- * : Set to {@code true} if event should be repeated
- * periodically.
- *
- * @param index
- * : Index position within the state machine's timeEvent array.
- */
- public TimeEvent(boolean periodic, int index) {
- this.periodic = periodic;
- this.index = index;
- }
- /**
- * Returns the state machine reference of the event.
- *
- */
- public ITimedStatemachine getStatemachine() {
- return statemachine;
- }
- /**
- * Sets the state machine reference of the event.
- *
- * @param statemachine
- */
- public void setStatemachine(ITimedStatemachine statemachine) {
- this.statemachine = statemachine;
- }
- public boolean isPeriodic() {
- return periodic;
- }
- public int getIndex() {
- return index;
- }
- }
- </code></pre>
- <p>A
- <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.
- </p>
- <h4 id="DefaultimplementationofITimerService">Default implementation of ITimerService</h4>
- <p>The java code generator generates a default implementation of
- <code>ITimerService</code> interface if the TimerService feature is set to ‚true’ in the sgen model. This implementation is based on
- <code>java.util.Timer</code> and
- <code>java.util.TimerTask</code> and should be compatible with the normal Oracle VM or Open JDK VM:
- </p>
- <pre class="prettyprint"><code>
- package org.yakindu.sct.examples.trafficlight.cyclebased;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Timer;
- import java.util.TimerTask;
- /**
- * Default timer service implementation.
- *
- */
- public class TimerService implements ITimerService {
- private final Timer timer = new Timer();
- private final Map<TimeEvent, TimerTask> timerTaskMap = new HashMap<TimeEvent, TimerTask>();
- public void setTimer(final TimeEvent event, long time,
- long cycleStartTime) {
- // Reset existing TimerTask for event. This step isn't necessary if
- // timer tasks are properly reset by sexec model.
- if (timerTaskMap.containsKey(event)) {
- resetTimer(event);
- }
- // Create a new TimerTask for given event.
- timerTaskMap.put(event, new TimerTask() {
- @Override
- public void run() {
- event.getStatemachine().onTimeEventRaised(event);
- }
- });
- // start scheduling the timer
- if (event.isPeriodic()) {
- timer.scheduleAtFixedRate(timerTaskMap.get(event),
- time - (System.currentTimeMillis() - cycleStartTime), time);
- } else {
- timer.schedule(timerTaskMap.get(event),
- time - (System.currentTimeMillis() - cycleStartTime));
- }
- }
- public void resetTimer(TimeEvent event) {
- if (timerTaskMap.containsKey(event) && timerTaskMap.get(event) != null) {
- timerTaskMap.get(event).cancel();
- timer.purge();
- }
- timerTaskMap.remove(event);
- }
- public void cancel() {
- timer.cancel();
- timer.purge();
- }
- public long getSystemTimeMillis() {
- return System.currentTimeMillis();
- }
- }
- </code></pre>
- <h4 id="RuntimeService">Runtime service</h4>
- <p>The runtime service class can be used by client implementations to execute a run to completion step of a statemachine periodically.</p>
- <h4 id="InterVar">Interfaces, Variable and Event access</h4>
- <p>In a yakindu statechart variables and events are contained in interfaces. There could be one default interface defined (without a name) and several named interfaces. In the generated code these interfaces are defined as internal java interfaces of an interface that has the same name as the statemachine. Let’s have a look at following example statechart interface declaration:</p>
- <pre class="prettyprint"><code>interface:
- var a:boolean
- in event evA:boolean
- out event evB:integer
- </code></pre>
- <p>The generated interface code looks like following:</p>
- <pre class="prettyprint"><code>public interface IDefaultSMStatemachine extends IStatemachine {
- public interface SCInterface {
- public void raiseEvA(boolean value);
- public boolean isRaisedEvB();
- public int getEvBValue();
- public boolean getA();
- public void setA(boolean value);
- }
- public SCInterface getSCInterface();
- }
- </code></pre>
- <p>For the unnamed statechart interface a java interface
- <code>SCInterface</code> is generated. For the incoming event ‚evA’ a raise method with a boolean parameter is created because the event has a boolean type set. For the outgoing event ‚evB’ the methods
- <code>isRaisedEvB()</code> and
- <code>getEvBValue()</code> are generated. The first one can be used to determine if the event is raised by the statemachine and the second method serves to query the boolean value of the event. For variables getters and setters are generated too. Additionally the parent interface decribes getter methods to acquire each nested interface.
- </p>
- <p>The statemachine implements the parent interface by iteself and each nested interface is implemented as internal class. It holds an instance of each nested interface implementation which is accessible through a getter method. Have a look at the code snipped of the ‚unnamed’ default interface implementation:</p>
- <pre class="prettyprint"><code>public class DefaultSMStatemachine implements IDefaultSMStatemachine {
- private final class SCInterfaceImpl implements SCInterface {
-
- private boolean evA;
-
- private boolean evAValue;
-
- public void raiseEvA(boolean value) {
- evA = true;
- evAValue = value;
- }
-
- private boolean getEvAValue() {
- if (!evA)
- throw new IllegalStateException(
- "Illegal event value acces. Event EvA is not raised!");
- return evAValue;
- }
-
- private boolean evB;
-
- private int evBValue;
-
- public boolean isRaisedEvB() {
- return evB;
- }
-
- private void raiseEvB(int value) {
- evB = true;
- evBValue = value;
- }
-
- public int getEvBValue() {
- if (!evB)
- throw new IllegalStateException(
- "Illegal event value acces. Event EvB is not raised!");
- return evBValue;
- }
-
- private boolean a;
-
- public boolean getA() {
- return a;
- }
-
- public void setA(boolean value) {
- this.a = value;
- }
-
- public void clearEvents() {
- evA = false;
- }
-
- public void clearOutEvents() {
- evB = false;
- }
- }
-
- private SCInterfaceImpl sCInterface;
-
- public DefaultSMStatemachine() {
- sCInterface = new SCInterfaceImpl();
- }
-
- public SCInterface getSCInterface() {
- return sCInterface;
- }
- }
- </code></pre>
- <p>Now even a few notes on the naming of the generated interfaces and the value access of events. The default (unnamed) interface is named ‚SCInterface’. If an interface has a dedicated name then this name is used with the prefix ‚SCI’ (e.g. ‚SCIInterfacename’). Additionally a value of an event can only be accessed if the event is occured during the run to completion step. Otherwise an
- <code>IllegalStateException</code> is thrown by the interface.
- </p>
- <h4 id="InterfaceObservers">Interface observers</h4>
- <p>If the general feature ‚InterfaceObserverSupport’ is enabled in the sgen model the generated interfaces get support to register observers:</p>
- <pre class="prettyprint"><code>public interface IDefaultSMStatemachine extends IStatemachine {
- public interface SCInterface {
- public void raiseEvA(boolean value);
- public boolean isRaisedEvB();
- public int getEvBValue();
- public boolean getA();
- public void setA(boolean value);
- public List<SCInterfaceListener> getListeners();
- }
- public interface SCInterfaceListener {
- public void onEvBRaised(int value);
- }
- public SCInterface getSCInterface();
- }
- </code></pre>
- <p>An additional interface
- <code>SCInterfaceListener</code> is generated. This interface has to be implemented by the user and contains callback methods for each outgoing event. So the listener get notified if a outgoing event is raised by the statemachine internally. To add or remove a listener from an interface use the
- <code>getListeners()</code> method of the associated interface. This method returns a
- <code>java.util.list</code> which is parameterized with the appropriate listener type. The operations within the callback should not take a long process time because the statemachine execution may hang too long which leads to a not expected runtime behavior.
- </p>
- <h4 id="OperationCallback">Operation callbacks</h4>
- <p>The yakindu statecharts support the declaration of operations which can be used within a statechart as actions. These operations have to be implemented by the user before a statechart is executable. Here is an example statechart using an operation:</p>
- <p>
- <img border="0" src="images/OperationExample.png"/>
- </p>
- <p>Now have a look at the generated code:</p>
- <pre class="prettyprint"><code>public interface IDefaultSMStatemachine extends IStatemachine {
- public interface SCInterface {
- public void raiseEvA(boolean value);
- public boolean isRaisedEvB();
- public int getEvBValue();
- public boolean getA();
- public void setA(boolean value);
- public void setSCInterfaceOperationCallback(
- SCInterfaceOperationCallback operationCallback);
- }
- public interface SCInterfaceOperationCallback {
- public int myOperation(int param1, boolean param2);
- }
- public SCInterface getSCInterface();
- }
- </code></pre>
- <p>An additional interface
- <code>SCInterfaceOperationCallback</code> with the method
- <code>myOperation(int param1, boolean param2)</code> is generated. This interface has to be implemented by the user and set with the
- <code>setSCInterfaceOperationCallback(SCInterfaceOperationCallback operationCallback)</code> method so that the statechart can use it:
- </p>
- <pre class="prettyprint"><code>public static void main(String[] args) {
- DefaultSMStatemachine statemachine = new DefaultSMStatemachine();
-
- SCInterfaceOperationCallback callback = new SCInterfaceOperationCallback() {
-
- @Override
- public int myOperation(int param1, boolean param2) {
- // Your operation code should be placed here;
- return 0;
- }
- };
-
- statemachine.getSCInterface().setSCInterfaceOperationCallback(callback);
-
- statemachine.init();
- statemachine.enter();
- statemachine.runCycle();
- }
- </code></pre>
- <h4 id="IntegrationGeneratedCode">Integration of generated code</h4>
- <p>To get a clue how to integrate the generated java statemachines into your existing projects have a look at the
- <br/>
- <code>CrossingDemoCycleBased</code> class and it’s abstract super class
- <code>CrossingDemoBase</code>. The code starts with the main method in
- <code>CrossingDemoCycleBased</code> class:
- </p>
- <pre class="prettyprint"><code>public static void main(String[] args) {
- new CrossingDemoCycleBased().runTrafficLight();
- }
- </code></pre>
- <p>A new instance of the class is created and the method
- <code>runTrafficLight()</code> is directly called. This method consist in the super class:
- </p>
- <pre class="prettyprint"><code>public void runTrafficLight() {
- setUpAndRunStatemachine();
- createUIContent();
- shell.open();
- while (!shell.isDisposed()) {
- // update traffic lights
- readStatemachineOutput();
- crossing.repaint();
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- tearDownStatemachine();
- }
- </code></pre>
- <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
- <code>setUpAndRunStatemachine()</code>,
- <code>readStatemachinOutput</code> and
- <code>tearDownStatemachine()</code>:
- </p>
- <pre class="prettyprint"><code>protected void setUpAndRunStatemachine() {
- statemachine = new TrafficLightWaitingStatemachine();
- statemachine.setTimerService(new TimerService());
- statemachine.init();
- statemachine.enter();
-
- RuntimeService.getInstance().registerStatemachine(statemachine, 100);
- }
- </code></pre>
- <p>First a new instance of the generated statemachine is created. Because the traffic light statechart uses timing clauses the default implementation of the
- <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
- <code>runCycle()</code> method of the statemachine every 100 ms. So the statemachine executes a run to completion step every 100 ms.
- </p>
- <pre class="prettyprint"><code>protected void readStatemachineOutput() {
- trafficLightFigure.setRed(statemachine.getSCITrafficLight()
- .getRed());
- trafficLightFigure.setYellow(statemachine.getSCITrafficLight()
- .getYellow());
- trafficLightFigure.setGreen(statemachine.getSCITrafficLight()
- .getGreen());
- pedestrianLightFigure.setWhite(statemachine.getSCIPedestrian()
- .getRequest());
- pedestrianLightFigure.setRed(statemachine.getSCIPedestrian()
- .getRed());
- pedestrianLightFigure.setGreen(statemachine.getSCIPedestrian()
- .getGreen());
- }
- </code></pre>
- <p>The generated code contains getters and setters for each variable and event. So it’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
- <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
- <code>pedestrianRequestButtonClicked()</code> and
- <code>onOffButtonClicked()</code> some events are raised.
- </p>
- <p>Hint:
- <br/>If outgoing events are raised within the statemachine they remain active until the next run to completion step is started.
- </p>
- <pre class="prettyprint"><code>@Override
- protected void tearDownStatemachine() {
- // End TimerHandler and RuntimeService.
- statemachine.getTimerService().cancel();
- RuntimeService.getInstance().cancelTimer();
- }
- </code></pre>
- <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>
- <h3 id="SpecC">Specifications of C code</h3>
- <p>For C you can checkout the project ‚org.yakindu.examples.c.trafficlight’ from the Yakindu google code repository (
- <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 ‚Local C/C++ application’ from the eclipse ‚Run As’ context menu.
- </p>
- <h4 id="generatedHeader">Generated header files</h4>
- <p>The C code generator generates three header files. The first one is ‚sc_types.h’:</p>
- <pre class="prettyprint"><code>#ifndef SC_TYPES_H_
- #define SC_TYPES_H_
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdint.h>
- #include <stdbool.h>
-
- typedef int_fast16_t sc_short;
- typedef uint_fast16_t sc_ushort;
- typedef int32_t sc_integer;
- typedef uint32_t sc_uinteger;
- typedef double sc_real;
- typedef char* sc_string;
- typedef void* sc_eventid;
- #ifdef __cplusplus
- }
- #endif
- #define sc_boolean bool
- #define bool_true true
- #define bool_false false
- #endif /* SC_TYPES_H_ */
- </code></pre>
- <p>It contains some basic definitions for C++ compiler compatibility and typedefs to map the Yakindu statechart types to C types. The next header file is named like the statechart. For the traffic light example it is called ‚TrafficLightWaiting.h’:</p>
- <pre class="prettyprint"><code>#ifndef TRAFFICLIGHTWAITING_H_
- #define TRAFFICLIGHTWAITING_H_
- #include "sc_types.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*! \file Header of the state machine 'TrafficLightWaiting'.
- */
- //! enumeration of all states
- typedef enum {
- TrafficLightWaiting_main_region_on ,
- TrafficLightWaiting_main_region_on_r1_StreetGreen ,
- TrafficLightWaiting_main_region_on_r1_PedWaiting ,
- TrafficLightWaiting_main_region_on_r1_PedWaiting_r1_waitOn ,
- TrafficLightWaiting_main_region_on_r1_PedWaiting_r1_waitOff ,
- TrafficLightWaiting_main_region_on_r1_StreetAttention ,
- TrafficLightWaiting_main_region_on_r1_StreetRed ,
- TrafficLightWaiting_main_region_on_r1_PedestrianGreen ,
- TrafficLightWaiting_main_region_on_r1_PedestrianRed ,
- TrafficLightWaiting_main_region_on_r1_StreetPrepared ,
- TrafficLightWaiting_main_region_off ,
- TrafficLightWaiting_main_region_off_r1_YellowOn ,
- TrafficLightWaiting_main_region_off_r1_YellowOff ,
- TrafficLightWaiting_last_state
- } TrafficLightWaitingStates;
- //! Type definition of the data structure for the TrafficLightWaitingIfaceTrafficLight interface scope.
- typedef struct {
- sc_boolean red;
- sc_boolean yellow;
- sc_boolean green;
- } TrafficLightWaitingIfaceTrafficLight;
- //! Type definition of the data structure for the TrafficLightWaitingIfacePedestrian interface scope.
- typedef struct {
- sc_boolean request;
- sc_boolean red;
- sc_boolean green;
- } TrafficLightWaitingIfacePedestrian;
- //! Type definition of the data structure for the TrafficLightWaitingIface interface scope.
- typedef struct {
- sc_boolean pedestrianRequest_raised;
- sc_boolean onOff_raised;
- } TrafficLightWaitingIface;
- //! Type definition of the data structure for the TrafficLightWaitingTimeEvents interface scope.
- typedef struct {
- sc_boolean TrafficLightWaiting_main_region_on_r1_PedWaiting_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_PedWaiting_r1_waitOn_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_PedWaiting_r1_waitOff_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_StreetAttention_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_StreetRed_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_PedestrianGreen_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_PedestrianRed_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_on_r1_StreetPrepared_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_off_r1_YellowOn_time_event_0_raised;
- sc_boolean TrafficLightWaiting_main_region_off_r1_YellowOff_time_event_0_raised;
- } TrafficLightWaitingTimeEvents;
- //! the maximum number of orthogonal states defines the dimension of the state configuration vector.
- #define TRAFFICLIGHTWAITING_MAX_ORTHOGONAL_STATES 1
- /*! Type definition of the data structure for the TrafficLightWaiting state machine.
- This data structure has to be allocated by the client code. */
- typedef struct {
- TrafficLightWaitingStates stateConfVector[TRAFFICLIGHTWAITING_MAX_ORTHOGONAL_STATES];
- sc_ushort stateConfVectorPosition;
-
- TrafficLightWaitingIfaceTrafficLight ifaceTrafficLight;
- TrafficLightWaitingIfacePedestrian ifacePedestrian;
- TrafficLightWaitingIface iface;
- TrafficLightWaitingTimeEvents timeEvents;
- } TrafficLightWaiting;
- /*! Initializes the TrafficLightWaiting state machine data structures. Must be called before first usage.*/
- extern void trafficLightWaiting_init(TrafficLightWaiting* handle);
- /*! Activates the state machine */
- extern void trafficLightWaiting_enter(TrafficLightWaiting* handle);
- /*! Deactivates the state machine */
- extern void trafficLightWaiting_exit(TrafficLightWaiting* handle);
- /*! Performs a 'run to completion' step. */
- extern void trafficLightWaiting_runCycle(TrafficLightWaiting* handle);
- /*! Raises a time event. */
- extern void trafficLightWaiting_raiseTimeEvent(TrafficLightWaiting* handle, sc_eventid evid);
- /*! Gets the value of the variable 'red' that is defined in the interface scope 'TrafficLight'. */
- extern sc_boolean trafficLightWaitingIfaceTrafficLight_get_red(TrafficLightWaiting* handle);
- /*! Sets the value of the variable 'red' that is defined in the interface scope 'TrafficLight'. */
- extern void trafficLightWaitingIfaceTrafficLight_set_red(TrafficLightWaiting* handle, sc_boolean value);
- /*! Gets the value of the variable 'yellow' that is defined in the interface scope 'TrafficLight'. */
- extern sc_boolean trafficLightWaitingIfaceTrafficLight_get_yellow(TrafficLightWaiting* handle);
- /*! Sets the value of the variable 'yellow' that is defined in the interface scope 'TrafficLight'. */
- extern void trafficLightWaitingIfaceTrafficLight_set_yellow(TrafficLightWaiting* handle, sc_boolean value);
- /*! Gets the value of the variable 'green' that is defined in the interface scope 'TrafficLight'. */
- extern sc_boolean trafficLightWaitingIfaceTrafficLight_get_green(TrafficLightWaiting* handle);
- /*! Sets the value of the variable 'green' that is defined in the interface scope 'TrafficLight'. */
- extern void trafficLightWaitingIfaceTrafficLight_set_green(TrafficLightWaiting* handle, sc_boolean value);
- /*! Gets the value of the variable 'request' that is defined in the interface scope 'Pedestrian'. */
- extern sc_boolean trafficLightWaitingIfacePedestrian_get_request(TrafficLightWaiting* handle);
- /*! Sets the value of the variable 'request' that is defined in the interface scope 'Pedestrian'. */
- extern void trafficLightWaitingIfacePedestrian_set_request(TrafficLightWaiting* handle, sc_boolean value);
- /*! Gets the value of the variable 'red' that is defined in the interface scope 'Pedestrian'. */
- extern sc_boolean trafficLightWaitingIfacePedestrian_get_red(TrafficLightWaiting* handle);
- /*! Sets the value of the variable 'red' that is defined in the interface scope 'Pedestrian'. */
- extern void trafficLightWaitingIfacePedestrian_set_red(TrafficLightWaiting* handle, sc_boolean value);
- /*! Gets the value of the variable 'green' that is defined in the interface scope 'Pedestrian'. */
- extern sc_boolean trafficLightWaitingIfacePedestrian_get_green(TrafficLightWaiting* handle);
- /*! Sets the value of the variable 'green' that is defined in the interface scope 'Pedestrian'. */
- extern void trafficLightWaitingIfacePedestrian_set_green(TrafficLightWaiting* handle, sc_boolean value);
- /*! Raises the in event 'pedestrianRequest' that is defined in the default interface scope. */
- extern void trafficLightWaitingIface_raise_pedestrianRequest(TrafficLightWaiting* handle);
- /*! Raises the in event 'onOff' that is defined in the default interface scope. */
- extern void trafficLightWaitingIface_raise_onOff(TrafficLightWaiting* handle);
- /*! Checks if the specified state is active. */
- extern sc_boolean trafficLightWaiting_isActive(TrafficLightWaiting* handle, TrafficLightWaitingStates state);
- #ifdef __cplusplus
- }
- #endif
- #endif /* TRAFFICLIGHTWAITING_H_ */
- </code></pre>
- <p>Within this header file an enum that contains the state names is defined as well as the data structures for each interface a statechart has. Aditionally a structure for the time events the statechart uses is defined. The interface and time events data structures are nested in the parent structure
- <code>TrafficLightWaiting</code>. The client has to allocate this structure. In general it is a parameter of the most methods the statechart defines. In the following it is called statechart data structure.
- </p>
- <h5 id="Basestatechartmethods">Base statechart methods</h5>
- <p>The most basic methods the statechart defines are methods to init, enter, exit and to start a run to completion step. In the header file they got the statechart name as prefix and expect the parent data structure as parameter. For example the init method of the traffic light example is called
- <code>extern void trafficLightWaiting_init(TrafficLightWaiting* handle)</code>.
- </p>
- <p>The init method is used to initialize the statechart data structure. 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 enter method should be called if the statemachine is entered. It sets the statemachine into a defined state. The exit 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 exit and reentered via enter it continues working with this state. The run cycle 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:</p>
- <ul>
- <li>Clear list of outgoing events.</li>
- <li>Check whether events have occurred which lead to a state change</li>
- <li>If a state change has to be done:
- <ul>
- <li>Execute exit actions of leaving state.</li>
- <li>Save history state if necessary.</li>
- <li>Set the new State active.</li>
- <li>Execute entry action of the new state.</li>
- </ul>
- </li>
- <li>Clear list of incoming events.</li>
- </ul>
- <h5 id="Variableandeventaccess">Variable and event access</h5>
- <p>The getters and setters for each variable and event are contained in the header file too. The method names uses the pattern <statechart name>Iface<Interface name>_<set, get or raise>_<variable / event name>. For example the getter for the variable ‚red’ of the ‚pedestrian’ interface is named
- <code>trafficLightWaitingIfacePedestrian_get_red(TrafficLightWaiting* handle)</code>.
- </p>
- <h4 id="optionalHeader">Optional header file</h4>
- <p>If the statechart uses timing or external operations an additional header file with the name pattern <statechart name>Required.h is generated. This header file defines method hooks the client code has to implement externally. </p>
- <h5 id="Timingservicesupport">Timing service support</h5>
- <p>Because the traffic light example uses timing it contains such a header file:</p>
- <pre class="prettyprint"><code>extern "C" {
- #endif
- /*! \file This header defines prototypes for all functions that are required by the state machine implementation.
- This is a state machine uses time events which require access to a timing service. Thus the function prototypes:
- - trafficLightWaiting_setTimer and
- - trafficLightWaiting_unsetTimer
- are defined.
-
- These functions will be called during a 'run to completion step' (runCycle) of the statechart.
- There are some constraints that have to be considered for the implementation of these functions:
- - never call the statechart API functions from within these functions.
- - make sure that the execution time is as short as possible.
-
- */
- //
- // This is a timed state machine that requires timer services
- //
- //! This function has to set up timers for the time events that are required by the state machine.
- /*!
- This function will be called for each time event that is relevant for a state when a state will be entered.
- \param evid An unique identifier of the event.
- \time_ms The time in milli seconds
- \periodic Indicates the the time event must be raised periodically until the timer is unset
- */
- extern void trafficLightWaiting_setTimer(const sc_eventid evid, const sc_integer time_ms, const sc_boolean periodic);
- //! This function has to unset timers for the time events that are required by the state machine.
- /*!
- This function will be called for each time event taht is relevant for a state when a state will be left.
- \param evid An unique identifier of the event.
- */
- extern void trafficLightWaiting_unsetTimer(const sc_eventid evid);
- #ifdef __cplusplus
- }
- #endif
- #endif /* TRAFFICLIGHTWAITINGREQUIRED_H_ */
- </code></pre>
- <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 method to set a timer and a method to reset it has to be implemented externally and linked to the generated code. In case of our trafficlight example the method
- <code>trafficLightWaiting_setTimer(const sc_eventid evid, const sc_integer time_ms, const sc_boolean periodic)</code> is called by the statemachine to notify the timer service that it has to start a timer for the given time event identifier and raise it after the time given by the same named parameter is expired. It is important that within the ‚setTimer’ method only a timer thread or a hardware timer interrupt is started and no long time taking operations like sleeping threads or waiting are done. Never call the statechart API functions from within these functions. Otherwise the statemachine execution may hang within the timer service or it shows a not expected runtime behavior. The parameter ‚periodic’ is set to true if the time event should be raised periodically by the timer service.
- </p>
- <p>The method
- <code>trafficLightWaiting_unsetTimer(const sc_eventid evid)</code> is called by the statemachine to notify the timer service to reset the timer for the given event identifier.
- </p>
- <p>To notify the statemachine of the occurence of a time event the header of the statemachine defines a ‚raiseTimeEvent’ method. In case of the traffic light example it’s named
- <code>trafficLightWaiting_raiseTimeEvent(TrafficLightWaiting* handle, sc_eventid evid)</code> (in TrafficLightWaiting.h).
- </p>
- <h5 id="Operationcallbacks">Operation callbacks</h5>
- <p>The yakindu statecharts support the declaration of operations which can be used within a statechart as actions. These operations have to be implemented by the user before a statechart is executable. Here is an example statechart using an operation:</p>
- <p>
- <img border="0" src="images/OperationExample.png"/>
- </p>
- <p>Now have a look at the generated code:</p>
- <pre class="prettyprint"><code>#ifndef DEFAULTREQUIRED_H_
- #define DEFAULTREQUIRED_H_
- #include "sc_types.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*! \file This header defines prototypes for all functions that are required by the state machine implementation.
- This state machine makes use of operations declared in the state machines interface or internal scopes. Thus the function prototypes:
- - defaultIface_myOperation
- are defined.
-
- These functions will be called during a 'run to completion step' (runCycle) of the statechart.
- There are some constraints that have to be considered for the implementation of these functions:
- - never call the statechart API functions from within these functions.
- - make sure that the execution time is as short as possible.
-
- */
- extern sc_integer defaultIface_myOperation(const sc_integer param1, const sc_boolean param2);
- #ifdef __cplusplus
- }
- #endif
- #endif /* DEFAULTREQUIRED_H_ */
- </code></pre>
- <p>An additional method
- <code>sc_integer defaultIface_myOperation(const sc_integer param1, const sc_boolean param2)</code> is generated. This method has to be implemented by the user and linked with the generated code so that the statechart can use it.
- </p>
- <h4 id="Integrationofgeneratedcode">Integration of generated code</h4>
- <p>To get a clue how to integrate the generated c statemachines into your existing projects have a look at the
- <br/>
- <code>main.cpp</code> file. The code starts with the main method:
- </p>
- <pre class="prettyprint"><code>#include "org_yakindu_sct_examples_c_trafficlight.h"
- #include <QtGui>
- #include <QApplication>
- #include "src-gen/sc_types.h"
- #include "src-gen/TrafficLightWaiting.h"
- #include "statemachine/TrafficLightTimer.h"
- #include "statemachine/TrafficLightRunner.h"
- TrafficLightTimer *timer;
- int main(int argc, char *argv[]) {
- TrafficLightWaiting handle;
- trafficLightWaiting_init(&handle);
- timer = new TrafficLightTimer(&handle);
- trafficLightWaiting_enter(&handle);
- QApplication a(argc, argv);
- TrafficLightRunner *runner = new TrafficLightRunner(&handle, 100);
- org_yakindu_sct_examples_c_trafficlight w(0, runner);
- w.show();
- int ret = a.exec();
- return ret;
- }
- void trafficLightWaiting_setTimer(const sc_eventid evid,
- const sc_integer time_ms, const sc_boolean periodic) {
- timer->setTimer(evid, time_ms, periodic);
- }
- void trafficLightWaiting_unsetTimer(const sc_eventid evid) {
- timer->unsetTimer(evid);
- }
- </code></pre>
- <p>First an instance of the main statemchine data structure is created and intialized with the
- <code>trafficLightWaiting_init(&handle)</code> method. In the next step the Timer is instantiated. The class
- <code>TrafficLightTimer</code> represents an implementation of a timer service and uses the timer fuctionality of the Qt framework. The
- <code>TrafficLightRunner</code> solves as a run time service which executes every 100 ms a run to completion step of the statemachine. Within the class
- <code>org_yakindu_sct_examples_c_trafficlight</code> the runner class and the gui are wired:
- </p>
- <pre class="prettyprint"><code>#include "org_yakindu_sct_examples_c_trafficlight.h"
- org_yakindu_sct_examples_c_trafficlight::org_yakindu_sct_examples_c_trafficlight(
- QWidget *parent, TrafficLightRunner *runner) :
- QMainWindow(parent) {
- ui.setupUi(this);
- crossing = new CrossingWidget(this);
- trafficLight = new TrafficLightWidget(crossing);
- trafficLight->setGeometry(275, 75, 30, 90);
- pedestrianLight = new PedestrianLightWidget(crossing);
- pedestrianLight->setGeometry(50, 10, 70, 20);
- connect(runner, SIGNAL(cycleDone(TrafficLightWaiting*)), this, SLOT(update(TrafficLightWaiting*)));
- pedestrianReq = new QPushButton("pedestrian request", this);
- pedestrianReq->setGeometry(1, 365, 150, 30);
- connect(pedestrianReq, SIGNAL(released()), runner, SLOT(raisePedestrianRequest()));
- off = new QPushButton("off / on", this);
- off->setGeometry(249, 365, 150, 30);
- connect(off, SIGNAL(released()), runner, SLOT(raiseOnOff()));
- }
- void org_yakindu_sct_examples_c_trafficlight::update(
- TrafficLightWaiting *handle) {
- trafficLight->setSignals(handle->ifaceTrafficLight.red,
- handle->ifaceTrafficLight.yellow, handle->ifaceTrafficLight.green);
- pedestrianLight->setSignals(handle->ifacePedestrian.request,
- handle->ifacePedestrian.red, handle->ifacePedestrian.green);
- QMainWindow::update();
- }
- org_yakindu_sct_examples_c_trafficlight::~org_yakindu_sct_examples_c_trafficlight() {
- }
- </code></pre>
- </body>
- </html>
|