1"""Model builder module.
3Provides some helper methods for easier work with basyx sdk data model
9from pathlib
import Path
12from basyx.aas
import model
16_logger = logging.getLogger(__name__)
20 """Generate a unique identifier string by appending a UUID to the provided ID short.
22 :param id_short: provided ID short
23 :return: unique identifier
25 return f
"{id_short}_{str(uuid.uuid4()).replace('-', '_')}"
29 """Creates a submodel from a JSON file file.
31 :param id: ID of the submodel. If empty, the template's ID is used
35 sm_template_file = Path(json_file).resolve()
38 if not sm_template_file.exists():
39 raise FileNotFoundError(f
"Submodel template file not found: {sm_template_file}")
44 with Path.open(sm_template_file,
"r", encoding=
"utf-8")
as f:
45 submodel_data = json.load(f)
48 submodel = sdk_tools.convert_to_object(submodel_data)
51 if not isinstance(submodel, model.Submodel):
52 raise TypeError(
"Loaded template JSON structure is not a Submodel.")
54 except Exception
as e:
55 _logger.error(f
"Error loading submodel template: {e}")
62 id_short: str |
None, type: model.datatypes, value: Any, display_name: str =
"", description: str =
""
64 """Create a basic SubmodelElement of type Property."""
65 sme = model.Property(id_short=id_short, value_type=type, value=value)
68 description_text = {
"en": f
"{description}"}
69 sme.description = model.MultiLanguageTextType(description_text)
72 display_name_text = {
"en": f
"{display_name}"}
73 sme.display_name = model.MultiLanguageNameType(display_name_text)
79 id_short: str, value: list[model.SubmodelElement], display_name: str =
"", description: str =
""
80) -> model.SubmodelElementCollection:
81 """Create a basic SubmodelElement of type SubmodelElementCollection."""
82 sme = model.SubmodelElementCollection(id_short=id_short, value=value)
85 description_text = {
"en": f
"{description}"}
86 sme.description = model.MultiLanguageTextType(description_text)
89 display_name_text = {
"en": f
"{display_name}"}
90 sme.display_name = model.MultiLanguageNameType(display_name_text)
95def create_base_submodel(identifier: str, id_short: str, display_name: str =
"", description: str =
"") -> model.Submodel:
96 """Create a basic Submodel.
98 :param identifier: identifier of the Submodel
99 :param id_short: ID short of the Submodel
100 :param display_name: display name of the Submodel, defaults to ""
101 :param description: description of the Submodel, defaults to ""
102 :return: Submodel instance
104 sm = model.Submodel(identifier)
105 sm.id_short = id_short
108 description_text = {
"en": f
"{description}"}
109 sm.description = model.MultiLanguageTextType(description_text)
112 display_name_text = {
"en": f
"{display_name}"}
113 sm.display_name = model.MultiLanguageNameType(display_name_text)
121 global_asset_identifier: str,
122 display_name: str =
"",
123 description: str =
"",
124 asset_kind: model.AssetKind = model.AssetKind.INSTANCE,
125) -> model.AssetAdministrationShell:
126 """Create a basic AAS and add base asset information.
128 :param identifier: identifier of the AAS
129 :param id_short: ID short of the AAS
130 :param global_asset_identifier: identifier of the global Asset. if not provided, defaults to the AAS identifier
131 :param display_name: display name of the AAS, defaults to ""
132 :param description: description of the AAS, defaults to ""
133 :param asset_kind: kind of the Asset, defaults to AssetKind.INSTANCE
134 :return: AssetAdministrationShell instance
136 if not identifier
or not id_short:
137 raise ValueError(
"Both 'identifier' and 'id_short' must be provided.")
139 if not global_asset_identifier:
140 global_asset_identifier = identifier
144 aas = model.AssetAdministrationShell(id_=identifier, asset_information=asset_info)
145 aas.id_short = id_short
148 description_text = {
"en": f
"{description}"}
149 aas.description = model.MultiLanguageTextType(description_text)
152 display_name_text = {
"en": f
"{display_name}"}
153 aas.display_name = model.MultiLanguageNameType(display_name_text)
161 asset_information: model.AssetInformation,
162 display_name: str =
"",
163 description: str =
"",
164) -> model.AssetAdministrationShell:
165 """Create a basic AAS.
167 :param identifier: identifier of the AAS
168 :param id_short: ID short of the AAS
169 :param asset_information: asset information of the AAS
170 :param display_name: display name of the AAS, defaults to ""
171 :param description: description of the AAS, defaults to ""
172 :return: AssetAdministrationShell instance
174 if not identifier
or not id_short
or not asset_information:
175 raise ValueError(
"Both 'identifier', 'id_short', and 'asset_information' must be provided.")
177 aas = model.AssetAdministrationShell(id_=identifier, asset_information=asset_information)
178 aas.id_short = id_short
181 description_text = {
"en": f
"{description}"}
182 aas.description = model.MultiLanguageTextType(description_text)
185 display_name_text = {
"en": f
"{display_name}"}
186 aas.display_name = model.MultiLanguageNameType(display_name_text)
191def create_specific_asset_id(name: str, value: str, external_subject_reference_value: str |
None =
None) -> model.SpecificAssetId:
192 """Create a new SpecificAssetId instance.
194 :param name: name of the SpecificAssetId
195 :param value: value of the SpecificAssetId
196 :param external_subject_reference_value: optional external subject reference value
197 :return: A new SpecificAssetId object.
199 if not name
or not value:
200 raise ValueError(
"Both 'name' and 'value' must be provided.")
202 external_subject_reference =
None
204 if external_subject_reference_value:
205 external_subject_reference = model.ExternalReference(
208 type_=model.KeyTypes.GLOBAL_REFERENCE,
209 value=external_subject_reference_value,
214 return model.SpecificAssetId(name=name, value=value, external_subject_id=external_subject_reference)
218 global_asset_id: str, asset_kind: model.AssetKind = model.AssetKind.INSTANCE, specific_asset_id: list[model.SpecificAssetId] |
None =
None
219) -> model.AssetInformation:
220 """Return a basic AssetInformation instance.
222 :param global_asset_id: global asset ID of the AssetInformation
223 :param asset_kind: kind of the Asset, defaults to AssetKind.INSTANCE
224 :param specific_asset_id: list of specific asset IDs, defaults to None
225 :return: AssetInformation instance
227 if not global_asset_id:
228 raise ValueError(
"The 'global_asset_id' must be provided.")
230 if specific_asset_id
is None:
231 specific_asset_id = []
233 return model.AssetInformation(asset_kind=asset_kind, global_asset_id=global_asset_id, specific_asset_id=specific_asset_id)
237 """Create a ModelReference.
239 :param id: ID of the Submodel to reference
240 :return: ModelReference instance
242 return model.ModelReference.from_referable(model.Submodel(id))
246 preferred_name: model.PreferredNameTypeIEC61360,
247 definition: model.DefinitionTypeIEC61360 |
None,
248 short_name: model.ShortNameTypeIEC61360 |
None,
250 unit_id: model.Reference |
None,
251 data_type: model.DataTypeIEC61360 |
None,
252 source_of_definition: str |
None,
254 value_format: str |
None,
255 value_list: set[model.ValueReferencePair] |
None,
257 level_types: list[model.IEC61360LevelType] |
None,
258) -> model.EmbeddedDataSpecification |
None:
259 """Create an embedded data specification with the given parameters.
261 :param preferred_name: The preferred name of the concept description.
262 :param definition: The definition of the concept description.
263 :param short_name: The short name of the concept description.
264 :param unit: The unit of the concept description.
265 :param unit_id: The unit reference of the concept description.
266 :param data_type: The data type of the concept description.
267 :param source_of_definition: The source of definition of the concept description.
268 :param symbol: The symbol of the concept description.
269 :param value_format: The value format of the concept description.
270 :param value_list: The value list of the concept description.
271 :param value: The value of the concept description.
272 :param level_types: The level types of the concept description.
273 :return: An instance of EmbeddedDataSpecification if successful, None otherwise.
275 if not preferred_name:
276 _logger.warning(
"Preferred name is missing for the embedded data specification.")
279 return model.EmbeddedDataSpecification(
280 data_specification=model.ExternalReference(
283 type_=model.KeyTypes.GLOBAL_REFERENCE, value=
"https://admin-shell.io/DataSpecificationTemplates/DataSpecificationIEC61360/3/1"
287 data_specification_content=model.DataSpecificationIEC61360(
288 preferred_name=preferred_name,
289 definition=definition,
290 short_name=short_name,
294 source_of_definition=source_of_definition,
296 value_format=value_format,
297 value_list=value_list,
299 level_types=level_types
or (),
model.Submodel create_submodel_from_file(str json_file="")
Creates a submodel from a JSON file file.
model.Property create_base_submodel_element_property(str|None id_short, model.datatypes type, Any value, str display_name="", str description="")
Create a basic SubmodelElement of type Property.
model.AssetAdministrationShell create_base_aas(str identifier, str id_short, str global_asset_identifier, str display_name="", str description="", model.AssetKind asset_kind=model.AssetKind.INSTANCE)
Create a basic AAS and add base asset information.
model.SpecificAssetId create_specific_asset_id(str name, str value, str|None external_subject_reference_value=None)
Create a new SpecificAssetId instance.
model.AssetAdministrationShell create_base_aas_with_asset_information(str identifier, str id_short, model.AssetInformation asset_information, str display_name="", str description="")
Create a basic AAS.
model.SubmodelElementCollection create_base_submodel_element_collection(str id_short, list[model.SubmodelElement] value, str display_name="", str description="")
Create a basic SubmodelElement of type SubmodelElementCollection.
model.AssetInformation create_base_asset_information(str global_asset_id, model.AssetKind asset_kind=model.AssetKind.INSTANCE, list[model.SpecificAssetId]|None specific_asset_id=None)
Return a basic AssetInformation instance.
model.EmbeddedDataSpecification|None create_embedded_data_specification(model.PreferredNameTypeIEC61360 preferred_name, model.DefinitionTypeIEC61360|None definition, model.ShortNameTypeIEC61360|None short_name, str|None unit, model.Reference|None unit_id, model.DataTypeIEC61360|None data_type, str|None source_of_definition, str|None symbol, str|None value_format, set[model.ValueReferencePair]|None value_list, str|None value, list[model.IEC61360LevelType]|None level_types)
Create an embedded data specification with the given parameters.
str create_unique_short_id(str id_short)
Generate a unique identifier string by appending a UUID to the provided ID short.
model.Submodel create_base_submodel(str identifier, str id_short, str display_name="", str description="")
Create a basic Submodel.
model.ModelReference create_reference(str id)
Create a ModelReference.