Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
calculation_rules.py 3.55 KiB
"""
File consists of several functions for the calculation rules of FAIR Quality KPIs
"""

from functions.classes import *

def test_function():
    """Test function to check module functionality"""
    print("You called the test function.")

def kpi_mass(system: LegoAssembly)->float:
    """
    Calculates the total mass of the system

    Args:
        system (LegoAssembly): LegoAssembly object that represents the system

    Returns:
        total_mass (float): Sum of masses of all components in the system in g

    Raises:
        TypeError: If an argument with unsupported type is passed
            (i.e. anything other than LegoAssembly).
    """
    if not isinstance(system, LegoAssembly):
        raise TypeError(f"Unsupported type {type(system)} passed to kpi_mass()")

    total_mass = 0
    for c in system.get_component_list(-1):
        total_mass += c.properties["mass [g]"]
    return total_mass # alternative: sum(c.properties["mass [g]"] for c in system.get_component_list(-1))

# Add new functions for calculating metrics


def kpi_delivery_time(system: LegoAssembly)->float:
    """
    Calculates the total delivery time of the system

    Args:
        system(LegoAssembly): LegoAssembly object that represents the system

    Returns:
           total_delivery_time (float): The amount of time of all components of the system it takes to arrive
           
    Raises:
        TypeError: If an argument with unsupported type is passed
            (i.e. anything other than LegoAssembly).
            
    """
    if not isinstance(system, LegoAssembly):
        raise TypeError(f"Unsupported type {type(system)} passed to kpi_delivery_time()")

    total_delivery_time=max(c.properties['delivery time [days]'] for c in system.get_component_list(-1))

    return total_delivery_time



def kpi_total_co2_emissions(system: LegoAssembly) -> float:
    """
    Calculates the total mass of CO2 emitted in the production of the system.

    Args:
        system (LegoAssembly): LegoAssembly object that represents the system

    Returns:
        total_co2_emissions (float): Sum of all CO2 emitted in the production of the system

    Raises:
        TypeError: If an argument with unsupported type is passed
            (i.e. anything other than LegoAssembly).
    """
    
    if not isinstance(system, LegoAssembly):
        raise TypeError(f"Unsupported type {type(system)} passed to kpi_total_co2_emissions()")

    total_co2_emissions = sum(
        c.properties["environmental impact [kg CO2e /kg]"] * c.properties["mass [g]"]
        for c in system.get_component_list(-1)
    )
    
    return total_co2_emissions


def kpi_total_price(system: LegoAssembly) -> float:
    """
    Calculates the total price of the system.

    Args:
        system (LegoAssembly): LegoAssembly object that represents the system

    Returns:
        total_price (float): Sum of prices of all component prices of the system

    Raises:
        TypeError: If an argument with unsupported type is passed
            (i.e. anything other than LegoAssembly).
    """
    if not isinstance(system, LegoAssembly):
        raise TypeError(f"Unsupported type {type(system)} passed to kpi_total_price()")

    
    total_price = sum(c.properties["price [Euro]"] for c in system.get_component_list(-1))

    return total_price








if __name__ == "__main__":
    """
    Function to inform that functions in this module is
    intended for import not usage as script
    """
    print(
        "This script contains functions for calculating the FAIR Quality KPIs."
        "It is not to be executed independently."
    )