tutorial_storage.py 5.33 KB
Newer Older
1
#!/usr/bin/env python3
2
3
4
# This work is licensed under a Creative Commons CCZero 1.0 Universal License.
# See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
"""
5
6
Tutorial for storing Asset Administration Shells, Submodels and Assets in an ObjectStore and using it for fetching these
objects by identification and resolving references.
7
8
"""

9
10
11
12
13
14
15
16
17
18
19
20
21
# For managing a larger number of Identifiable AAS objects (AssetAdministrationShells, Assets, Submodels,
# ConceptDescriptions), the PyI40AAS library provides the `ObjectStore` functionality. This tutorial shows the basic
# features of an ObjectStore and how to use them. This includes usage of the built-in `resolve()` method of Reference
# objects, which can be used to easily get the Submodel objects, which are referenced by the
# `AssetAdministrationShell.submodel` set, etc.
#
# Step by Step Guide:
# step 1: creating Asset, Submodel and Asset Administration Shell objects
# step 2: storing the data in an ObjectStore for easier handling
# step 3: retrieving objects from the store by their identifier
# step 4: using the ObjectStore to resolve a reference


22
23
from aas import model
from aas.model import Asset, AssetAdministrationShell, Submodel
24

25
26
27
28
29
30
31

###########################################################################
# Step 1: Creating Asset, Submodel and Asset Administration Shell objects #
###########################################################################

# For more details, take a look at `tutorial_create_simple_aas.py`

32
33
34
35
asset = Asset(
    kind=model.AssetKind.INSTANCE,
    identification=model.Identifier('https://acplt.org/Simple_Asset', model.IdentifierType.IRI)
)
36
37
38
39
40
41
42
43
44
45
46
47
48
prop = model.Property(
    id_short='ExampleProperty',
    value_type=model.datatypes.String,
    value='exampleValue',
    semantic_id=model.Reference(
        (model.Key(
            type_=model.KeyElements.GLOBAL_REFERENCE,
            local=False,
            value='http://acplt.org/Properties/SimpleProperty',
            id_type=model.KeyType.IRI
        ),)
    )
)
49
50
submodel = Submodel(
    identification=model.Identifier('https://acplt.org/Simple_Submodel', model.IdentifierType.IRI),
51
    submodel_element={prop}
52
53
54
55
)
aas = AssetAdministrationShell(
    identification=model.Identifier('https://acplt.org/Simple_AAS', model.IdentifierType.IRI),
    asset=model.AASReference.from_referable(asset),
56
    submodel={model.AASReference.from_referable(submodel)}
57
58
)

59
60
61
62
63
64
65
66
67
68
69

##################################################################
# step 2: Storing the Data in an ObjectStore for Easier Handling #
##################################################################

# step 2.1: create an ObjectStore for identifiable objects
#
# In this tutorial, we use a `DictObjectStore`, which is a simple in-memory store: It just keeps track of the Python
# objects using a dict.
# This may not be a suitable solution, if you need to manage large numbers of objects or objects must kept in a
# persistent memory (i.e. on hard disk). In this case, you may chose the `CouchDBObjectStore` from
70
71
72
73
# `aas.backends.couchdb` to use a CouchDB database server as persistent storage. Both ObjectStore implementations
# provide the same interface. In addition, the CouchDBObjectStores allows synchronizing the local object with the
# database via a Backend and the update()/commit() mechanism. See the `tutorial_backend_couchdb.py` for more
# information.
74
obj_store: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
75

TorbenD's avatar
TorbenD committed
76
# step 2.2: add asset, submodel and asset administration shell to store
77
78
79
80
obj_store.add(asset)
obj_store.add(submodel)
obj_store.add(aas)

81
82
83
84
85
86

#################################################################
# step 3: Retrieving Objects From the Store by Their Identifier #
#################################################################

tmp_submodel = obj_store.get_identifiable(
87
    model.Identifier('https://acplt.org/Simple_Submodel', model.IdentifierType.IRI))
TorbenD's avatar
TorbenD committed
88

89
90
91
92
93
94
assert(submodel is tmp_submodel)


########################################################
# step 4: Using the ObjectStore to Resolve a Reference #
########################################################
TorbenD's avatar
TorbenD committed
95

96
97
98
99
100
101
102
103
104
105
106
# The `aas` object already contains a reference to the submodel.
# Let's create a list of all submodels, to which the AAS has references, by resolving each of the submodel references:
submodels = [reference.resolve(obj_store)
             for reference in aas.submodel]

# The first (and only) element of this list should be our example submodel:
assert(submodel is tmp_submodel)

# Now, let's manually create a reference to the Property within the submodel. The reference uses two keys, the first one
# identifying the submodel by its identification, the second one resolving to the Property within the submodel by its
# idShort.
TorbenD's avatar
TorbenD committed
107
108
109
110
111
property_reference = model.AASReference(
    (model.Key(
        type_=model.KeyElements.SUBMODEL,
        local=True,
        value='https://acplt.org/Simple_Submodel',
112
        id_type=model.KeyType.IRI),
TorbenD's avatar
TorbenD committed
113
114
115
116
     model.Key(
         type_=model.KeyElements.PROPERTY,
         local=True,
         value='ExampleProperty',
117
118
         id_type=model.KeyType.IDSHORT),
     ),
119
    target_type=model.Property
TorbenD's avatar
TorbenD committed
120
)
121
122
123
124

# Now, we can resolve this new reference.
# The `resolve()` method will fetch the Submodel object from the ObjectStore, traverse down to the included Property
# object and return this object.
TorbenD's avatar
TorbenD committed
125
tmp_property = property_reference.resolve(obj_store)
126
assert(prop is tmp_property)