We’re here to help you

Menu

Specifying Behaviour

A behaviour section is used to define the actions an interface or component will perform based on the events received.

Interface versus Component behaviour

Behaviour in an interface is a specification. It is an agreement between components providing and requiring this interface about the messages and protocol exchanged. Both components must adhere to this specification. Interface conformance is an important check in the verifier. The code generator does not generate any code based on the behaviour specification of an interface.

Behaviour in a component is a definition. It defines the actual behaviour expressed by the object modelled. Whereas an interface might still allow some freedom, sometimes multiple messages are allowed, inside a component the behaviour must be fully deterministic. The code generator uses this component behaviour specification to generate target code.

Syntax

The 'behaviour' keyword is used to declare a behaviour section. Optionally it may have a name.

behaviour [OptionalName]
{
  behaviour-body
}

The behaviour body contains a collection of statements that define the actions that will be performed based on events received.

The following elements can be used in the behaviour body:

Examples

A basic behaviour section

This example shows an interface with a basic behaviour section.

interface IMotion
{
  in void start();
  in void stop();

  behaviour
  {
      bool started = false;

      on start: started = true;
      on stop: started = false;
  }
}

A behaviour section with a protocol definition

This example shows an interface with a more complex behaviour section, where the start event is only allowed in case the interface is in state Off and the stop event is only allowed in case the interface is in state Active.

interface IMotion
{
  in void start();
  in void stop();

  behaviour
  {
      enum State {Off, Active};
      State state = State.Off;

      [state.Off]
      {
          on start: state = State.Active;
          on stop:  illegal;
      }
      [state.Active]
      {
          on start: illegal;
          on stop:  state = State.Off;
      }
   }
}

A behaviour section with a protocol definition, alternative form

This example shows an interface with the same behaviour as the one above, but an alternative form is used to define the behaviour.

In the previous example based on the state value (using guarded statements) the actions for the events per state were defined. In the alternative below based on the events the actions in each state are defined.

interface IMotion
{
  in void start();
  in void stop();

  behaviour
  {
      enum State {Off, Active};
      State state = State.Off;

      on start:
      {
          [state.Off] state = State.Active;
          [state.Active] illegal;
      }
      on stop:
      {
          [state.Off] illegal;
          [state.Active]  state = State.Off;
      }
   }
}

If you have questions that weren’t answered by this Guide,
let our support team help you out.

Enjoy this article? Don't forget to share.