maltoolbox.model module
MAL-Toolbox Model Module
- class maltoolbox.model.Model(name: str, lang_graph: LanguageGraph, mt_version: str = '1.0.1')[source]
Bases:
object
An implementation of a MAL language model containing assets
- add_asset(asset_type: str, name: str | None = None, asset_id: int | None = None, defenses: dict[str, float] | None = None, extras: dict | None = None, allow_duplicate_names: bool = True) ModelAsset [source]
Create an asset based on the provided parameters and add it to the model.
Arguments: asset_type - string containing the asset type name name - string containing the asset name. If not
provided the concatenated asset type and id will be used as a name.
- asset_id - id to assign to this asset, usually from an
instance model file. If not provided the id will be set to the next highest id available.
defeses - dictionary of defense values extras - dictionary of extras allow_duplicate_name - allow duplicate names to be used. If allowed
and a duplicate is encountered the name will be appended with the id.
Return: The newly created asset.
- get_asset_by_id(asset_id: int) ModelAsset | None [source]
Find an asset in the model based on its id.
Arguments: asset_id - the id of the asset we are looking for
Return: An asset matching the id if it exists in the model.
- get_asset_by_name(asset_name: str) ModelAsset | None [source]
Find an asset in the model based on its name.
Arguments: asset_name - the name of the asset we are looking for
Return: An asset matching the name if it exists in the model.
- classmethod load_from_file(filename: str, lang_graph: LanguageGraph) Model [source]
Create from json or yaml file depending on file extension
- next_id: int = 0
- remove_asset(asset: ModelAsset) None [source]
Remove an asset from the model.
Arguments: asset - the asset to remove
- class maltoolbox.model.ModelAsset(name: str, asset_id: int, lg_asset: LanguageGraphAsset, defenses: dict[str, float] | None = None, extras: dict | None = None)[source]
Bases:
object
- add_associated_assets(fieldname: str, assets: set[ModelAsset])[source]
Add the assets provided as a parameter to the set of associated assets dictionary entry corresponding to the given fieldname.
- property associated_assets
- property id
- remove_associated_assets(fieldname: str, assets: set[ModelAsset])[source]
Remove the assets provided as a parameter from the set of associated assets dictionary entry corresponding to the fieldname parameter.
- validate_associated_assets(fieldname: str, assets_to_add: set[ModelAsset])[source]
Validate an association we want to add (through fieldname) is valid with the assets given in param assets_to_add: - fieldname is valid for the asset type of this ModelAsset - type of assets_to_add is valid for the association - no more assets than ‘field.maximum’ are added to the field
- Raises:
LookupError - fieldname can not be found for this ModelAsset –
ValueError - there will be too many assets in the field – if we add this association
TypeError - if the asset type of assets_to_add is not valid –