forked from eclipse-basyx/basyx-python-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aasx.py: Rename a few functions and check the docstrings. tutorial_cr…
…eate_simple_aas.py: Implement tutorial comparable to the tutorial in the basyx python sdk
- Loading branch information
Showing
4 changed files
with
132 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,151 @@ | ||
import json | ||
import aas_core3.types as aas_types | ||
import aas_core3.jsonization as aas_jsonization | ||
from basyx.object_store import ObjectStore | ||
from basyx.aasx import AASXWriter, AASXReader, DictSupplementaryFileContainer | ||
import pyecma376_2 # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class. | ||
#!/usr/bin/env python3 | ||
""" | ||
Tutorial for exporting Asset Administration Shells with related objects and auxiliary files to AASX package files, using | ||
the :mod:`~basyx.aasx` module from the Eclipse BaSyx Python Framework. | ||
""" | ||
|
||
import datetime | ||
from pathlib import Path # Used for easier handling of auxiliary file's local path | ||
|
||
Referencetype = aas_types.ReferenceTypes("ModelReference") | ||
import pyecma376_2 # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class. | ||
from aas_core3 import types as model | ||
from basyx.aasx import AASXWriter, AASXReader, DictSupplementaryFileContainer | ||
from basyx.object_store import ObjectStore | ||
|
||
# step 1: Setting up an SupplementaryFileContainer and AAS & submodel with File objects | ||
# step 2: Writing AAS objects and auxiliary files to an AASX package | ||
# step 3: Reading AAS objects and auxiliary files from an AASX package | ||
|
||
|
||
######################################################################################## | ||
# Step 1: Setting up a SupplementaryFileContainer and AAS & submodel with File objects # | ||
######################################################################################## | ||
|
||
key_types = aas_types.KeyTypes("Submodel") | ||
# Let's first create a basic Asset Administration Shell with a simple submodel. | ||
# See `tutorial_create_simple_aas.py` for more details. | ||
submodel = model.Submodel(id='https://acplt.org/Submodel', submodel_elements=[]) | ||
|
||
key = aas_types.Key(value="some-unique-global-identifier", type=key_types) | ||
key_types = model.KeyTypes("Submodel") | ||
Referencetype = model.ReferenceTypes("ModelReference") | ||
key = model.Key(value='https://acplt.org/Submodel', type=key_types) | ||
reference = model.Reference(type=Referencetype, keys=[key]) | ||
|
||
reference = aas_types.Reference(type=Referencetype, keys=[key]) | ||
aas = model.AssetAdministrationShell(id='https://acplt.org/Simple_AAS', | ||
asset_information=model.AssetInformation( | ||
asset_kind=model.AssetKind.TYPE), | ||
submodels=[reference]) | ||
|
||
submodel = aas_types.Submodel( | ||
id="some-unique-global-identifier", | ||
submodel_elements=[ | ||
aas_types.Property( | ||
id_short="some_property", | ||
value_type=aas_types.DataTypeDefXSD.INT, | ||
value="1984", | ||
semantic_id=reference | ||
) | ||
] | ||
# Another submodel, which is not related to the AAS: | ||
unrelated_submodel = model.Submodel( | ||
id='https://acplt.org/Unrelated_Submodel' | ||
) | ||
|
||
# We add these objects to an ObjectStore for easy retrieval by id. | ||
object_store: ObjectStore = ObjectStore([unrelated_submodel, submodel, aas]) | ||
|
||
|
||
# For holding auxiliary files, which will eventually be added to an AASX package, we need a SupplementaryFileContainer. | ||
# The `DictSupplementaryFileContainer` is a simple SupplementaryFileContainer that stores the files' contents in simple | ||
# bytes objects in memory. | ||
file_store = DictSupplementaryFileContainer() | ||
|
||
# Now, we add an example file from our local filesystem to the SupplementaryFileContainer. | ||
# | ||
# For this purpose, we need to specify the file's name in the SupplementaryFileContainer. This name is used to reference | ||
# the file in the container and will later be used as the filename in the AASX package file. Thus, this file must begin | ||
# with a slash and should begin with `/aasx/`. Here, we use `/aasx/suppl/MyExampleFile.pdf`. The | ||
# SupplementaryFileContainer's add_file() method will ensure uniqueness of the name by adding a suffix if an equally | ||
# named file with different contents exists. The final name is returned. | ||
# | ||
# In addition, we need to specify the MIME type of the file, which is later used in the metadata of the AASX package. | ||
# (This is actually a requirement of the underlying Open Packaging Conventions (ECMA376-2) format, which imposes the | ||
# specification of the MIME type ("content type") of every single file within the package.) | ||
|
||
with open(Path(__file__).parent / 'data' / 'TestFile.pdf', 'rb') as f: | ||
actual_file_name = file_store.add_file("/aasx/suppl/MyExampleFile.pdf", f, "application/pdf") | ||
|
||
if submodel.submodel_elements is not None: | ||
submodel.submodel_elements.append(aas_types.File(id_short="documentationFile", | ||
content_type="application/pdf", | ||
value=actual_file_name)) | ||
|
||
aas = aas_types.AssetAdministrationShell(id="urn:x-test:aas1", | ||
asset_information=aas_types.AssetInformation( | ||
asset_kind=aas_types.AssetKind.TYPE), | ||
submodels=[reference]) | ||
# With the actual_file_name in the SupplementaryFileContainer, we can create a reference to that file in our AAS | ||
# Submodel, in the form of a `File` object: | ||
|
||
obj_store: ObjectStore = ObjectStore() | ||
obj_store.add(aas) | ||
obj_store.add(submodel) | ||
model.File(id_short="documentationFile", content_type="application/pdf", value=actual_file_name) | ||
|
||
file = model.File(id_short="documentationFile", content_type="application/pdf", value=actual_file_name) | ||
if submodel.submodel_elements is not None: | ||
submodel.submodel_elements.append(file) | ||
|
||
# Serialize to a JSON-able mapping | ||
jsonable = aas_jsonization.to_jsonable(submodel) | ||
|
||
###################################################################### | ||
# Step 2: Writing AAS objects and auxiliary files to an AASX package # | ||
###################################################################### | ||
|
||
meta_data = pyecma376_2.OPCCoreProperties() | ||
meta_data.creator = "Chair of Process Control Engineering" | ||
meta_data.created = datetime.datetime.now() | ||
# After setting everything up in Step 1, writing the AAS, including the Submodel objects and the auxiliary file | ||
# to an AASX package is simple. | ||
|
||
# Open an AASXWriter with the destination file name and use it as a context handler, to make sure it is properly closed | ||
# after doing the modifications: | ||
with AASXWriter("./MyAASXPackage.aasx") as writer: | ||
writer.write_aas(aas_ids=["urn:x-test:aas1"], | ||
object_store=obj_store, | ||
file_store=file_store, | ||
write_json=False) | ||
# Write the AAS and everything belonging to it to the AASX package | ||
# The `write_aas()` method will automatically fetch the AAS object with the given id | ||
# and all referenced Submodel objects from the ObjectStore. It will also scan every object for | ||
# semanticIds referencing ConceptDescription, fetch them from the ObjectStore, and scan all sbmodels for `File` | ||
# objects and fetch the referenced auxiliary files from the SupplementaryFileContainer. | ||
# In order to add more than one AAS to the package, we can simply add more Identifiers to the `aas_ids` list. | ||
# | ||
# ATTENTION: As of Version 3.0 RC01 of Details of the Asset Administration Shell, it is no longer valid to add more | ||
# than one "aas-spec" part (JSON/XML part with AAS objects) to an AASX package. Thus, `write_aas` MUST | ||
# only be called once per AASX package! | ||
writer.write_aas(aas_ids=['https://acplt.org/Simple_AAS'], | ||
object_store=object_store, | ||
file_store=file_store) | ||
|
||
# Alternatively, we can use a more low-level interface to add a JSON/XML part with any Identifiable objects (not | ||
# only an AAS and referenced objects) in the AASX package manually. `write_aas_objects()` will also take care of | ||
# adding referenced auxiliary files by scanning all submodel objects for contained `File` objects. | ||
# | ||
# ATTENTION: As of Version 3.0 RC01 of Details of the Asset Administration Shell, it is no longer valid to add more | ||
# than one "aas-spec" part (JSON/XML part with AAS objects) to an AASX package. Thus, `write_all_aas_objects` SHALL | ||
# only be used as an alternative to `write_aas` and SHALL only be called once! | ||
objects_to_be_written: ObjectStore[model.Identifiable] = ObjectStore([unrelated_submodel]) | ||
writer.write_all_aas_objects(part_name="/aasx/my_aas_part.xml", | ||
objects=objects_to_be_written, | ||
file_store=file_store) | ||
|
||
# We can also add a thumbnail image to the package (using `writer.write_thumbnail()`) or add metadata: | ||
meta_data = pyecma376_2.OPCCoreProperties() | ||
meta_data.creator = "Chair of Process Control Engineering" | ||
meta_data.created = datetime.datetime.now() | ||
writer.write_core_properties(meta_data) | ||
|
||
# Closing the AASXWriter will write some required parts with relationships and MIME types to the AASX package file and | ||
# close the package file afterward. Make sure, to always call `AASXWriter.close()` or use the AASXWriter in a `with` | ||
# statement (as a context manager) as shown above. | ||
|
||
|
||
######################################################################## | ||
# Step 3: Reading AAS objects and auxiliary files from an AASX package # | ||
######################################################################## | ||
|
||
# Let's read the AASX package file, we have just written. | ||
# We'll use a fresh ObjectStore and SupplementaryFileContainer to read AAS objects and auxiliary files into. | ||
new_object_store: ObjectStore = ObjectStore() | ||
new_file_store = DictSupplementaryFileContainer() | ||
|
||
with AASXReader("./MyAASXPackage.aasx") as reader: | ||
# Again, we need to use the AASXReader as a context manager (or call `.close()` in the end) to make sure the AASX | ||
# package file is properly closed when we are finished. | ||
with AASXReader("MyAASXPackage.aasx") as reader: | ||
# Read all contained AAS objects and all referenced auxiliary files | ||
reader.read_into(object_store=new_object_store, | ||
file_store=new_file_store) | ||
|
||
print(new_object_store.__len__()) | ||
for item in file_store.__iter__(): | ||
print(item) | ||
# We can also read the metadata | ||
new_meta_data = reader.get_core_properties() | ||
|
||
# We could also read the thumbnail image, using `reader.get_thumbnail()` | ||
|
||
|
||
for item in new_file_store.__iter__(): | ||
print(item) | ||
# Some quick checks to make sure, reading worked as expected | ||
assert 'https://acplt.org/Submodel' in new_object_store | ||
assert actual_file_name in new_file_store | ||
assert new_meta_data.creator == "Chair of Process Control Engineering" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters