base_models¶
common¶
- class EntityAwareInitDataHandler(handler: InitDataClient, strategy: ExternalSerializationStrategy | None = None)¶
Bases:
InitDataClient
simple_model¶
- class SimpleModel(model_config: dict, validate_config=True)¶
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)¶
- schema: AttributeSchema = None¶
- set_schema(schema)¶
- update(message: UpdateMessage, data: dict | None) Tuple[dict | None, int | None]¶
- update_series(message: UpdateSeriesMessage, data: Iterable[dict | None]) Tuple[dict | None, int | None]¶
tracked_model¶
- class TrackedModel(model_config: dict, validate_config=True)¶
Bases:
ModelTo work with a
TrackedState, a model developer could create their ownTrackedState()object and work with it directly to track changes and produce updates of changed data. However, It is also possible to extend thisTrackedModelclass and let theTrackedModelAdaptermanage theTrackedState- Variables:
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_resetto0,PUBLISH,SUBSCRIBEorPUBLISH|SUBSCRIBE. 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
initializemethod is called when all of thestate’sINITattribute arrays are filled with data. This may be during the model engines initialization phase or duringt=0. Data that is required for the model to initialize attribute may be published in another model’s t0-update, and theTrackedModelAdaptercan wait for this to happen before callinginitialize. When the simulation progresses tot>0before the model’s INIT attributes have been filled, an Exception is raised, indicating that the model was not ready yet.Model.initializemay raiseNotReadyto indicate that it does not have its required input data yet. This is for example useful if a model has a numberOPTional required attributes of which at least one must be set. The model would check whether this is the case, and raiseNotReadyif 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, moment: 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
initializeandupdatemethods have been called the first time.- Parameters:
state – The model’s TrackedState object, managed by the
TrackedModelAdaptermoment – The new simulation
Moment
- abstractmethod setup(state: TrackedState, settings: Settings, schema: AttributeSchema, init_data_handler: InitDataHandler, logger: Logger)¶
In
setup, a model receives astateobject, it’sconfigand other parameters. The goal ofsetupis to prepare thestateby 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 inEntityGroupclasses or created directly. The main entry points for registering are:state.add_dataset()for registering a bunch ofEntityGroupclasses for a certain dataset name at oncestate.add_entity_group()for registering a singleEntityGroupclass (or instance) for a dataset namestate.register_attribute()for registering a single attribute in a dataset/entity_group combination
During
setupthere is no data available in thestate. These will be downloaded automatically by theTrackedModelAdapter. However, additional datasets may be requested directly through theinit_data_handlerparameter.- Parameters:
state – The model’s TrackedState object, managed by the
TrackedModelAdaptersettings – global settings
schema – The AttributeSchema with all registered attributes
init_data_handler – an
InitDataHandlerthat may be used to retrieve additional datasetslogger – a
logging.Loggerinstance
- 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
initializeandupdatemethods have been called the first time
- abstractmethod update(state: TrackedState, moment: Moment) Moment | None¶
The
updatemethod 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 tot>0before 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
TrackedModelAdaptermoment – The current simulation
Moment
- Returns:
an optional
Momentindicating 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¶
- model_initialized: bool¶
- model_ready_for_update: bool¶
- new_time(message: NewTimeMessage)¶
- next_time: int | None¶
- process_input(data: dict | None) bool¶
- schema: AttributeSchema | None¶
- set_schema(schema: AttributeSchema)¶
- try_initialize()¶
- update(message: UpdateMessage, data: dict | None) Tuple[dict | None, int | None]¶
- update_series(message: UpdateSeriesMessage, data: Iterable[dict | None]) Tuple[dict | None, int | None]¶