Guideline: Rhapsody Action Language
Brief introduction to Rhapsody's action language.

Basic Syntax 

The language is case sensitive. That is, “evmove” is different from “evMove”.

Each statement must end with a semi-colon.

All names must start with a letter and cannot contain spaces. Special characters are not permitted in names, except for underscores (_). However, a name should never start with an underscore.

The following words are reserved and should not be used for names: asm, auto, break, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, GEN, goto, id, if, inline, int, IS_IN, IS_PORT, long, new, operator, OPORT, OUT_PORT, params, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, virtual, void, volatile, while.  

Assignment and Arithmetic Operators 

X=1;
(Sets X equal to 1)
X=Y;
(Sets X equal to Y)
X=X+5;
(Adds 5 to X)
X=X-3;
(Subtracts 3 from X)
X=X*4;
(Multiplies X by 4)
X=X/2;
(Divides X by 2)
X=X%5;
(Sets X to the remainder of X divided by 5)
X++;
(Increments X by 1)
X--;
(Decrements X by 1)
 

Printing

The “cout” operator prints to the screen. Elements to be printed are separated by the “<<” operator. Text strings are surrounded by double quotes. Attributes are referenced using their names. The “endl” operator prints a carriage return. So, to print out the current value of X, use the following command:

cout << “The value of X is “ << X << endl;

If the current value of X is 5, this statement prints the following message on the screen:

The value of X is 5 


Comparison Operators 

X==5
(X equal to 5)
X!=5
(X not equal to 5)
X<3
(X less than 3)
X<=3
(X less than or equal to 3)
X>4
(X greater than 4)
X>=4
(X greater than or equal to 4)
X>2 && X<7
(X greater than 2 and X less than 7
X<2 || X==7
(X less than 2 or X equal to 7)
 

Conditional Statements

Conditional statements begin with the keyword “if” followed by a conditional expression in parenthesis, followed by the statement to execute if the condition evaluates to true. You can optionally add the “else” keyword to execute a statement if the condition evaluates to false. The “else” clause can contain another nested “if” statement as well. For example:

if (X<=10)

X++;

else

X=0;

Multiple statements can be grouped together by placing them in curly braces.

if (X<=10)

{

X++;

cout << “The value of X is ” << X << endl;

}

else

{

X=0;

cout << “Finished” << endl;

} 


Incremental Looping Statements

Incremental looping is accomplished using the “for” statement. It holds three sections separated by semicolons to specify: 1) an initialization statement, 2) a conditional expression, and 3) an increment statement. For example, to iteratively set the value of X from 0 to 10 while printing out its value:

for (X=0; X<=10; X++)

cout << X << endl; 


Conditional Looping Statements

The “while” statement is used for conditional looping. This statement has a single conditional expression and iterates so long as it evaluates to true. The previous example could be implemented using a “while” statement as follows:

X=0;

while(X<=10)

{

cout << X << endl;

X++;


Invoking Operations

To invoke an operation on a block, use the operation name followed by parenthesis. For example, to invoke the “go” operation:

go();

If an operation takes parameters, place them in a comma-separated list. For example, to invoke the “min” operation with two parameters:

min(X,Y); 


Generating Events

The “OUT_PORT” and “GEN” keywords are used to generate events through ports. For example, to send an event named “evStart” out the port named “p2”, issue the following statement:

OUT_PORT(p2)->GEN(evStart);

To generate an event with parameters, place them into a comma-separated list. For example, to generate an event named “evMove” with two parameters for velocity and direction:

OUT_PORT(p2)->GEN(evMove(10,2));

NOTE: The “OPORT” keyword can be used in place of “OUT_PORT”. 


Referring to Event Parameters in Transitions

The “params” keyword followed by the “->” operator is used to reference the parameters of the event that caused the current transition. For example, if an event named “evMove” has a parameter named “velocity”, that parameter can be referenced using “params->velocity”. This syntax can also be embedded in statements within the action on the transition. For example,

if (params->velocity <= 5)… 


Testing the Port on which an Event Arrives

The “IS_PORT” keyword is used to test whether the event that caused the current transition arrived through a specific port. For example:

if (IS_PORT(p2))… 


Testing the State of a State Machine

The “IS_IN” keyword is used to test whether a state machine is in a specific state. For example, to test whether the state machine of a block is in a state called “Accelerating”:

if (IS_IN(Accelerating))…