Main Content

Eliminate Redundant Code by Combining State Actions

You can combine entry, during, and exit actions that execute the same tasks in a state.

By combining state actions that execute the same tasks, you eliminate redundant code. For example:

Separate ActionsEquivalent Combined Actions

entry:
  y = 0;
  y=y+1;
during: y=y+1;

entry: y = 0;
entry, during: y=y+1;

en: 
  fcn1();
  fcn2();
du: fcn1();
ex: fcn1();

en, du, ex: fcn1();
en: fcn2();

Combining state actions this way produces the same chart execution behavior (semantics) and generates the same code as the equivalent separate actions.

How to Combine State Actions

Combine a set of entry, during, and/or exit actions that perform the same task as a comma-separated list in a state. Here is the syntax:

entry, during, exit: task1; task2;...taskN;

You can also use the equivalent abbreviations:

en, du, ex: task1; task2;...taskN;

Valid Combinations

You can use any combination of the three actions. For example, the following combinations are valid:

  • en, du:

  • en, ex:

  • du, ex:

  • en, du, ex:

You can combine actions in any order in the comma-separated list. For example, en, du: gives the same result as du, en:.

Invalid Combinations

You cannot combine two or more actions of the same type. For example, the following combinations are invalid:

  • en, en:

  • ex, en, ex:

  • du, du, ex:

If you combine multiple actions of the same type, you receive a warning that the chart executes the action only once.

Order of Execution of Combined Actions

States execute combined actions in the same order as they execute separate actions:

  1. Entry actions first, from top to bottom in the order they appear in the state

  2. During actions second, from top to bottom

  3. Exit actions last, from top to bottom

The order in which you combine actions does not affect state execution behavior. For example:

Combined ActionsOrder of Execution

Entry action y = 0 followed by combined entry and during action y = y+1.

  1. en: y = 0;

  2. en: y = y+1;

  3. du: y = y+1;

Combined entry and during action y = y+1 followed by entry action y = 0.

  1. en: y = y+1;

  2. en: y = 0;

  3. du: y = y+1;

Combined during and entry action y = y+1 followed by entry action y = 0.

  1. en: y = y+1;

  2. en: y = 0;

  3. du: y = y+1;

Combined during and entry action y = y+1 followed by combined entry and exit action y = 10.

  1. en: y = y+1;

  2. en: y = 10;

  3. du: y = y+1;

  4. ex: y = 10;

Rules for Combining State Actions

  • Do not combine multiple actions of the same type.

  • Do not create data, events, or messages that have the same name as the action keywords: entry, en, during, du, exit, ex.

Related Topics