eclipse-basyx / basyx-python-sdk

MIT License
61 stars 28 forks source link

Parsing AssetAdministrationShell fails when parsing `asset` element #9

Closed the-nic closed 2 years ago

the-nic commented 2 years ago

When parsing an AssetAdministrationShell from json _construct_asset_administration_shell is expecting a type dict in asset element:

def _construct_asset_administration_shell(
            cls, dct: Dict[str, object], object_class=model.AssetAdministrationShell) -> model.AssetAdministrationShell:
        ret = object_class(
            asset=cls._construct_aas_reference(_get_ts(dct, 'asset', dict), model.Asset),
            identification=cls._construct_identifier(_get_ts(dct, 'identification', dict)))

However, at that point the asset element is already parsed and fails with:

TypeError: Dict entry 'asset' has unexpected type Asset

Changing the above line to

def _construct_asset_administration_shell(
            cls, dct: Dict[str, object], object_class=model.AssetAdministrationShell) -> model.AssetAdministrationShell:
        ret = object_class(
            asset=get_ts(dct, 'asset', model.Asset),
            identification=cls._construct_identifier(_get_ts(dct, 'identification', dict)))

seems to fix the problem.

Example python code:

    json_ = """
[
    {
        "conceptDictionary": [],
        "assetRef": {
            "keys": [
                {
                    "idType": "IRI",
                    "type": "Asset",
                    "value": "1030_8141_0112_5510",
                    "local": true
                }
            ]
        },
        "identification": {
            "idType": "IRI",
            "id": "1220_8141_0112_0875"
        },
        "idShort": "AAS",
        "dataSpecification": [],
        "derivedFrom": {
            "keys": [
                {
                    "idType": "IdShort",
                    "type": "AssetAdministrationShell",
                    "value": "6455_1111_0112_4769",
                    "local": true
                }
            ]
        },
        "modelType": {
            "name": "AssetAdministrationShell"
        },
        "asset": {
            "idShort": "CA",
            "modelType": {
                "name": "Asset"
            },
            "identification": {
                "id": "1030_8141_0112_5510",
                "idType": "IRI"
            },
            "kind": "Instance"
        },
        "embeddedDataSpecifications": [],
        "views": []
    }
]
    """
    shells = json.loads(json_, cls=basyx.aas.adapter.json.AASFromJsonDecoder)
jkhsjdhjs commented 2 years ago

Yes, it's retrieving the value of the asset property as a dict in order to construct a Reference object from the dict. In the DotAAS metamodel, references to other objects are described by Reference objects, which point to the actual object. This is also the case for the asset property of an AssetAdministrationShell, you can verify it on page 234 of the spec: https://www.plattform-i40.de/IP/Redaktion/EN/Downloads/Publikation/Details_of_the_Asset_Administration_Shell_Part1_V2.pdf?__blob=publicationFile

Thus, the exemplary JSON structure you gave doesn't adhere to the DotAAS spec in version 2.0.1 and isn't compatible with this implementation.

the-nic commented 2 years ago

Thank you very much for the explanation!