////
  // Copyright (c) 2021, 2025 Contributors to the Eclipse Foundation
  //
  // This program and the accompanying materials are made
  // available under the terms of the Eclipse Public License 2.0
  // which is available at https://www.eclipse.org/legal/epl-2.0/
  //
  // SPDX-License-Identifier: EPL-2.0
////

include::_initCommon.adoc[]

[[dispatching-generation]]
== Generating an Optimal Activity Dispatching Sequence

A supervisory controller modeled using CIF, see <<create-supervisory-controller-specification,Create Supervisory Controller Specification>>, captures all possible activity dispatching sequences.
Given such a specification, an optimal activity dispatching sequence can be computed.
We distinguish between two situations:

. *Optimal continuous processing of products* 
In the first case, the supervisory controller specifies all possible activity sequences to establish a continuous product flow. 
The supervisor should have cycles, which represent repeatable activity sequences, modeling the continuous processing of products.
An optimal activity dispatching sequence can be generated, that contains a repeatable activity sequence achieving the maximum throughput, as well as the transient activity sequence to get to this steady-state repeatable activity sequence from the initial system state.

. *Optimal processing a batch of products* 
In the second case, the supervisory controller specifies all possible product flows for a batch of products.
The supervisor should not have any cycle. It is assumed that any path to a final state without enabled edges means that the product flow successfully completed.
An optimal activity dispatching sequence can be generated, that achieves the minimum makespan, i.e., the fastest way to execute all required activities and reach a final state.


=== Generating a Dispatching File for Maximum Throughput
*Target element*: an activity specification

Right click on a target element, select _Generate Dispatching File (throughput)_ to generate an activity dispatching sequence with maximum throughput.

image::{imgsdir}/clickThroughput.png[]


Select a relevant CIF specification file.

image::{imgsdir}/selectThroughputCIF.png[]

In this example, we use the following CIF specification.
---- 
include::{codedir}/supervisor_cyclic.cif[lines=10..21]
----

Click 'OK' to start running the throughput analysis.

image::{imgsdir}/clickThroughput.png[]

Once the analysis is finished, the computed throughput value is reported.

image::{imgsdir}/throughputResult.png[]

The dispatching file containing the optimal activity dispatching sequence is generated in the _result_ folder.

image::{imgsdir}/throughputResultDispatching.png[]

The first part of the dispatching file specifies the activity sequence that is needed to reach the steady state of the system.
In the steady state, the same repeatable activity sequence can continuously be executed.
In our example, this steady state is immediately reached.
The throughput is defined as the number of activities that are executed, divided by the total execution time of the cycle.
In our example, the throughput is 2 activities / 11.606 sec = 0.1723 activities per second.

This can visually be shown in the following corresponding Gantt chart.

image::{imgsdir}/throughputResultGantt.png[]

NOTE: To scheduled the generated activity dispatching sequence in the form of a Gantt chart, see section <<schedule-activities>>.


[NOTE]
====
To calculate the maximum throughput, the CIF file must contain a cycle.

image::{imgsdir}/errorThroughput.png[]
====

=== Generating a Dispatching File for Minimum Makespan
*Target element*: an activity specification

Right click on a target element, select _Generate Dispatching File (makespan)_ to generate an activity dispatching sequence with minimum makespan.

image::{imgsdir}/clickMakespan.png[]

Select a relevant CIF specification file.

image::{imgsdir}/selectMakespanCIF.png[]

In this example, we use the following CIF specification.
---- 
include::{codedir}/supervisor_acyclic.cif[lines=10..21]
----

Click 'OK' to start running the makespan analysis.


Once the analysis is finished, the computed makespan value is reported, which is the total execution time of the activity sequence.

image::{imgsdir}/makespanResult.png[]

The dispatching file showing the optimal activity dispatching sequence is generated in the _result_ folder.

image::{imgsdir}/makespanResultDispatching.png[]


[NOTE]
====
To calculate the minimum makespan, the CIF file must not contain a cycle.

image::{imgsdir}/errorMakespan.png[]
====

=== Viewing Makespan/Throughput Values

The minimum makespan/maximum throughput, as well as some details about the algorithm execution, can be seen in the _Error Log_ view. 
The _Error Log_ view can be opened by clicking _Window_ in the toolbar on top and Show View -> Error Log.

image::{imgsdir}/openErrorLog.png[]

image::{imgsdir}/showThroughput.png[]

[WARNING]
====
An infinite state space can be generated during the throughput analysis, if there are multiple resources that can execute actions without ever synchronizing.
A warning is generated if a resource is nowhere used in the analysis, or if it is never synchronized with another resource.
To prevent a never terminating analysis, these problems should be resolved in the specification.
====
