### Numeric Modes Example

Dynamic task activations and the execution of different control paths can depend on internal system states, external events and scheduling effects resulting in different execution sequences of tasks. In APP4MC modes can be used to steer different execution paths. The designer can define modes that execute different paths within runnables, tasks, and even control dynamic activation schemes of tasks with multiple dependencies. This page explains the concepts with a simple example. For the description of the basic model elements, we refer the reader to the chapters:

Data Models -> Software Model -> Modes
Data Models -> Software Model -> Runnable Items -> Runnable Mode Switch
Data Models -> Software Model -> The Call Graph -> Mode Switch
Data Models -> Stimuli Model -> Mode Value List and Execution Condition

#### Example description

In this chapter, we show a small example how to use modes to steer the execution and the activation of tasks with modes. The following picture depicts the example:

In this example, we have three modes, one _Enum__ and two Numeric. The numeric modes are initialized with the value -1:

First, we have two data generators with Task genX and Task genY where each execution increases a numeric mode by 1. They are activated by a relative sporadic stimulus with an occurrence interval between 10ms and 50ms. The runnable counterForX executes this for genX:

On every execution, the generator tasks trigger the CustomEvent check to evaluate the EventStimulus. This event stimulus checks the condition:
executionMode == ModeA && countX > 2 && countY > 4
In this example, we do not describe the change of the executionMode to keep the model simpler. This mode could be changed e.g. from within the system or by an external ISR trigger.

If this condition is true, both counting modes are set to zero and the Task doA is called.

Another way to steer the execution depending can be done via mode switches on task level. In doB the mode check is done at task level as depicted in the following picture:

Only if the condition is true runnable runB is called. Within this runnable the counting modes are decremented by 3.

The last mechanisms is also possible at runnable level by using the runnableModeSwitch.

Beware of where to reset or change the modes. In doB the modes are reseted directly in the beginning. If you by accident put these after the tick element, it could result in an unintended behavior. In the generator tasks be aware of where to put the customEventTrigger. This element activates the stimuli evaluation. If you put a tick element between the increment and the trigger, the evaluation is delayed.