base_models#

common#

class EntityAwareInitDataHandler(handler: InitDataHandler, strategy: ExternalSerializationStrategy | None = None)#

Bases: InitDataHandler

get(name: str) Tuple[FileType | None, DatasetPath | None]#

simple_model#

class SimpleModel(model_config: dict)#

Bases: Model

close(message: QuitMessage)#
get_adapter() Type[ModelAdapterBase]#
initialize(settings: Settings, schema: AttributeSchema, init_data_handler: InitDataHandler, logger: Logger) DataMask#
new_time(new_time: Moment, message: NewTimeMessage)#
update(moment: Moment, data: dict | None, message: UpdateMessage) Tuple[dict | None, Moment | None]#
update_series(moment: Moment, data: Iterable[dict | None], message: UpdateSeriesMessage) Tuple[dict | None, Moment | None]#
class SimpleModelAdapter(model: Model, settings: Settings, logger: Logger)#

Bases: ModelAdapterBase

close(message: QuitMessage)#
initialize(init_data_handler: InitDataHandler) DataMask#
model: SimpleModel#
new_time(message: NewTimeMessage)#
process_input(data: bytes | None) dict | None#
process_result(result: Tuple[dict | None, Moment | int | None]) Tuple[bytes | None, int | None]#
schema: AttributeSchema = None#
serialization: InternalSerializationStrategy#
set_schema(schema)#
update(message: UpdateMessage, data: bytes | None) Tuple[bytes | None, int | None]#
update_series(message: UpdateSeriesMessage, data: Iterable[bytes | None]) Tuple[bytes | None, int | None]#

tracked_model#

class TrackedModel(model_config: dict)#

Bases: Model

To work with a TrackedState, a model developer could create their own TrackedState() object and work with it directly to track changes and produce updates of changed data. However, It is also possible to extend this TrackedModel class and let the TrackedModelAdapter manage the TrackedState

Attributes:
auto_reset By default, the TrackedModelAdapter resets tracking information of the

state for PUB and/or SUB attributes at the appropriate time, so that the model receives a SUB update only once, and PUB attributes are published only once. By setting auto_reset to 0, PUB, SUB or PUB|SUB. A model can limit this automatic behaviour and gain full control over which attributes are reset and when. However, when overriding the default behaviour, a model must be very careful in implementing this appropriately.

auto_reset = 10#
get_adapter() Type[ModelAdapterBase]#
initialize(state: TrackedState)#

The initialize method is called when all of the state’s INIT attribute arrays are filled with data. This may be during the model engines initialization phase or during t=0. Data that is required for the model to initialize attribute may be published in another model’s t0-update, and the TrackedModelAdapter can wait for this to happen before calling initialize. When the simulation progresses to t>0 before the model’s INIT attributes have been filled, an Exception is raised, indicating that the model was not ready yet.

Model.initialize may raise NotReady to indicate that it does not have its required input data yet. This is for example useful if a model has a number OPT`ional required attributes of which at least one must be set. The model would check whether this is the case, and raise `NotReady if it is not. Once a model has succesfully run its initialize method, this method will not be called again for the duration of the simulation.

Parameters:

state – The model’s TrackedState object, managed by the TrackedModelAdapter

new_time(state: TrackedState, time_stamp: Moment)#

Called for every change of timestamp during a simulation run. This method is called before checking whether the state is ready for INIT or PUB and may be called before the initialize and update methods have been called the first time.

abstract setup(state: TrackedState, settings: Settings, schema: AttributeSchema, init_data_handler: InitDataHandler, logger: Logger)#

In setup, a model receives a state object, it’s config and other parameters. The goal of setup is to prepare the state by giving it information of the attributes it needs to track (by subscribing (INIT/SUB/OPT) or publishing (PUB) attributes) from which datasets. These attributes may be grouped together in EntityGroup classes or created directly. The main entry points for registering are:

  • state.add_dataset() for registering a bunch of EntityGroup classes for a certain dataset name at once

  • state.add_entity_group() for registering a single EntityGroup class (or instance) for a dataset name

  • state.register_attribute() for registering a single attribute in a dataset/entity_group combination

During setup there is no data available in the state. These will be downloaded automatically by the TrackedModelAdapter. However, additional datasets may be requested directly through the init_data_handler parameter.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • settings – global settings

  • schema – The AttributeSchema with all registered attributes

  • init_data_handler – an InitDataHandler that may be used to retrieve additional datasets

  • logger – a logging.Logger instance

shutdown(state: TrackedState)#

Called when a simulation ends (either due to it being finished or one of the models raises an exception). The model may implement this method to clean up local resources. This method may be called before the initialize and update methods have been called the first time

abstract update(state: TrackedState, moment: Moment) Moment | None#

The update method is called for every update coming from the model engine. However it is only called the first time once all PUB attributes have their arrays filled with data. When the simulation progresses to t>0 before the model’s SUB attributes have been filled, an Exception is raised, indicating that the model was not ready yet.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • moment – The current simulation Moment

Returns:

an optional Moment indicating the next time a model want to be woken up, as per the model engine’s protocol

class TrackedModelAdapter(model: TrackedModel, settings: Settings, logger: Logger)#

Bases: ModelAdapterBase

close(message: QuitMessage)#
download_init_data(init_data_handler: InitDataHandler)#
format_uninitialized_attributes() str#
initialize(init_data_handler: InitDataHandler)#
model: TrackedModel#
new_time(message: NewTimeMessage)#
process_input(data: bytes | None) bool#
process_result(result: Tuple[dict | None, Moment | int | None]) Tuple[bytes | None, int | None]#
serialization: InternalSerializationStrategy#
set_schema(schema: AttributeSchema)#
try_calculate(timestamp: int, should_calculate=True) Tuple[dict | None, Moment | int | None]#
try_initialize()#
update(message: UpdateMessage, data: bytes | None) Tuple[bytes | None, int | None]#
update_series(message: UpdateSeriesMessage, data: Iterable[bytes | None]) Tuple[bytes | None, int | None]#

Module contents#

class SimpleModel(model_config: dict)#

Bases: Model

close(message: QuitMessage)#
get_adapter() Type[ModelAdapterBase]#
initialize(settings: Settings, schema: AttributeSchema, init_data_handler: InitDataHandler, logger: Logger) DataMask#
new_time(new_time: Moment, message: NewTimeMessage)#
update(moment: Moment, data: dict | None, message: UpdateMessage) Tuple[dict | None, Moment | None]#
update_series(moment: Moment, data: Iterable[dict | None], message: UpdateSeriesMessage) Tuple[dict | None, Moment | None]#
class SimpleModelAdapter(model: Model, settings: Settings, logger: Logger)#

Bases: ModelAdapterBase

close(message: QuitMessage)#
initialize(init_data_handler: InitDataHandler) DataMask#
logger: Logger#
model: SimpleModel#
new_time(message: NewTimeMessage)#
process_input(data: bytes | None) dict | None#
process_result(result: Tuple[dict | None, Moment | int | None]) Tuple[bytes | None, int | None]#
schema: AttributeSchema = None#
serialization: InternalSerializationStrategy#
set_schema(schema)#
settings: Settings#
update(message: UpdateMessage, data: bytes | None) Tuple[bytes | None, int | None]#
update_series(message: UpdateSeriesMessage, data: Iterable[bytes | None]) Tuple[bytes | None, int | None]#
class TrackedModel(model_config: dict)#

Bases: Model

To work with a TrackedState, a model developer could create their own TrackedState() object and work with it directly to track changes and produce updates of changed data. However, It is also possible to extend this TrackedModel class and let the TrackedModelAdapter manage the TrackedState

Attributes:
auto_reset By default, the TrackedModelAdapter resets tracking information of the

state for PUB and/or SUB attributes at the appropriate time, so that the model receives a SUB update only once, and PUB attributes are published only once. By setting auto_reset to 0, PUB, SUB or PUB|SUB. A model can limit this automatic behaviour and gain full control over which attributes are reset and when. However, when overriding the default behaviour, a model must be very careful in implementing this appropriately.

auto_reset = 10#
get_adapter() Type[ModelAdapterBase]#
initialize(state: TrackedState)#

The initialize method is called when all of the state’s INIT attribute arrays are filled with data. This may be during the model engines initialization phase or during t=0. Data that is required for the model to initialize attribute may be published in another model’s t0-update, and the TrackedModelAdapter can wait for this to happen before calling initialize. When the simulation progresses to t>0 before the model’s INIT attributes have been filled, an Exception is raised, indicating that the model was not ready yet.

Model.initialize may raise NotReady to indicate that it does not have its required input data yet. This is for example useful if a model has a number OPT`ional required attributes of which at least one must be set. The model would check whether this is the case, and raise `NotReady if it is not. Once a model has succesfully run its initialize method, this method will not be called again for the duration of the simulation.

Parameters:

state – The model’s TrackedState object, managed by the TrackedModelAdapter

new_time(state: TrackedState, time_stamp: Moment)#

Called for every change of timestamp during a simulation run. This method is called before checking whether the state is ready for INIT or PUB and may be called before the initialize and update methods have been called the first time.

abstract setup(state: TrackedState, settings: Settings, schema: AttributeSchema, init_data_handler: InitDataHandler, logger: Logger)#

In setup, a model receives a state object, it’s config and other parameters. The goal of setup is to prepare the state by giving it information of the attributes it needs to track (by subscribing (INIT/SUB/OPT) or publishing (PUB) attributes) from which datasets. These attributes may be grouped together in EntityGroup classes or created directly. The main entry points for registering are:

  • state.add_dataset() for registering a bunch of EntityGroup classes for a certain dataset name at once

  • state.add_entity_group() for registering a single EntityGroup class (or instance) for a dataset name

  • state.register_attribute() for registering a single attribute in a dataset/entity_group combination

During setup there is no data available in the state. These will be downloaded automatically by the TrackedModelAdapter. However, additional datasets may be requested directly through the init_data_handler parameter.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • settings – global settings

  • schema – The AttributeSchema with all registered attributes

  • init_data_handler – an InitDataHandler that may be used to retrieve additional datasets

  • logger – a logging.Logger instance

shutdown(state: TrackedState)#

Called when a simulation ends (either due to it being finished or one of the models raises an exception). The model may implement this method to clean up local resources. This method may be called before the initialize and update methods have been called the first time

abstract update(state: TrackedState, moment: Moment) Moment | None#

The update method is called for every update coming from the model engine. However it is only called the first time once all PUB attributes have their arrays filled with data. When the simulation progresses to t>0 before the model’s SUB attributes have been filled, an Exception is raised, indicating that the model was not ready yet.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • moment – The current simulation Moment

Returns:

an optional Moment indicating the next time a model want to be woken up, as per the model engine’s protocol

class TrackedModelAdapter(model: TrackedModel, settings: Settings, logger: Logger)#

Bases: ModelAdapterBase

close(message: QuitMessage)#
download_init_data(init_data_handler: InitDataHandler)#
format_uninitialized_attributes() str#
initialize(init_data_handler: InitDataHandler)#
logger: Logger#
model: TrackedModel#
model_initialized: bool#
model_ready_for_update: bool#
new_time(message: NewTimeMessage)#
next_time: int | None#
process_input(data: bytes | None) bool#
process_result(result: Tuple[dict | None, Moment | int | None]) Tuple[bytes | None, int | None]#
schema: AttributeSchema | None#
serialization: InternalSerializationStrategy#
set_schema(schema: AttributeSchema)#
settings: Settings#
try_calculate(timestamp: int, should_calculate=True) Tuple[dict | None, Moment | int | None]#
try_initialize()#
update(message: UpdateMessage, data: bytes | None) Tuple[bytes | None, int | None]#
update_series(message: UpdateSeriesMessage, data: Iterable[bytes | None]) Tuple[bytes | None, int | None]#