4.3. local_file - Store and Retrieve AAS-objects as JSON Files

This module adds the functionality of storing and retrieving Identifiable objects in local files.

The LocalFileBackend takes care of updating and committing objects from and to the files, while the LocalFileObjectStore handles adding, deleting and otherwise managing the AAS objects in a specific Directory.

exception FileBackendSourceError

Raised, if the given object’s source is not resolvable as a local file

class LocalFileBackend

This Backend stores each Identifiable object as a single JSON document as a local file in a directory. Each document’s id is build from the object’s identifier using a SHA256 sum of its identifiable; the document’s contents comprise a single property data, containing the JSON serialization of the BaSyx Python SDK object. The adapter.json package is used for serialization and deserialization of objects.

classmethod commit_object(committed_object: Referable, store_object: Referable, relative_path: List[str]) None

Function (class method) to be called when an object shall be committed (local changes pushed to the external data source) via this backend implementation.

It is automatically called by the commit() implementation, when the source URI of the object or the source URI one of its ancestors in the AAS object containment hierarchy include an URI schema for which this backend has been registered. Both of the objects are passed to this function: the one which shall be committed (committed_object) and its ancestor with the relevant source URI (store_object). They may be the same, the committed object has a source with the relevant schema itself. Additionally, the relative_path from the store_object down to the committed_object is provided.

The backend MUST ensure to commit all local changes of at least the committed_object and all objects contained within it (if any) to the data source. It MAY additionally commit changes to other objects (i.e. the store_object and any additional contained object).

For this purpose a concrete implementation of this method would typically use the source attribute of the store_object to identify the data source. If the data source supports fine-grained access to contained objects, the relative_path may become handy to compose the committed object’s address within the data source’s interface.

Parameters:
  • committed_object – The object which shall be synced to the external data source

  • store_object – The object which originates from the relevant data source (i.e. has the relevant source attribute). It may be the committed_object or one of its ancestors in the AAS object hierarchy.

  • relative_path – List of idShort strings to resolve the committed_object starting at the store_object, such that obj = store_object; for i in relative_path: obj = obj.get_referable(i) resolves to the committed_object. In case that store_object is committed_object, it is an empty list.

Raises:

BackendNotAvailableException – when the external data source cannot be reached

classmethod update_object(updated_object: Referable, store_object: Referable, relative_path: List[str]) None

Function (class method) to be called when an object shall be updated (local object updated with changes from the external data source) via this backend implementation.

It is automatically called by the update() implementation, when the source URI of the object or the source URI one of its ancestors in the AAS object containment hierarchy include an URI schema for which this backend has been registered. Both of the objects are passed to this function: the one which shall be update (updated_object) and its ancestor with the relevant source URI (store_object). They may be the same, the updated object has a source with the relevant schema itself. Additionally, the relative_path from the store_object down to the updated_object is provided.

The backend MUST ensure to update at least the updated_object and all objects contained within it (if any) with any changes from the data source. It MAY additionally update other objects (i.e. the store_object and any additional contained object).

For this purpose a concrete implementation of this method would typically use the source attribute of the store_object to identify the data source. If the data source supports fine-grained access to contained objects, the relative_path may become handy to compose the updated object’s address within the data source’s interface.

Parameters:
  • updated_object – The object which shall be synced from the external data source

  • store_object – The object which originates from the relevant data source (i.e. has the relevant source attribute). It may be the committed_object or one of its ancestors in the AAS object hierarchy.

  • relative_path – List of idShort strings to resolve the updated_object starting at the store_object, such that obj = store_object; for i in relative_path: obj = obj.get_referable(i) resolves to the updated_object. In case that store_object is updated_object, it is an empty list.

Raises:

BackendNotAvailableException – when the external data source cannot be reached

class LocalFileObjectStore(directory_path: str)

An ObjectStore implementation for Identifiable BaSyx Python SDK objects backed by a local file based local backend

add(x: Identifiable) None

Add an object to the store

Raises:

KeyError – If an object with the same id exists already in the object store

check_directory(create=False)

Check if the directory exists and created it if not (and requested to do so)

Parameters:

create – If True and the database does not exist, try to create it

discard(x: Identifiable) None

Delete an Identifiable AAS object from the local file store

Parameters:

x – The object to be deleted

Raises:

KeyError – If the object does not exist in the database

generate_source(identifiable: Identifiable) str

Generates the source string for an Identifiable object that is backed by the File

Parameters:

identifiable – Identifiable object

get_identifiable(identifier: str) Identifiable

Retrieve an AAS object from the local file by its Identifier

Raises:

KeyError – If the respective file could not be found

get_identifiable_by_hash(hash_: str) Identifiable

Retrieve an AAS object from the local file by its identifier hash

Raises:

KeyError – If the respective file could not be found