Previous: Variable Syntax, Up: Using Local Variables [Contents][Index]
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; } } }
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; } } }
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; } } /* ... */ /* ... */ } }
Previous: Variable Syntax, Up: Using Local Variables [Contents][Index]