Next: , Previous: , Up: Specifying Behaviour   [Contents][Index]


3.6.2 Behaviour 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.6.2.1 Examples

3.6.2.2 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.6.2.3 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.6.2.4 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:


Next: , Previous: , Up: Specifying Behaviour   [Contents][Index]