The Scheduler class
The PFDL scheduler is the interface between the PFDL core (model, petri net) and the outside world. If you want to receive the current state of the Production Order or push it further by sending status updates of the services you can register callback functions and fire events.
pfdl_scheduler.scheduler.Scheduler(pfdl_file_path, generate_test_ids=False, draw_petri_net=True, scheduler_id='', dashboard_host_address='')
Bases: Subject
Schedules Tasks of a given PFDL file.
The scheduler comprises almost the complete execution of a production order including the parsing of the PFDL description, model creation and validation and execution of the petri net. It interacts with the execution engines and informs them about services or tasks which started or finished.
This class implements the Observer pattern and serves as subject. Observers can be registered in the scheduler and receive updates (e.g. log entries, info about a new petri net img,..)
Attributes:
Name | Type | Description |
---|---|---|
running |
bool
|
A boolean that indicates whether the scheduler is running. |
pfdl_file_valid |
bool
|
A boolean indicating whether the given PFDL file was valid. |
process |
Process
|
The corresponding Process instance from the PFDL file. |
petri_net_generator |
PetriNetGenerator
|
A PetriNetGenerator instance for generating the petri net. |
petri_net_logic |
PetriNetLogic
|
A PetriNetLogic instance for execution of the petri net. |
task_callbacks |
TaskCallbacks
|
TaskCallbacks instance which holds the registered callbacks. |
variable_access_function |
Callable[[str], str]
|
The function which will be called when the scheduler needs a variable. |
loop_counters |
Dict[str, Dict[str, int]]
|
A dict for mapping task ids to the current loop counter (counting loops). |
awaited_events |
List[Event]
|
A list of awaited |
generate_test_ids |
bool
|
Indicates whether test ids should be generated. |
test_id_counters |
List[int]
|
A List consisting of counters for the test ids of tasks and services. |
observers |
List[Observer]
|
List of |
Initialize the object.
If the given path leads to a valid PFDL file the parsing will be started. If no errors
occur the model of the PFDL File will be transformed into a petri net and be drawn if
the draw_petri_net
flag is set. If generate_test_ids
is set the ids of the called
tasks and services will be an enumeration starting at 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pfdl_file_path |
str
|
The path to the PFDL file. |
required |
generate_test_ids |
bool
|
A boolean indicating whether test ids should be generated. |
False
|
draw_petri_net |
bool
|
A boolean indicating whether the petri net should be drawn. |
True
|
scheduler_id |
str
|
A unique ID to identify the Scheduer / Production Order |
''
|
dashboard_host_address |
str
|
The address of the Dashboard (if existing) |
''
|
start()
Starts the scheduling process for the given PFDL file from the path.
Returns:
Type | Description |
---|---|
bool
|
True if the corresponding PFDL file was valid and the Scheduler could be started. |
fire_event(event)
Forwards the given Event to the PetriNetLogic instance.
The given Event
object will be passed to the petri net if it is an awaited
event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event
|
An |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the event could be fired to the petri net (is an awaited event). |
register_callback_service_started(callback)
Registers the given callback in the service_started list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[ServiceAPI], Any]
|
Function which will be invoked when a Service is started. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the callback was successfully registered. |
register_callback_service_finished(callback)
Registers the given callback in the service_finished list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[ServiceAPI], Any]
|
Function which will be invoked when a Service is finished. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the callback was successfully registered. |
register_callback_task_finished(callback)
Scheduler Callbacks
This section is an introduction of how to use the schedulers callback functions.
In general, the callback functions can be subdivided into two classes: The first contains callback functions that are used by the scheduler to communicate the start or the finish of a task or service respectively:
The task started callback creates an object of this task inside the EE. The required information to create this object are inside the TASK API object that is provided by the scheduler. Most important for this callback are the task’s input variables. As tasks are nested inside other tasks, each new task object is a new level where local variables can be defined. The input variables that have to be part of the new task object are provided by the task_api.task_call.input_parameters object. New tasks can rename their input. The corresponding input values are provided in two different ways. First, variables can be transmitted to the new task object based on a variable inside the task_context. The second case describes the introduction of a new variable based on a literal structure definition inside the pfdl file. In this case, the variable’s value is provided by the task_api.task_call.input_parameters object.
Similar to the task started callback function, the task finished callback is used to update the task objects in the data lifecycle object. However, the task finished callback deletes them. In this case, variables in the task context object are either updated or added. The corresponding variables and values are provided that are returned from the task object can be extracted from the task_api.task.output_parameters object. The corresponding variable names are stored inside the task_api.task_call.output_parameters object.
The service started callback leads to a service execution on the field level. In this connection, the PFDL scheduler schedules one service. Its input variables can be accessed through the service.service.input_parameters object. Here, it is either possible to use literal input values from the PFDL file, or use existing variables from the task object to parameterize the service. To identify the task object from which the variables values have to be queried, each service provides a service context object that identifies the task object in which context the service is executed. Services are never added to the data lifecycle object; however, their execution can be illustrated by providing, e.g. a state variable to mirror the execution’s state and a service execution variable to specify the currently executed service.
As Services possess output variables, the Service finished callback function is used to either update existing variables in the task object or to add new ones. Equally to the service started callback, the service finished callback provides a service.task_context.uuid object to identify the task object that has to be adjusted.
The second class contains two callback functions that are used by the scheduler to access information from the EE. The first of these functions places tokens inside the petri net as soon as a service execution is completed. The second delivers a structure variable from the Data Lifecycle Object to the scheduler. This callback is required by the scheduler to access runtime data and thus, enables the scheduler to execute PFDL condition blocks. In this context, the scheduler always receives the complete structure and accesses single structure fields by itself. To identify the structure in the Data Lifecycle Object, the scheduler provides the tasks uuid, the task_contexts uuid and the variable name.
pfdl_scheduler.scheduling.event.Event(event_type='', data=None)
Data class for controlling the PetriNet instance.
Currently avaiable Events
- Event(event_type="service_finished", data={"service_id":
})
Attributes:
Name | Type | Description |
---|---|---|
event_type |
str
|
A string representing the type of the event. |
data |
Dict
|
A dict containing the corresponding data of the event type. |
from_json(json_string)
classmethod
pfdl_scheduler.scheduling.task_callbacks.TaskCallbacks()
dataclass
Contains lists of callback functions that where registered in the scheduler.
Attributes:
Name | Type | Description |
---|---|---|
task_started |
List[Callable[[TaskAPI], Any]]
|
A list of callback functions which get called when a task is started. |
service_started |
List[Callable[[ServiceAPI], Any]]
|
A list of callback functions which get called when a service is started. |
service_finished |
List[Callable[[ServiceAPI], Any]]
|
A list of callback functions which get called when a service is finished. |
task_finished |
List[Callable[[TaskAPI], Any]]
|
A list of callback functions which get called when a task is finished. |
Initialize the object.