Change the Shell and its Submodels
We continue with the shell and the submodels from the previous chapter, Create a Shell with Submodels.
Change a Submodel Element
The elements of a submodel are addressed by their ID-short paths, such as MaxRotationSpeed, Dimensions.Height for an element in a collection, or Documents[0] for an item of a list.
Since the square brackets are not allowed in a path of a URL, the specification requires that an ID-short path in a path is URL-encoded (percent-encoded), so that Documents[0] becomes Documents%5B0%5D.
The client does this encoding for you, so you always pass the ID-short paths as they are.
Before we change anything, keep in mind what the objects in your program are. When you get an element from the server, you get a copy of it: a plain Python object, which knows nothing about the server it came from. If you change the copy, nothing happens on the server. The client is not an object mapper (such as an ORM for databases) which keeps your objects in sync with the server behind the scenes.
This follows the REST style, on which the API of the specification is built. Each request is stateless: it carries everything the server needs, and the server keeps no session for your program between the requests. To change something on the server, you send the changed data back in a request of its own.
Hence, to change an element, get it, change it, and put it back:
element = aas_client_submodel_repository.get_submodel_element_by_path(
submodel_repository_server,
# The identifier of the submodel which contains the element
specification.id,
# The ID-short path of the element within the submodel. The property
# MaxRotationSpeed sits at the top level of the submodel, so its path
# is just its ID-short.
"MaxRotationSpeed",
)
assert isinstance(element, aas_types.Property)
element.value = "5500"
aas_client_submodel_repository.put_submodel_element_by_path(
submodel_repository_server,
specification.id,
"MaxRotationSpeed",
element,
)
Change Only the Values
If you only want to change the values, send the element in the value-only serialization. You still pass a typed element; the client serializes only its value:
aas_client_submodel_repository.patch_submodel_element_by_path_value_only(
submodel_repository_server,
specification.id,
"Weight",
aas_types.Property(
id_short="Weight",
value_type=aas_types.DataTypeDefXSD.DOUBLE,
value="12.7",
),
)
The value-only serialization of a whole submodel comes back as JSON, since it can only be interpreted together with the submodel:
values = aas_client_submodel_repository.get_submodel_by_id_value_only(
submodel_repository_server, specification.id
)
print(values["MaxRotationSpeed"], values["Weight"])
# Prints: 5500.0 12.7
All the numbers in the value-only JSON come back as floats.
Add and Remove Elements
Add a new element at the top level of the submodel:
aas_client_submodel_repository.post_submodel_element(
submodel_repository_server,
specification.id,
aas_types.SubmodelElementCollection(
id_short="Dimensions",
value=[
aas_types.Property(
id_short="Length",
value_type=aas_types.DataTypeDefXSD.DOUBLE,
value="0.35",
),
],
),
)
Add an element into the collection by its path:
aas_client_submodel_repository.post_submodel_element_by_path(
submodel_repository_server,
specification.id,
"Dimensions",
aas_types.Property(
id_short="Height",
value_type=aas_types.DataTypeDefXSD.DOUBLE,
value="0.2",
),
)
And remove it again:
aas_client_submodel_repository.delete_submodel_element_by_path(
submodel_repository_server, specification.id, "Dimensions.Height"
)
Replace a Whole Submodel
To change many things at once, get the submodel, change it, and put it back. The server replaces the submodel as a whole:
More useful: You send one request instead of many, however many elements you change. Your changes arrive together, so the submodel is not left half-changed, as it would be if one of many individual requests failed midway. You can also change what no ID-short path reaches, such as the description or the semantic ID of the submodel itself, and add, remove and reorder many elements in one go.
Less useful: You transfer the whole submodel twice, even if you change only a single value, which is slow for large submodels. What you put back overwrites everything on the server: if another program changed an element between your get and your put, its change is lost, even if you did not touch that element. Changing individual elements keeps this risk to the elements you actually change.
Note
Watch out for the blobs!
By default, the server leaves out the values of the blobs when you get a submodel (the extent withoutBlobValue, see Read Only the Parts You Need).
If you put such a submodel back, the blobs lose their values.
Pass extent=aas_types.Extent.WITH_BLOB_VALUE when you get a submodel with blobs which you mean to put back.
Our submodel is small and has no blobs, so we simply replace it:
a_submodel = aas_client_submodel_repository.get_submodel_by_id(
submodel_repository_server, identification.id
)
a_submodel.description = [
aas_types.LangStringTextType(
language="en", text="Identification of the motor"
)
]
aas_client_submodel_repository.put_submodel_by_id(
submodel_repository_server, identification.id, a_submodel
)
Change the Shell
The shell is changed the same way, in the AAS Repository:
a_shell = (
aas_client_aas_repository.get_asset_administration_shell_by_id(
aas_repository_server, shell.id
)
)
a_shell.display_name = [
aas_types.LangStringNameType(language="en", text="Motor SN-0042")
]
aas_client_aas_repository.put_asset_administration_shell_by_id(
aas_repository_server, shell.id, a_shell
)
Add a Submodel to the Shell
Create the submodel in the Submodel Repository, and add the reference to it to the shell:
documentation = aas_types.Submodel(
id="https://example.com/ids/sm/getting-started/documentation",
id_short="Documentation",
)
aas_client_submodel_repository.post_submodel(
submodel_repository_server, documentation
)
aas_client_aas_repository.post_submodel_reference(
aas_repository_server,
shell.id,
aas_types.Reference(
type=aas_types.ReferenceTypes.MODEL_REFERENCE,
keys=[
aas_types.Key(
type=aas_types.KeyTypes.SUBMODEL,
value=documentation.id,
)
],
),
)
page = aas_client_aas_repository.get_all_submodel_references(
aas_repository_server, shell.id
)
for reference in page.over_result_or_empty():
print(reference.keys[0].value)
# Prints:
# https://example.com/ids/sm/getting-started/identification
# https://example.com/ids/sm/getting-started/specification
# https://example.com/ids/sm/getting-started/documentation
Remove a Submodel from the Shell
Remove the reference from the shell, and the submodel from the repository:
aas_client_aas_repository.delete_submodel_reference(
aas_repository_server, shell.id, documentation.id
)
aas_client_submodel_repository.delete_submodel_by_id(
submodel_repository_server, documentation.id
)
Clean Up
Finally, delete the shell and its submodels, so that you can run the code of this chapter and the previous one again. The identifiers are unique, so the server would refuse to create the shell and the submodels a second time:
aas_client_aas_repository.delete_asset_administration_shell_by_id(
aas_repository_server, shell.id
)
aas_client_submodel_repository.delete_submodel_by_id(
submodel_repository_server, identification.id
)
aas_client_submodel_repository.delete_submodel_by_id(
submodel_repository_server, specification.id
)
Deleting the shell does not delete its submodels, since they live in a separate repository, and might be referred to by other shells as well.