We’re here to help you

Menu

Using Local Variables

Variables can be defined and used.

Syntax

Variables are named, have a type, and an initial value, which is an expression:

variableType variableName = initialValue;

Supported types are:

  • enum: enumerated type.

  • bool: boolean type (true, false)

  • subint: integer type.

To ensure that an integer variable falls within a finite set (in order to have a limited range of possible paths / states in the system), use a type definition, e.g. subint myInt {0..10}.

Examples

Simple state machine where the state is captured in a variable of enum type

This example shows a simple state machine for a siren where the state is captured in an enumeration.

interface Siren
{
  in void turnOn();
  in void turnOff();

  behaviour
  {
    enum State { SirenOff, SirenOn }; // type declaration
    State state = State.SirenOff;     // variable declaration

    [state.SirenOff] // if the Siren is off, only allow to turn it on
    {
      on turnOn:  state = State.SirenOn;
      on turnOff: illegal;
    }
    [state.SirenOn] // if the Siren is on, only allow to turn it off
    {
      on turnOff: state = State.SirenOff;
      on turnOn:  illegal;
    }
  }
}

Simple state machine where the state is captured in a boolean type

This example shows a simple state machine for a siren where the state is captured in a variable of boolean type

interface Siren
{
  in void turnOn();
  in void turnOff();

  behaviour
  {
    bool SirenOn = false;

    [SirenOn] // if the Siren is on, only allow to turn it off
    {
      on turnOff: SirenOn = false;
      on turnOn:  illegal;
    }
    [otherwise]
    {
      on turnOn:  SirenOn = true;
      on turnOff: illegal;
    }
  }
}

Limited retrying activation of a device using a variable of integer type

This example shows how a counter based on an integer type can be used to limit retrying activation of a device.

component AlarmSystemComp
{
  provides AlarmSystem alarmSystem;
  requires Sensor sensor;
  requires Siren siren;

  behaviour
  {
    enum State
    {
      NotActivated,
      Activated_Idle,
      Activated_AlarmMode,
      Deactivating
    };
    subint myInt {0..10};                 // myInt is defined as integer within the range 0-10
    myInt counter = 0;                    // myInt is initialised with value 0.
    State state = State.NotActivated;

    void activating(Sensor.Values value)  // recursive function to activate the sensor, it will retry 10 times.
    {
      if (value == Sensor.Values.OK)
      {
        reply(AlarmSystem.Values.Ok);
        state = State.Activated_Idle;
      }
      else
      {
        if (counter < 10)
        {
          counter = counter + 1;
          Sensor.Values val = Sensor.Activate();
          activating(val);                 // try again
        }
        else
        {
          reply(AlarmSystem.Values.Failed);
        }
      }
    }

    [state.NotActivated]
    {
      on alarmSystem.SwitchOn():
      {
        Sensor.Values val = Sensor.Activate();
        activating(val);                    // call the activation function
      }
      on alarmSystem.SwitchOff(), sensor.DetectedMovement(), sensor.Deactivated():
      {
        illegal;
      }
    }
    /* ... */
    /* ... */
  }
}

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.
google-site-verification:google656c7703ab521151.html