**************************** Read Only the Parts You Need **************************** The specification lets you read a submodel or its elements in different *forms*: for example, the whole submodel, only its metadata without the elements, or only the values of the elements. A smaller form saves the bandwidth, and gives you exactly what you need. Each form is a separate operation of the specification, such as ``GetSubmodelById-Metadata``, and the client gives each operation as a separate function, named after it, such as ``get_submodel_by_id_metadata``. On top of that, some operations take *modifiers*, which fine-tune the form: how deep the elements are given (``level``), and whether the content of the blobs is given (``extent``). Set Up ====== To see what the different forms leave out and what they keep, we first need something on the server to read. Hence, before the actual examples, we connect to the Submodel Repository and store a small submodel in it. We choose the submodel so that every form shows a difference: * The collection ``Temperature`` nests the property ``Value`` one level deeper, so we can see how deep the elements are given. * The blob ``Snapshot`` holds a few bytes of content, so we can see whether the content is given or left out. This chapter does not depend on the previous ones, so you can run its code on its own: .. code-block:: python import requests import aas_core3_2.types as aas_types from aas_client3_2 import ( http as aas_client_http, requests_transport as aas_client_requests_transport, submodel_repository as aas_client_submodel_repository, ) server = aas_client_http.Server( transport=aas_client_requests_transport.RequestsTransport( requests.Session() ), base_url="http://localhost:5005", ) submodel = aas_types.Submodel( id="https://example.com/ids/sm/getting-started/modifiers", id_short="Measurements", submodel_elements=[ aas_types.SubmodelElementCollection( id_short="Temperature", value=[ aas_types.Property( id_short="Value", value_type=aas_types.DataTypeDefXSD.DOUBLE, value="21.5", ), ], ), aas_types.Blob( id_short="Snapshot", content_type="application/octet-stream", value=b"\xde\xad\xbe\xef", ), ], ) aas_client_submodel_repository.post_submodel(server, submodel) Level and Extent ================ ``level`` tells how deep the elements are given: :py:attr:`aas_core3_2.types.Level.DEEP` (the default) gives all of them, and :py:attr:`~aas_core3_2.types.Level.CORE` only the direct children. ``extent`` tells whether the content of the blobs is given: :py:attr:`aas_core3_2.types.Extent.WITHOUT_BLOB_VALUE` (the default) leaves it out. .. note:: Watch out for the blobs! With the default extent, the blobs in your copy have no content. If you change such a copy and put it back to the server, for example, with :py:func:`~aas_client3_2.submodel_repository.put_submodel_by_id`, the server replaces the blobs as well, and their content is removed. Pass ``extent=aas_types.Extent.WITH_BLOB_VALUE`` when you get something with blobs which you mean to put back (see also :doc:`change`). With the extent ``withBlobValue``, the server gives the content of the blob ``Snapshot``: .. code-block:: python got = aas_client_submodel_repository.get_submodel_by_id( server, submodel.id, extent=aas_types.Extent.WITH_BLOB_VALUE ) # NOTE: The server may give the elements in any order, since # the elements of a submodel are unordered. snapshot = None for element in got.over_submodel_elements_or_empty(): if element.id_short == "Snapshot": snapshot = element assert isinstance(snapshot, aas_types.Blob) print(snapshot.value) # Prints: b'\xde\xad\xbe\xef' With the level core, the server gives the direct children of the submodel, but not their own children. Hence, the collection ``Temperature`` comes without the property ``Value``: .. code-block:: python core = aas_client_submodel_repository.get_submodel_by_id( server, submodel.id, level=aas_types.Level.CORE ) temperature = None for element in core.over_submodel_elements_or_empty(): if element.id_short == "Temperature": temperature = element assert isinstance(temperature, aas_types.SubmodelElementCollection) print(list(temperature.over_value_or_empty())) # Prints: [] Metadata, Reference, Path and Value =================================== Each of the following functions gives a different form of the same submodel. The metadata, everything except the values: .. code-block:: python metadata = aas_client_submodel_repository.get_submodel_by_id_metadata( server, submodel.id ) print(metadata.id_short) # Prints: Measurements The model reference to the submodel: .. code-block:: python reference = ( aas_client_submodel_repository.get_submodel_by_id_reference( server, submodel.id ) ) print(reference.keys[0].type.value, reference.keys[0].value) # Prints: Submodel https://example.com/ids/sm/getting-started/modifiers The ID-short paths of all the elements: .. code-block:: python paths = aas_client_submodel_repository.get_submodel_by_id_path( server, submodel.id ) print(sorted(paths)) # Prints: ['Snapshot', 'Temperature', 'Temperature.Value'] ... and the value-only representation, as JSON. The value-only representation is a compact form, defined by the specification, which keeps only the values of the elements, and leaves out everything else, such as the model types, the value types and the semantic IDs. The elements become the keys of a JSON object by their ID-shorts, and a collection becomes a nested JSON object, so the collection ``Temperature`` with its property ``Value`` becomes ``{"Temperature": {"Value": 21.5}}``. The values are given as JSON values, so the value ``"21.5"`` of the property, a string in the model, becomes the number ``21.5``. Since the value-only representation leaves out the types, it can not be turned back into the instances of :py:mod:`aas_core3_2.types` on its own, so the client gives you the JSON as it is: .. code-block:: python values = aas_client_submodel_repository.get_submodel_by_id_value_only( server, submodel.id ) print(values["Temperature"]) # Prints: {'Value': 21.5} Not every operation takes both modifiers. The specification allows for each operation only the modifiers which make sense for it, and the function takes exactly those: * :py:func:`~aas_client3_2.submodel_repository.get_submodel_by_id` and :py:func:`~aas_client3_2.submodel_repository.get_submodel_by_id_value_only` take both ``level`` and ``extent``. * :py:func:`~aas_client3_2.submodel_repository.get_submodel_by_id_path` takes only ``level``, since the paths contain no content of the blobs anyway. * :py:func:`~aas_client3_2.submodel_repository.get_submodel_by_id_metadata` and :py:func:`~aas_client3_2.submodel_repository.get_submodel_by_id_reference` take neither, since they leave out the elements altogether. The same forms exist for a single element by its path, for example: .. code-block:: python print( aas_client_submodel_repository.get_submodel_element_by_path_value_only( server, submodel.id, "Temperature.Value" ) ) # Prints: 21.5 Clean Up ======== Delete what we created in this chapter, so that you can run the code of this chapter again. The identifiers are unique, so the server would refuse to create the submodel a second time: .. code-block:: python aas_client_submodel_repository.delete_submodel_by_id( server, submodel.id )