Code segments

Simulation with code

Each transition may have an attached code segment which contains ML code. Code segments are executed when their parent transition occurs.

Adding/editing code segments

Code segments are created by adding/editing an inscription for a transition.

Before a code segment has been added, the default text for the inscription is:

input ();
output ();
action
();

Adding code segment

Characteristics and syntax

Code segments may use CPN variables and may bind CPN variables located on output arcs that are not bound elsewhere.

Each code segment may contain:

  • Input pattern (optional)
  • Output pattern (optional)
  • Code action (mandatory)

Input pattern

An input pattern is a tuple of CPN variables, preceded by the keyword input. The input pattern lists the CPN variables that can be used in the code action. The code action can use the values of these CPN variables but it cannot change them.

The CPN variables listed in the input pattern can be used in the code action even if you have declared an ML identifier with the same name in the declaration node.

If the input clause is omitted, it implies that no CPN variables can be used in the code action.

Output pattern

An output pattern is a tuple of CPN variables, preceded by the keyword output. The output pattern lists the CPN variables to be changed as a result of the execution of the code action.

An output pattern must be a CPN variable or a tuple of CPN variables without repetitions.

If the output clause is omitted, it implies that no CPN variables are calculated.

Code actions

A code action is an ML expression, preceded by the keyword action. The code action cannot contain any declaration of color sets, CPN variables, or reference variables. It can, however, apply user-declared and predeclared constants, operations, and functions. In addition, new functions and constants can be defined for local use by means of let-in-end.

The code action is executed as a local declaration in an environment containing the CPN variables specified in the input pattern. This guarantees that the code action cannot directly change any CPN variables but only local copies of them.

When the code action has been executed, its result is applied to bind the CPN variables in the output pattern. The code action, when evaluated in an environment containing the input pattern variables must yield a result of the same type as the output pattern. If no output pattern is given, its type is assumed to be unit.

Example

In this example, there is one variable in the input pattern (m), and two variables in the output pattern (n,b).

Example code segment

When the transition Even occurs in the given marking, the CPN variables will be bound as follows:

  • m is bound to 3,
  • n is bound to 4,
  • b is bound to false.

The I/O version of the example net for the Simple protocol example also uses code segments.

Related pages