Since manually creating large instance models requires a lot of effort, we developed a CPS Model Generator that executes a number of generation phases defined in a plan and based on simple configuration can output arbitrarily large CPS models. The model generator is built in Xtend and uses VIATRA Query patterns for gathering elements for complex operations.

The model generator aims to output models that are similar in fine structure but have different number of elements (to generate scaled-up models) and allow some randomization (e.g. to create state machines with different number of states for different application types).

Randomization is controlled by min-max and percentage type parameters and ratio maps:

- A min-max parameter specifies a range with a minimum and maximum value, while operations depending on the given parameter can obtain a random number that is part of the range (e.g. create statemachine with 5 to 10 states).

- A percentage parameter specifies the fraction of a total, while operations may use it to decide how to distribute the choices for the possible elements (e.g. 35% of transitions in a state machine should have actions).

- A ratio map parameter assigns integer values to classes, while operations may use it to distribute choices (e.g. how application instances are allocated to different host class instances).

The fine structure is specified with host and application classes:

- A host class determines how many host types are created (min-max), how many instances are created for each type in the class (min-max), how many other host instance each host instance communicates with (min-max) and how the communications are distributed among instances of different host classes (ratio map).

- An application class determines how many application types are created (min-max), how many instances are created for each type in the class (min-max), how many states and transitions should the state machine of each type contain (both min-max), how many of the instances are allocated (percentage), how is the allocation distributed among instances of different host classes (ratio map), how many of the transitions should define actions (percentage) and of those what is the ratio of sends (percentage).

Based on a list of host and application classes as input, the CPS model generator outputs an instance model that satisfies the constraints of the classes. While min-max parameters are always satisfied, percentage and ratio map parameters may not be precisely followed (e.g. allocating 35% of 10 applications may be 3 or 4). However, for larger sizes and in general, the generated model will have the structure specified in the classes.

The model generator component is able to generate `CyberPhysicalSystem` models with the specified properties. The generator is built on top of the PlanExecutor and implements specific plans, phases, operations, initializer and fragment objects. The generation process is based on pseudo-random actions which means the output is deterministic according to the input parameters.

Preferences of the model are declared in `ICPSConstraints` objects. The following constraints are available:

- It is able to define Host and Application Classes which specify constraints for `Host-` and `ApplicationTypes`. Each HostClass contains the following attributes:

  - Name: Name of the HostClass (important for the ids)

  - HostTypes: Min-Max value of the instantiated `HostTypes` (the exact value is randomized between the min and max values)

  - HostInstances: Min-Max value of the instantiated `HostInstances`. (the exact value is randomized separately for each `HostTypes` of the HostClass, between the min and max values)

  - CommunicationLines: This min-max value specifies the number of accessible `HostInstances` (`communicateWith` attribute).

  - CommunicationRatios: The accessible `HostInstances` are chosen from these `HostClasses` with the given ratio.

- ApplicationClass is defined with the following properties:

  - Name: Name of the ApplicationClass (important for the ids)

  - ApplicationTypes: Min-Max value of the instantiated `ApplicationTypes` (the exact value randomized between the min and max values)

  - ApplicationInstances: Min-Max value of the instantiated `ApplicationInstances`. (the exact value is randomized separately for each `ApplicationTypes` of the ApplicationClass, between the min and max values)

  - States: Min-Max value of the instantiated `States` for each `StateMachine` of the `ApplicationType`

  - Transitions: Min-Max value of the instantiated `Transitions` for each `StateMachine` of the `ApplicationType`

  - PercentageOfAllocatedInstances: Probability of application allocation

  - AllocationRatios: Describes the allocation ratio by HostClasses

  - ProbabilityOfActionGeneration: probability of the action generation for `Transitions`

  - ProbabilityOfSendAction: probability of generating sendSignal action (other is the wiatForSignal)

- Number of the available signals is specified with Min-Max value

## CPS model generation plan

The CPS plan consists of seven phases and eight operations.

- Prepare: prepare the IncQueryEngine

- SignalSet: Generate signals

- Types: Generate Host and Application types (include StateMachines) according to the Classes

- Instances: Generate Host and Application instances according to the Classes

- Host Communication: Add communication lines to `HostInstances`

- Allocations: Allocate `ApplicationInstances` to `HostInstances`

- Actions: Generate actions to `Transitions`

## Usage example

First, the `ICPSConstraints` interface shall be implemented.

    class SimpleCPSConstraints implements ICPSConstraints {

        override getName() {
            "Simple"
        }

        val hostClass1 = new HostClass(
            "FirstHostClass",
            new MinMaxData(1, 3), // HostTypes
            new MinMaxData(2, 5), // HostInstances
            new MinMaxData(1, 2), // CommLines
            new HashMap // CommRatios
        )
        val hostClass2 = new HostClass(
            "OtherHostClass",
            new MinMaxData(1, 1), // HostTypes
            new MinMaxData(2, 2), // HostInstances
            new MinMaxData(1, 1), // CommLines
            new HashMap // CommRatios
        )

        new() {
            for (class1 : hostClasses) {
                for (class2 : hostClasses) {
                    class1.communicationRatios.put(class2, 1)
                }
            }
        }

        override getHostClasses() {
            #[hostClass1, hostClass2];
        }

        override getNumberOfSignals() {
            new MinMaxData(1, 10);
        }

        override getApplicationClasses() {
            val firstAppClassAllocations = new HashMap();
            firstAppClassAllocations.put(hostClass1, 1);
            firstAppClassAllocations.put(hostClass2, 2);

            #[
                new AppClass(
                    "FirstAppClass",
                    new MinMaxData(1, 3), // AppTypes
                    new MinMaxData(1, 2), // AppInstances
                    new MinMaxData(2, 4), // States
                    new MinMaxData(1, 2), // Transitions
                    new Percentage(100), // PercentageOfAllocatedInstances
                    firstAppClassAllocations, // allocationRatios
                    new Percentage(95), // probabilityOfActionGeneration
                    new Percentage(60) // probabilityOfSendAction
                )
            ];
        }
    }

This model shall contain least one and maximum three `HostTypes` of FirstHostClass and exactly one of the OtherHostClass. Each `HostType` of the FirstHostClass shall be instantiated minimum two and maximum five times and the `HostInstances` shall communicate with one or two other instances. The OtherHostClass is more stringent, it specifies the exact number of types, instances and communication lines (1,2,1). Instances can communicate with other instances from both HostClasses with equal possibility. Number of the generated signals shall be in range of 1 to 10. The SimpleCPSConstraints specifies only one ApplicationClass, the FirstAppClass. Least one and maximum three ApplicationType shall be created for this class. Each types of FirstAppClass shall be instantiated one or two times and the `StateMachine` of the types shall contain minimum two and maximum four `States` with one or two `Transactions`. Every `ApplicationInstance` shall be allocated (PercentageOfAllocatedInstances ). Two times more application instances shall be allocated on the instances of the OtherHostClass than the FirstHostClass (allocationRatios). Transitions contain actions with 95% and the probability of the "sendSignal" is 60%.

Then the `CPSGeneratorBuilder.buildAndGenerateModel(long seed, ICPSConstraints constraints) : CPSFragment` should be called.