Skip to main content

aas-pydantic is a package that combines the basyx python sdk for handling Asset Administration Shells (AAS) with pydantic Basemodels for fast and easy modeling and transformation of pydantic models and AAS.

Project description

AAS Pydantic

Build-sucess PyPI PyPI - Python Version License: MIT

A Python package that combines the BaSyx Python SDK for handling Asset Administration Shells (AAS) with Pydantic models for fast and easy modeling and transformation of Industry 4.0 data structures.

The package enables:

  • Creating AAS models using Pydantic's intuitive syntax
  • Converting between Pydantic models and BaSyx AAS instances
  • Converting IDTA submodel templates to Pydantic types and vice versa
  • Serialization to JSON/Schema formats for easy integrations with other tools

Installation

You can install the package using pip:

pip install aas-pydantic

Note that the package requires python 3.10 or higher.

Usage

The package provides a set of Pydantic models that can be used to create AAS models. The models are based on the BaSyx SDK and are designed to be easily converted to and from AAS instances.

In the following example, we create a simple AAS of a Device with one Submodel DeviceConfig.

from typing import List, Set, Union
from enum import Enum
from aas_pydantic import AAS, Submodel, SubmodelElementCollection

# Define custom enums
class StatusEnum(str, Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"

# Define SubmodelElementCollections
class DeviceProperties(SubmodelElementCollection):
    serial_number: str
    firmware_version: str
    status: StatusEnum
    temperature_sensors: List[str]
    config_params: Set[str]

# Define Submodels
class DeviceConfig(Submodel):
    id_short: str
    description: str
    properties: DeviceProperties
    measurements: List[float]
    settings: Union[str, int]

# Create AAS class
class DeviceAAS(AAS):
    device_info: DeviceConfig

We also want to create an instance of this model, which we will use later:

device = DeviceAAS(
    id="device_1",
    description="Example device",
    device_info=DeviceConfig(
        id_short="device_1_config",
        description="Device 1 Configuration",
        properties=DeviceProperties(
            id_short="device_1_properties",
            serial_number="1234",
            firmware_version="1.0",
            status=StatusEnum.ACTIVE,
            temperature_sensors=["sensor_1", "sensor_2"],
            config_params={"param_1", "param_2"}
        ),
        measurements=[1.0, 2.0, 3.0],
        settings="default"
    )
)

Model Conversion

The package provides methods for converting between Pydantic models and BaSyx models. This works both for pydantic types / basyx templates and instances.

Convert Pydantic model to BaSyx templates

At first, we want to show an submodel template can be created from the modelled type:

submodel_template = aas_pydantic.convert_model_to_submodel_template(DeviceConfig)

For inspection of the submodel template, we can serialize it to a file:

import basyx.aas.adapter.json

with open("submodel_template_DeviceConfig.json", "w") as f:
    json_string = json.dumps(
        submodel_template,
        cls=basyx.aas.adapter.json.AASToJsonEncoder,
    )
    f.write(json_string)

We can also convert a whole AAS type to a Basyx Object Store:

aas_template_objectstore = aas_pydantic.convert_model_to_aas_template(DeviceAAS)

We can also serialize the AAS template to a file:

with open("aas_template_DeviceAAS.json", "w") as f:
    basyx.aas.adapter.json.write_aas_json_file(
        f, aas_template_objectstore
    )

Convert Pydantic model instance to BaSyx instance

We can also convert an instance of the Pydantic model to a BaSyx instance:

basyx_objectstore = aas_pydantic.convert_model_to_aas(example_device)

When we convert a pydantic AAS model to a basyx instance, we obtain an object store, holding the AAS and its submodels. We can serialize this object store to a file:

with open("aas_instance_DeviceAAS.json", "w") as f:
    basyx.aas.adapter.json.write_aas_json_file(f, basyx_objectstore)

Only converting a single submodel instance is also possible:

basyx_submodel = aas_pydantic.convert_model_to_submodel(example_device.device_info)

Convert BaSyx templates to pydantic types

In aas-pydantic you can create pydantic types from BaSyx templates at runtime. This can be useful when you want to create a Pydantic model from an existing AAS data structure.

pydantic_aas_types = aas_pydantic.convert_object_store_to_pydantic_types(
    aas_template_objectstore
)
pydantic_submodel_type = aas_pydantic.convert_submodel_template_to_pydatic_type(
    submodel_template
)
print(pydantic_submodel_type.model_fields)

This conversion significantly reduces data complexity since relying of flat object structures instead of the AAS Meta Model. Moreover, since pydantic has many integration, such as JSonSchema or fastAPI, this transformation can be useful for many use cases.

Convert BaSyx instances to Pydantic instances

In aas-pydantic you can also convert BaSyx instances to Pydantic instances. This can be useful when you want to work with Pydantic models in your application, but need to convert them to BaSyx instances for communication with other systems.

For conversion of the instances, you require the previously generated Pydantic types:

pydantic_submodel = aas_pydantic.convert_submodel_to_model_instance(basyx_submodel, model_type=pydantic_submodel_type)
pydantic_aas = aas_pydantic.convert_object_store_to_pydantic_models(
    basyx_objectstore, pydantic_aas_types
)
print(pydantic_aas)

Working with IDTA Submodel Templates

Previously, we have shown how submodel templates can be loaded. Of course, this is also possible for submodel templates obtained from IDTA. For example, we can load to submodel template for Data Model for Asset Location (Version 1.0:

import aas_pydantic
import basyx.aas.adapter.json

with open(
    "IDTA 02045-1-0_Template_DataModelForAssetLocation.json", "r"
) as file:
    basyx_object_store = basyx.aas.adapter.json.read_aas_json_file(file)
pydantic_types = aas_pydantic.convert_object_store_to_pydantic_types(basyx_object_store)
with open("pydantic_types.json", "w") as f:
    json.dump(pydantic_types[0].model_json_schema(), f, indent=2)

This example shows, how simple integration between Submodel templates and JSON Schema can be achieved. The geneated JSON Schema could, e.g. be used in code generation tools for pydantic models to have defined pydantic types for the submodel templates. For more integration, check out aas-middleware, a python package for industrial data and software integration based on AAS.

Conversion Capabilities

The package provides the following conversion of python type annotations:

  • Primitive types (int, float, str, bool)
  • Collections (List, Set, Tuples)
  • Enums and Literals
  • Unions and Optional types
  • Nested Pydantic models

Note that dicts are not supported, since a clear distinction from SubmodelElementCollection to the dict or pydantic type is not possible.

The following BaSyx Submodel Element Types are supported:

  • SubmodelElementCollection
  • SubmodelElementList
  • ReferenceElement
  • RelationshipElement
  • Property
  • Blob
  • File

During conversion, the following information will be preserved:

  • Identifiers
  • Descriptions
  • Semantic Identifiers
  • Values and Value Types

Storage of additional information that cannot be kept easily in the BaSyx model (attribute and class names, optional and union types, enums) will be stored in the Concept Descriptions of the BaSyx model.

License

The package is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aas_pydantic-0.1.2.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aas_pydantic-0.1.2-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file aas_pydantic-0.1.2.tar.gz.

File metadata

  • Download URL: aas_pydantic-0.1.2.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Windows/10

File hashes

Hashes for aas_pydantic-0.1.2.tar.gz
Algorithm Hash digest
SHA256 29d2d20ce895cfcb36b21599276be7c5d879fd32fe93bd675e19d4abb6b0688b
MD5 4a788361ca41f8c19958d5463b18ad48
BLAKE2b-256 a7cdb0eb96d0ae9a92a5eec6a4fda82078969fa679a4cb301d9316285d7b9df6

See more details on using hashes here.

File details

Details for the file aas_pydantic-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aas_pydantic-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Windows/10

File hashes

Hashes for aas_pydantic-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6ecf207031b2cd0999d9fde113dbca00d5d04df23ad6cf7e004e699b0ec69be3
MD5 2b20dfb9ddfdc579635b6577b391e3d9
BLAKE2b-256 45218cf9f1308f775210fa207efb8afae82f6373b1328c19f2fa8cb00ab380fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page