# CPS Application

The CPS Demonstrator application include various features to demonstrate various capabilities of VIATRA using the CPS domain models.

## How to start

Start the CPS Application Eclipse instance (download from here).

| Starting multiple instances of this application causes `Java was started but returned exit code=1` error message caused by both applications opening port 1099 for the VIATRA debugger to connect to. In order to support multiple instances, you have to modify the debug port number in the corresponding eclipse.ini file.

## Initializing CPS models

Select File/New/Other and in the CPS Demonstrator category select CPS Demonstrator Example and create an example project.
This project contains an example CPS model (example.cyberphysicalsystem) as the source model of the transformation, an empty target model (example.deployment) and an example.traceability, which connects the source and target models.

## Executing the transformation

The CPS Transformations view allows starting or stopping CPS-to-Deployment M2M transformations using different strategies and displays the running transformations.
To open the view press CTRL+3 and type the name of the view.

The view shows the details of the currently running transformations:

- the name of the resource which contains the model

- the transformation strategy

Double-click on a transformation to navigate to the model.

Transformation strategies:
In a drop-down list you can select a strategy for the transformation. There are two execution modes batch and incremental.

- The batch mode clears the destination model and transforms the whole source model then stops.

- The incremental transformations transform the model, then continue to run and apply changes of the source model to the destination model.

Operations:

- Run Transformation image::cps/load_query_icon.png[] : Open the Traceability model and select a `CPS To Deployment` element. This button starts a new transformation with the selected strategy.

- Run Transformation with Debugger image::cps/load_xform_w_debugger_icon.png[] : This button starts a new transformation on a `CPS To Deployment` element with the selected strategy. The transformation does not start until a VIATRA Transformation Remote Debugging is connected.

- Stop Transformation image::cps/unload_icon.gif[] : Stops a currently running incremental transformation (selected below) and releases its resources.

## Advanced

### Using the Transformation Debugger

1. Select a `CPS To Deployment` element and start a VIATRA transformation with debugger support. Now the transformation is waiting for a debugger to be attached.

2. Open Debug perspective

3. In Run/Debug Configurations create a new VIATRA Transformations Remote Debugging

4. Set the Target Port to 10990 and press Query Port, then the transformation appears in the Target VIATRA Transformation list

5. The Java class which defines the transformation should be entered into the VIATRA Transformation Class field to associate the breakpoints with it. This time use a new dummy class in a new Java project.

6. Press Debug

Use Step Over, set breakpoints and Resume the transformation.
The following views help the debugging:

- Transformation Browser view shows which activation of the transformation rules can be executed. Here you can set breakpoints to suspend if a certain rule or activation is to be executed or a condition is true.

- Transformation Model Instance Viewer shows the current state of the models during the transformation.

| Using the same Eclipse instance for debugging an incremental transformation and changing a model causes the program to freeze. Use a different Eclipse instance or run a new Eclipse Application instance from Run/Run Configurations and run the debugger from it.

Find more details on the debugger here.

### CPS Model Generator wizard

In addition of initializing a prepared example or manually creating the CPS models, the demonstrator also includes a CPS Model Generator, which can create instances of predefined models and also supports generating arbitrarily large CPS models.

Select File/New/Other and in the CPS Demonstrator category select Generate CPS Model. Select a folder and enter a name for the CPS model.

| In contrast to CPS Demonstrator Example project, which generates a source CPS model, a target Deployment model and a connector Traceability model, the CPS Model Generator only generates the CPS model, which must be connected to the other models to use it in a transformation. To do this manually refer this section.

To create a simple model you can choose from a few preset models under the Simple Test Models option.

| Some generators use random numbers. The initial value of the seed is time-dependent, so models are very likely to be distinct on each generation. To produce the same models use the same arbitrary number as random seed each time.

#### Scalable Models

To create arbitrarily large CPS models, which are similar in structure, but have different number of elements, choose a Scalable Model type. Enter a positive a scaling factor, which is proportional to the number of model elements. To generate a model with reasonable size which can be opened in the editor enter a scaling factor from the recommended range.

| Opening large models can take a while. Switch off opening if it is not needed.

#### Model by Detailed Constraints

CPS Model Generator uses a detailed configuration during generation phase. (<<Model-Generator#,Here> described in detail.) A simple version of this configuration can be used if you select Model by Detailed Constraints.

Some of the parameters are specified with a min-max range, which is used to obtain a random number of the range to get the exact value of that parameter.
Other parameters are percentage parameters, which is used to decide how to distribute the choices for the possible elements.

For the Hosts you can define how many HostTypes and how many HostInstances for each type exist. For each instance the number of communication lines will fall into the defined range. The number of generated signals can be specified too.
The number of ApplicationTypes and ApplicationInstances can be defined similarly. Also the number of states and transitions in the statemachine of an ApplicationInstance can be defined. The ratio how many ApplicationInstances are allocated to a HostInstance, the ratio of actions in the transitions and the ratio of send action in all the actions can be specified with percentage parameters.

### Initializing CPS models manually

- Create a Deployment Model (File/New/Other and CPS Demonstrator category)

  - Root element shall be Deployment

- Create a Traceability Model (File/New/Other and CPS Demonstrator category)

  - Root element shall be CPS To Deployment

- Open the Traceability file

- In the Traceability editor, load both the existing CPS and the newly created Deployment models with Load Resources... in the context menu

- Set CPS and Deployment references of Traceability model in the properties view