AAS HTTP Client Documentation
Loading...
Searching...
No Matches
model_builder.py
Go to the documentation of this file.
1"""Model builder module.
2
3Provides some helper methods for easier work with basyx sdk data model
4"""
5
6import json
7import logging
8import uuid
9from pathlib import Path
10from typing import Any
11
12from basyx.aas import model
13
14from aas_http_client.utilities import sdk_tools
15
16_logger = logging.getLogger(__name__)
17
18
19def create_unique_short_id(id_short: str) -> str:
20 """Generate a unique identifier string by appending a UUID to the provided ID short.
21
22 :param id_short: provided ID short
23 :return: unique identifier
24 """
25 return f"{id_short}_{str(uuid.uuid4()).replace('-', '_')}"
26
28def create_submodel_from_file(json_file: str = "") -> model.Submodel:
29 """Creates a submodel from a JSON file file.
30
31 :param id: ID of the submodel. If empty, the template's ID is used
32
33 """
34 # Define the path to the submodel template file
35 sm_template_file = Path(json_file).resolve()
37 # Check if the template file exists
38 if not sm_template_file.exists():
39 raise FileNotFoundError(f"Submodel template file not found: {sm_template_file}")
40
41 submodel_data = {}
42 try:
43 # Load the template JSON file
44 with Path.open(sm_template_file, "r", encoding="utf-8") as f:
45 submodel_data = json.load(f)
46
47 # Load the template JSON into a Submodel object
48 submodel = sdk_tools.convert_to_object(submodel_data)
49
50 # Ensure the loaded template is indeed a Submodel
51 if not isinstance(submodel, model.Submodel):
52 raise TypeError("Loaded template JSON structure is not a Submodel.")
53
54 except Exception as e:
55 _logger.error(f"Error loading submodel template: {e}")
56 raise e
57
58 return submodel
59
60
62 id_short: str | None, type: model.datatypes, value: Any, display_name: str = "", description: str = ""
63) -> model.Property:
64 """Create a basic SubmodelElement of type Property."""
65 sme = model.Property(id_short=id_short, value_type=type, value=value)
66
67 if description:
68 description_text = {"en": f"{description}"}
69 sme.description = model.MultiLanguageTextType(description_text)
70
71 if display_name:
72 display_name_text = {"en": f"{display_name}"}
73 sme.display_name = model.MultiLanguageNameType(display_name_text)
74
75 return sme
76
77
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)
83
84 if description:
85 description_text = {"en": f"{description}"}
86 sme.description = model.MultiLanguageTextType(description_text)
87
88 if display_name:
89 display_name_text = {"en": f"{display_name}"}
90 sme.display_name = model.MultiLanguageNameType(display_name_text)
91
92 return sme
93
94
95def create_base_submodel(identifier: str, id_short: str, display_name: str = "", description: str = "") -> model.Submodel:
96 """Create a basic Submodel.
97
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
103 """
104 sm = model.Submodel(identifier)
105 sm.id_short = id_short
107 if description:
108 description_text = {"en": f"{description}"}
109 sm.description = model.MultiLanguageTextType(description_text)
110
111 if display_name:
112 display_name_text = {"en": f"{display_name}"}
113 sm.display_name = model.MultiLanguageNameType(display_name_text)
114
115 return sm
116
117
119 identifier: str,
120 id_short: str,
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.
127
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
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
135 """
136 if not global_asset_identifier:
137 global_asset_identifier = identifier
138
139 asset_info = create_base_asset_information(global_asset_identifier, asset_kind)
140
141 aas = model.AssetAdministrationShell(id_=identifier, asset_information=asset_info)
142 aas.id_short = id_short
143
144 if description:
145 description_text = {"en": f"{description}"}
146 aas.description = model.MultiLanguageTextType(description_text)
147
148 if display_name:
149 display_name_text = {"en": f"{display_name}"}
150 aas.display_name = model.MultiLanguageNameType(display_name_text)
151
152 return aas
153
154
155def create_base_asset_information(identifier: str, asset_kind: model.AssetKind = model.AssetKind.INSTANCE) -> model.AssetInformation:
156 """Return a basic AssetInformation instance.
157
158 :param identifier: identifier of the AssetInformation
159 :param asset_kind: kind of the Asset, defaults to AssetKind.INSTANCE
160 :return: AssetInformation instance
161 """
162 return model.AssetInformation(asset_kind, identifier)
163
165def create_reference(id: str) -> model.ModelReference:
166 """Create a ModelReference.
167
168 :param id: ID of the Submodel to reference
169 :return: ModelReference instance
170 """
171 return model.ModelReference.from_referable(model.Submodel(id))
172
175 preferred_name: model.PreferredNameTypeIEC61360,
176 definition: model.DefinitionTypeIEC61360 | None,
177 short_name: model.ShortNameTypeIEC61360 | None,
178 unit: str | None,
179 unit_id: model.Reference | None,
180 data_type: model.DataTypeIEC61360 | None,
181 source_of_definition: str | None,
182 symbol: str | None,
183 value_format: str | None,
184 value_list: set[model.ValueReferencePair] | None,
185 value: str | None,
186 level_types: list[model.IEC61360LevelType] | None,
187) -> model.EmbeddedDataSpecification | None:
188 """Create an embedded data specification with the given parameters.
189
190 :param preferred_name: The preferred name of the concept description.
191 :param definition: The definition of the concept description.
192 :param short_name: The short name of the concept description.
193 :param unit: The unit of the concept description.
194 :param unit_id: The unit reference of the concept description.
195 :param data_type: The data type of the concept description.
196 :param source_of_definition: The source of definition of the concept description.
197 :param symbol: The symbol of the concept description.
198 :param value_format: The value format of the concept description.
199 :param value_list: The value list of the concept description.
200 :param value: The value of the concept description.
201 :param level_types: The level types of the concept description.
202 :return: An instance of EmbeddedDataSpecification if successful, None otherwise.
203 """
204 if not preferred_name:
205 _logger.warning("Preferred name is missing for the embedded data specification.")
206 return None
207
208 return model.EmbeddedDataSpecification(
209 data_specification=model.ExternalReference(
210 (
211 model.Key(
212 type_=model.KeyTypes.GLOBAL_REFERENCE, value="https://admin-shell.io/DataSpecificationTemplates/DataSpecificationIEC61360/3/1"
213 ),
214 )
215 ),
216 data_specification_content=model.DataSpecificationIEC61360(
217 preferred_name=preferred_name,
218 definition=definition,
219 short_name=short_name,
220 unit=unit,
221 unit_id=unit_id,
222 data_type=data_type,
223 source_of_definition=source_of_definition,
224 symbol=symbol,
225 value_format=value_format,
226 value_list=value_list,
227 value=value,
228 level_types=level_types or (),
229 ),
230 )
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.
model.AssetInformation create_base_asset_information(str identifier, model.AssetKind asset_kind=model.AssetKind.INSTANCE)
Return a basic AssetInformation instance.
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.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.