GreenDelta / olca-schema

Other
14 stars 7 forks source link

Add Reference to `UnitGroup` in `Unit` #5

Closed Miguel-g-c closed 2 years ago

Miguel-g-c commented 2 years ago

Add a new attribute to Unit model, a Ref pointing to the UnitGroup to which the Unit belongs.

Why?

Having this reference improves the domain model, avoiding unnecessary boilerplate code in other applications to find out the UnitGroup of a specific Unit.

Example

Right now, the CSV format of openLCA reference data for units has the following columns:

  1. reference ID (UUID; required)
  2. name (string; required)
  3. description (string; optional)
  4. conversion factor (double; required)
  5. synonyms (string: list separated by semicolon; optional)
  6. reference ID of unit group (UUID, required)

For example, a Repository to manage these files may be implemented in Python. Right now it could look like:

class CsvRepository(AbstractRepository):
    def __init__(self, path: str) -> None:
        self.__path = path

    def add(self, entity: Unit) -> None:
        with open(self.__path, "a", encoding="utf-8") as file:
            writer = csv.writer(file)
            writer.writerow(
                [
                    entity.id,
                    entity.name,
                    entity.description,
                    entity.conversion_factor,
                    entity.synonyms,
                    function_to_find_the_unit_group_of(entitiy, unit_groups),
                ]
            )

The implementation would be easier just having entity.unit_group.id.