# Timed-SAS: Modeling and Analyzing the Time Behaviors of Self-Adaptive Software under Uncertainty
## 4 Implementation of the Timed-SAS Approach
### Formal Modeling Templates and Application
#### 4.2.1 Templates for Self-Adaptation Processes and Time Behaviors  
In the Timed-SAS approach, we use the NPTA model to depict the self-adaptation processes of \"Monitor-Analyze-Plan-Execute\", use \"declaration\" to model the \"Knowledge\" module, and use \"clock\", \"guard\" and \"invariant\" to specify the deterministic time constraints. The uncertainty delay time constraints are implemented in the \"declaration\" module.  
Concretely, the modeling template for Monitor process and Monitoring Period \(M_{\text{Period}}\) is shown in Figure 8a. It has decomposed the Monitor process into four sub-processes of \"Timing, Data-Collection, Comparing, Triggering\". In the Timing sub-process, it has defined Monitoring Period by integrating invariant \(T<\) = Period with guard \(T==\) Period. In the Data-Collection sub-process, it uses the function \(getData(\,)\) to detect the software running information. In the Comparing sub-process, it judges system states, _low, high_ or _NoChange_, by comparing the monitoring variable with the upper and lower limit with the _compare_(\(\,\)) function. Finally, it triggers different subsequent transitions by assigning different values to _flagA_. Notably, the implementation details of the above two functions are stored in the back-end configuration, which support customized definition according to the application scenarios.  
The modeling template for Analyze process and Triggering-Delay Time \(A_{\text{Delay}}\) is shown in Figure 8b. This process is triggered by _flagA_, and it is composed of three sub-processes of \"Delaying-Comparing-Triggering\". In the process of Delaying, two Triggering-Delay Time \(D\_T1\) and \(D\_T2\) are defined. When the Triggering-Delay Time is finished and the monitored variable is still over-limit, the system state would be determined as \"_OverSatisfied_\" or \"_UnderSatisfied_\" with function _analyze_(\(\,\)). Meanwhile, the flag of _flagP_ would be assigned as \(-1\) or \(1\), and the subsequent self-adaptation processes would be triggered. Similarly, the implementation details of _analyze_(\(\,\)) need to be further customized.  
The modeling template for Plan process and the uncertainty delay time \(Delay_{\text{Plan}}\) is shown in Figure 8c. This process is triggered by _flagP_, and the concrete planning processes are implemented with two functions of \(plan\_LCL(\,)\) and \(plan\_LCL(\,)\), which need customization according to concrete applications. The uncertainty delay time is implemented with the function of _delay_(_type, par1, par2_), the parameters of which needs further instantiation.  
Similarly, the template for Execute process and the uncertainty delay time \(Delay_{\text{Execute}}\) is shown in Figure 8d. This process is triggered by _flagE_, and the executing processes are implemented with two functions of \(ex\_LCL(\,)\) and \(ex\_UCL(\,)\), which need further customization in the back-end configuration.  
The uncertainty delay time constraints are implemented in the \"declaration\" module of NPTA, and Listing 1 provides the details. The function _delay_( ) defines types of probability distribution (from line 15 to line 20) for the delay time. When _type_ = 1, the uncertainty delay time follows the normal distribution (from line 17 to line 18), while _type_ = 2 follows the uniform distribution (from line 19 to line 20). The uniform distribution function (from line 9 to line 13) is created based on the UPPAAL-SMC built-in function of _random_ ( ); while the normal distribution function (from line 5 to line 8) is implemented based on the standard normal distribution function (from line 1 to line 4), which is created using the Box-Muller method [24].