This versatile engine enables users to execute a user-defined python script as an engine server, thus ensuring synchronization and enabling datapack data transfer with the Simulation Loop process. It can be used to integrate any simulator with a Python API in a NRP-core experiment.
Engines based on PythonJSONEngine can be implemented as Python classes based on EngineScript (as in the example listed below).
EngineScript
provides a base class from which custom Engines can inherit. The derived class must implement methods:
initialize()
: executed when the engine is initializedrunLoop(timestep_ns)
: executed when the engine is requested to advance its simulation (from EngineClient::runLoopStep)shutdown()
: executed when the engine is requested to shutdownOptionally, the derived class can implement a reset()
function. If it is implemented, it will be used for resetting the Engine. Otherwise the Engine is reset by calling shutdown()
and initialize()
sequentially.
Besides, EngineScript
provides the following ready-to-use methods to handle datapack communication:
_time_ns
: the internal simulation time of the Engine._getDataPack(datapack_name)
: returns the latest value available of a datapack with name datapack_name
_setDataPack(datapack_name, data)
: sets a new value for a datapack with name datapack_name
. data
is always a Python dictionary._registerDataPack(datapack_name)
: registers a datapack with the engine. Just registered datapacks can be set
and get
. Under the hood, registered datapacks are sent to the corresponding EngineClient upon request and their values updated when the EngineClient send them._config
: the engine configuration as a JSON objectBelow is an example of a class inheriting from EngineScript
. The example is taken from the examples/tf_exchange
experiment.
The Python JSON engine supports a unique datapack type: JsonDataPack, which can be used to transfer information between the engine and TFs. The data contained in this datapack can be any JSON-serializable Python object; that is, any object that can be decoded/encoded by JSONDecoder/JSONEncoder. This data can be accessed in TransceiverFunctions from the datapack data attribute, as shown in this example TF (also taken from examples/tf_exchange
):
Attribute | Description | Python Type | C type |
---|---|---|---|
data | data contained in the datapack as a NlohmannJson object | NlohmannJson | nlohmann::json |
This Engine type parameters are defined in the PythonJSONEngine schema (listed here), which in turn is based on EngineBase and EngineJSON schemas and thus inherits all parameters from them.
To use the Python JSON engine in an experiment, set EngineType
to "python_json".
Name | Description | Type | Default | Required | Array |
---|---|---|---|---|---|
EngineName | Name of the engine | string | X | ||
EngineType | Engine type. Used by EngineLauncherManager to select the correct engine launcher | string | X | ||
EngineProcCmd | Engine Process Launch command | string | |||
EngineProcStartParams | Engine Process Start Parameters | string | [] | X | |
EngineEnvParams | Engine Process Environment Parameters | string | [] | X | |
EngineLaunchCommand | LaunchCommand with parameters that will be used to launch the engine process | object | {"LaunchType":"BasicFork"} | ||
EngineTimestep | Engine Timestep in seconds | number | 0.01 | ||
EngineCommandTimeout | Engine Timeout (in seconds). It tells how long to wait for the completion of the engine runStep. 0 or negative values are interpreted as no timeout | number | 0.0 |
Name | Description | Type | Default | Required | Array |
---|---|---|---|---|---|
ServerAddress | EngineJSONServer address. Should this address already be in use, the server will continue trying ports higher up | string | localhost:9002 | ||
RegistrationServerAddress | Address EngineJSONRegistrationServer is listening at. Once the JSON engine server has bound to a port, it will use this address to register itself with the SimulationManager | string | localhost:9001 |
Name | Description | Type | Default | Required | Array |
---|---|---|---|---|---|
PythonFileName | Path to the Python script containing the engine definition | string | X | ||
ServerOptions | Additional options that will be used by the server (gunicorn) on startup. The string should contain a Python dictionary in the following format - "{'key1': value, 'key2': 'value_str'}". The full list of options can be found at the official page | string |
As explained above, the schema used by the PythonJSON engine inherits from EngineBase and EngineJSON schemas. A complete schema for the configuration of this engine is given below: