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 not description:
68 description = f"This is the submodel element with ID short '{id_short}'"
69
70 description_text = {"en": f"{description}"}
71 sme.description = model.MultiLanguageTextType(description_text)
72
73 if not display_name:
74 display_name = "POC Submodel Element"
75
76 display_name_text = {"en": f"{display_name}"}
77 sme.display_name = model.MultiLanguageNameType(display_name_text)
78
79 return sme
80
81
83 id_short: str, value: list[model.SubmodelElement], display_name: str = "", description: str = ""
84) -> model.SubmodelElementCollection:
85 """Create a basic SubmodelElement of type SubmodelElementCollection."""
86 sme = model.SubmodelElementCollection(id_short=id_short, value=value)
87
88 if not description:
89 description = f"This is the submodel element with ID short '{id_short}'"
90
91 description_text = {"en": f"{description}"}
92 sme.description = model.MultiLanguageTextType(description_text)
93
94 if not display_name:
95 display_name = "POC Submodel Element"
96
97 display_name_text = {"en": f"{display_name}"}
98 sme.display_name = model.MultiLanguageNameType(display_name_text)
99
100 return sme
101
102
103def create_base_submodel(identifier: str, id_short: str, display_name: str = "", description: str = "") -> model.Submodel:
104 """Create a basic Submodel.
105
106 :param identifier: identifier of the Submodel
107 :param id_short: ID short of the Submodel
108 :param display_name: display name of the Submodel, defaults to ""
109 :param description: description of the Submodel, defaults to ""
110 :return: Submodel instance
111 """
112 sm = model.Submodel(identifier)
113 sm.id_short = id_short
115 if not description:
116 description = f"This is the submodel with ID short '{id_short}'"
117
118 description_text = {"en": f"{description}"}
119 sm.description = model.MultiLanguageTextType(description_text)
120
121 if not display_name:
122 display_name = "POC AAS"
123
124 display_name_text = {"en": f"{display_name}"}
125 sm.display_name = model.MultiLanguageNameType(display_name_text)
126
127 return sm
128
129
131 identifier: str, id_short: str, global_asset_identifier: str = "", display_name: str = "", description: str = ""
132) -> model.AssetAdministrationShell:
133 """Create a basic AAS.
134
135 :param identifier: identifier of the AAS
136 :param id_short: ID short of the AAS
137 :param global_asset_identifier: identifier of the global Asset
138 :param display_name: display name of the AAS, defaults to ""
139 :param description: description of the AAS, defaults to ""
140 :return: AssetAdministrationShell instance
141 """
142 if not global_asset_identifier:
143 global_asset_identifier = identifier
144
145 asset_info = create_base_asset_information(global_asset_identifier)
146
147 aas = model.AssetAdministrationShell(id_=identifier, asset_information=asset_info)
148 aas.id_short = id_short
149
150 if not description:
151 description = f"This is the asset administration shell with ID short '{id_short}'"
152
153 description_text = {"en": f"{description}"}
154 aas.description = model.MultiLanguageTextType(description_text)
155
156 if not display_name:
157 display_name = "POC AAS"
158
159 display_name_text = {"en": f"{display_name}"}
160 aas.display_name = model.MultiLanguageNameType(display_name_text)
161
162 return aas
163
164
165def create_base_asset_information(identifier: str) -> model.AssetInformation:
166 """Return a basic AssetInformation instance.
167
168 :param id_short: short ID of the AssetInformation
169 :param namespace: namespace of the AssetInformation, defaults to "basyx_python_aas_server"
170 :return: AssetInformation instance
171 """
172 return model.AssetInformation(model.AssetKind.INSTANCE, identifier)
173
175def create_reference(id: str) -> model.ModelReference:
176 """Create a ModelReference.
177
178 :param id: ID of the Submodel to reference
179 :return: ModelReference instance
180 """
181 return model.ModelReference.from_referable(model.Submodel(id))
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.AssetInformation create_base_asset_information(str identifier)
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.
str create_unique_short_id(str id_short)
Generate a unique identifier string by appending a UUID to the provided ID short.
model.AssetAdministrationShell create_base_aas(str identifier, str id_short, str global_asset_identifier="", str display_name="", str description="")
Create a basic AAS.
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.