h1. YAKINDU SCT 2 Reference

h2. State chart elements

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.

The YAKINDU SCT 2 meta model is designed similar to the UML state chart meta model with the following differences

* they are self contained with interfaces defined by events and variables
* core execution semantics are cycle driven, not event driven
** this allows to process concurrent events
** event driven behavior can be defined on top
* time is an abstract concept for state charts
* time control is delegated to the environment

The model interpreter and different flavors of generated code follow these same core semantics.

Please refer to the description of the "UML Statecharts":http://en.wikipedia.org/wiki/UML_state_machine for more details.

h3. Regions

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.  

!images/parallelRegions.jpg!

h3. States

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.   

h3. Transitions

A transition is the transfer of one state to another. Transitions are diagrammed as arrows and can carry events and actions but must not. 

The syntax of events and actions is defined by a textual description language (#Statechartdescriptionlanguage). Please refer to the documentation section "Events":#Events for more details. For more details on Actions refer to the chapter "Actions":#ReactionTriggers.

If a state has more than one outgoing transition without event that transition is carried out first that was modeled first.

h3. Initial state and final state

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. 

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.

Inside a region only one initial state is allowed, but every region can have an initial state.

h3. Choice

Choice is also a pseudo state. It can be used to model a conditional path. Choice nodes divide a transition into multiple parts. 

Usually the first transition points towards the choice node. One of the choice outgoing transitions can carry a condition. 

h3. Junction

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.

h3. Composite State

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.

Composite states contain other state machine branches.

h4. Orthogonal states

In the context of state machines orthogonal states are states that are independent from each other. The most famous example is the keyboard example:

!images/orthogonalState_example.jpg!

h4. Submachine states

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.

!images/substatemachine_example.jpg!

h3. Shallow History

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:

!images/shallowHistory01.jpg!

The interesting parts in this state chart are the events _checkProgress_ and _goon_. CheckProgress jumps back to the init state while assigning the current progress count to the variable _temp_. _goon_ jumps to the shallow history state that was placed inside the composite state.

!images/shallowHistory02.jpg!

!images/shallowHistory03.jpg!

On triggering the _goon_ event the last active state is activated again.

h3. Deep History

Deep history is similar to shallow history but more complex. With a deep history the latest state of multiple nested states is remembered.

h2. Statechart description language

The textual description language is used to declare and describe behaviors in the state machine. It is case sensitive.

h3. Typesystem

The language has an integrated small typesystem with the following simple types:

* integer
* real
* boolean
* string
* void

So events and variables can be declared with types:

bc.. 
var intVar : integer
var realVar : real
var boolVar : boolean
var stringVar : string
var voidVar : void
event addInt : integer
event checkValidity : boolean
 

h3. Expressions

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.

Logical expressions are similar to other programming languages. The return type is *boolean*. In the following there are some examples of these:

h4. Logical AND

bc.. 
var1 && var2
h4. Logical OR

bc.. 
var1 ||  var2 
h4. Logical NOT

bc.. 
!var1 
h4. Conditional expression

bc.. 
var1 ? var2 : 23 
h4. Bitwise XOR

bc.. 
var1 ^ var2
h4. Bitwise OR

bc.. 
var1 | var2
h4. Bitwise AND

bc.. 
var1 & var2
h4. Logical Relations and Shift Operators

|less than | < |
|equal or less than | <= |
|greater than | > |
|equal or greater than | >= |
|equal | == |
|not equal | != |
|shift left | << |
|shift right | >> |


h4. Binary arithmetic operators

|plus | + |
|minus | - |
|multiply | * |
|divide | / |
|modulo | % |

h4. Unary arithmetic operators

|positive | + |
|negative | - |
|circa	| ~ |


h3. Statements

A statements can be either an assignment, raising an event or call an operation. The language has the following assignment operators:

* simple assignment:  = 
* multiply and assign: *= 
* divide and assign: /= 
* calculate modulo and assign: %= 
* add and assign: += 
* subtract and assign: -= 
* bitshift left and assign: <<= 
* bitshift right and assign: >>= 
* bitwise AND and assign: &= 
* bitwise XOR and assign: ^= 
* bitwise OR and assign: @|=@ 

An event is raised by the keyword _raise_ followed by the event name and if it is an interface event the name of the interface.

An operation is called similar to other programming languages with the operation name and passing concrete parameters. The parameters can be expressions.


h3. Scopes

==<!-- Start stext_keyword_namespace -->==

h4. Namespace

The language allows to define unique namespaces, which can be used to qualify references to the statechart.

bc.. 
namespace trafficlights

p. ==<!-- End stext_keyword_namespace -->==
==<!-- Start stext_keyword_interface -->==

h4. interface scope

Declarations in the interface scope are externally visible. They can be shared within the environment.


bc.. 
interface NamedInterface:
in event event1
out event event3
var variable1 : real
entrypoint entry1
exitpoint exit1

p. ==<!-- End stext_keyword_interface -->==
==<!-- Start stext_keyword_internal -->==

h4. internal scope

Declarations made in an internal scope are only visible for contained states.

bc.. 
internal:
var localVariable1: integer
event localEvent: integer
local event localEvent2: NamedInterface.event1 || localEvent
local event localEvent3: localEvent || localEvent2 : 25
operation localOperation (integer, integer): integer
localEvent3 / raise NamedInterface.event3 :
localOperation(valueOf(localEvent),NamedInterface.variable1);

p. ==<!-- End stext_keyword_internal -->==

h3. Declarations

Within scopes there can be declarations of Events, Variables, Operations, LocalReactions, EntryPoints and ExitPoints.

==<!-- Start stext_keyword_event -->==

h3. Events

Within interface scope events have an direction. They can either be ingoing or outgoing:

bc.. 
interface NamedInterface:
in event event1
out event event2
p. Within local scope events can carry variables:

bc.. 
internal:
event localEvent1 : integer
p. Local events can be derived from interface events or other local events and can have a value assignment:

bc.. 
internal:
event localEvent1: integer
local event localEvent2 = NamedInterface.event1 || localEvent1
local event localEvent3 = localEvent2 || 25

p. ==<!-- End stext_keyword_event -->==

==<!-- Start stext_keyword_var -->==

h3. Variables

Variables can have different visibilities. They can be visible for the environment:

bc.. 
var variable1: real
p. Variables can be *readonly* (constants):

bc.. 
var readonly pi: real = 3.1415
p. Variables can be referenced by the environment.

bc.. 
var external variable3: integer = 34

p. ==<!-- End stext_keyword_var -->==

h3. Reaction Triggers

Actions are key constructs in state machines to model behavior. The YAKINDU SCT 2 knows the following kinds of actions.



==<!-- Start stext_keyword_after -->==

h4. after

The _after_ trigger specifies one-shot time events. 

After the specified time the reaction is triggered. An _after_ 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. 

bc. after 20 s

Structure: 

@after@ _@time@_ (_@unit@_)? 

The time unit can be:

* ==<!-- Start stext_keyword_s -->== s - seconds ==<!-- End stext_keyword_s -->==
* ==<!-- Start stext_keyword_ms -->== ms - milliseconds ==<!-- End stext_keyword_ms -->==
* ==<!-- Start stext_keyword_us -->== us - microseconds ==<!-- End stext_keyword_us -->==
* ==<!-- Start stext_keyword_ns -->== ns - nanoseconds ==<!-- End stext_keyword_ns -->==
* empty - implies seconds

p. ==<!-- End stext_keyword_after -->==
==<!-- Start stext_keyword_every -->==

h4. every

The _every_ trigger specifies periodic time events. 

The reaction is triggered periodically after the specified time. An _every_ 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. 

bc. every 200 ms

Structure: 

@every@ _@time@_ (_@unit@_)? 

The time unit can be:

* s - seconds
* ms - milliseconds
* us - microseconds
* ns - nanoseconds
* empty - implies seconds

==<!-- End stext_keyword_every -->==
==<!-- Start stext_keyword_always -->==

h4. always

This trigger is always true and enables a reaction to be executed in every run to completion step (RTS). It is equivalent to _oncycle_.

==<!-- End stext_keyword_always -->==

==<!-- Start stext_keyword_default -->==
==<!-- Start stext_keyword_else -->==


h4. default, else

The _default_ trigger is equivalent to the _else_ trigger. It is intended for use for the outgoing transitions of _choice_ 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. 

==<!-- End stext_keyword_else -->==
==<!-- End stext_keyword_default -->==


==<!-- Start stext_keyword_entry -->==

h4. entry

An _entry_ trigger marks actions that are carried out on entering a state or state machine.

==<!-- End stext_keyword_entry -->==
==<!-- Start stext_keyword_exit -->==

h4. exit

An _exit_ trigger marks actions that are carried out on exiting a state or state machine.

==<!-- End stext_keyword_exit -->==
==<!-- Start stext_keyword_oncycle -->==

h4. oncycle

The _oncycle_ trigger is always true and enables a reaction to be executed in every run to completion step (RTS). It is equivalent to _always_.

==<!-- End stext_keyword_oncycle -->==
==<!-- Start stext_keyword_operation -->==

h3. Operations

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.

bc.. 
operation localOperation (integer, integer):integer
localEvent3/ raise NamedInterface3.event1

p. ==<!-- End stext_keyword_operation -->==

h3. LocalReactions

Local reactions describe the internal behavior of a state. So they have internal scope. A local reaction is  declared as follows:

bc.. 
LocalReaction: ReactionTrigger '/' ReactionEffect ('#' ReactionProperties)?

ReactionTrigger: (Event ("," Event	)* 	(=> '[' Expression ']')?) | '[' Expression ']'					

ReactionEffect:  Statement (';' Statement )* (';')?

Statement: Assignment | EventRaising | OperationCall

ReactionProperties: (EntryPoint | ExitPoint)*
p. Within a local reaction an interface event can be raised:

bc.. 
internal:
localEvent1 / raise NamedInterface.event3 : localOperation (valueOf(localEvent), NamedInterface.variable1);
p. Local reactions can have priority values. These are defined by a following # and the integer number of priority:

bc.. 
localEvent2 / NamedInterface.variable2 += 3; #1
localEvent3 / NamedInterface.variable4 += 2.0; #2

p. ==<!-- Start stext_keyword_entrypoint -->==

h3. EntryPoints

Every state chart has an entry point. An entry point can be declared like the following:

bc.. 
entrypoint entry1

p. ==<!-- End stext_keyword_entrypoint -->==
==<!-- Start stext_keyword_exitpoint -->==

h3. ExitPoints

Every state chart has an exit point. This exit point can be declared like the following.

bc.. 
exitpoint exit1
p. ==<!-- End stext_keyword_exitpoint -->==

h2. SGraph

SGraph is the metamodel for the graphical part of the statechart editor. It owns all core elements of a state machine like states, pseudostates, transitons etc. but it describes how these elements shall be shown by the editor.

h2. SExec

SExec is the name of an intermediate execution model. This intermediate model is used behind the scenes as a foundation for the code generators and the simulation engine. This guarantees that the simulation behaves in the same way as the generated Statechart implementations. It captures the execution behavior and may also serve as the basis of custom code generators.

h2. SGen

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. 

To get started with the generator model, we included a new Eclipse wizard that creates a basic configuration file with default values.

!images/sGenEditor.png!

The generator model is associated with the builder. If *Project* > *Build Automatically* is checked the files are generated. In the following the specific customizing features of the generator models are explained.

h3. Generator model for Java

The generator model for Java is used to customize the generation of Java code from the state chart model. 

h4. Feature Outlet

With the feature *Outlet* you define the folder the source files will be generated in:

bc.. 
		feature Outlet {
			targetProject = "org.terra.coffee.machine"
			targetFolder = "src-gen"			
		}

h4. Feature LicenseHeader

With the feature Licence header you can set a licence text that is added to the headers of all generated files:

bc.. 
		feature licenseHeader {
			licenseText = "Copyright (c) 2012 itemis AG.
 All rights reserved."			
		}


h4. Feature Debug

The feature debug controls the output of debug information. An important information source is the intermediate model sExec.

bc.. 
		feature Debug {
			dumpSexec = false			
		}

h4. Feature FunctionInlining

bc.. 
		feature FunctionInlining {
			inlineChoices=true
			inlineEnterRegion=true
			inlineEnterSequences=true
			inlineEntries=true
			inlineEntryActions=true
			inlineExitActions=true
			inlineExitRegion=true
			inlineExitSequences=true
			inlineReactions=true
		}

h4. Feature GeneralFeatures

bc.. 
		feature GeneralFeatures {
			EventBasedStatemachine=true
			GenericInterfaceSupport=true
			InterfaceObserverSupport=true
			RuntimeService=false
			StatemachineFactorySupport=true
			TimerService=true
		}

h4. Feature Naming


bc.. 
		feature Naming {
			basePackage="org.java"
			implementationSuffix="ipl"
		}
h3. Generator model for C

h4. Feature Outlet

The feature is similar to the target language Java.

h4. Feature LicenseHeader

The feature is similar to the target language Java.

h4. Feature Debug

The feature is similar to the target language Java.

h4. Feature CCodeFeature

bc.. 
		feature CCodeFeature {
			DebugType="DEBUG"
			InterfaceEventListener=false
			Singleton=true
		}

h4. Feature FunctionInlining

h3. Generator model for C++

h4. Feature Outlet

The feature is similar to the target language Java.

h4. Feature LicenseHeader

The feature is similar to the target language Java.

h4. Feature Debug

The feature is similar to the target language Java.

h4. Feature FunctionInlining



