Previous: , Up: Creating a Component   [Contents][Index]


3.3.6 Behaviour

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

3.3.6.1 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 imprtant 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 modeled. 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.

3.3.6.2 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:

3.3.6.3 Examples

3.3.6.4 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;
  }
}

3.3.6.5 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;
      }
   }
}

3.3.6.6 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;
      }
   }
}

See also:


Previous: , Up: Creating a Component   [Contents][Index]