Skip to content
Snippets Groups Projects
minimalbeispiel.ipynb 26.1 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c9328cd1",
   "metadata": {},
   "source": [
    "# FAIRe Qualitäts-KPIs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ee8746d",
   "metadata": {},
   "source": [
    "## Einführung\n",
    "FAIRe Qualitäts-KPIs schaffen eine transparente und nachvollziehbare Entscheidungsgrundlage. In dieser Lerneinheit sollen sie die Methodik erlernen, indem sie LEGO-Autos zusammenbauen, deren Quality KPIs bestimmen und miteinander vergleichen.\n",
    "\n",
    "### Werkzeug für den Zusammenbau\n",
    "Der Zusammenbau der Autos erfolgt virtuell. Als Werkzeug steht Ihnen das Modul `classes` zur Verfügung. In diesem sind unterschiedliche Klassen und Methoden implementiert (Objektorientierung). Sie erzeugen jeweils Objekte der Klassen. Ein Objekt ist dabei die virtuelle Abbildung eines realen Bauteiles. Die Eigenschaften des Bauteiles werden über die Eigenschaften des Objektes abgebildet.\n",
    "\n",
    "### Berechnung der Quality KPIs\n",
    "KPIs (Key Performance Indikatoren) sind Kenngrößen des Systems. Sie werden über Berechnungsvorschriften bestimmt. Diese Berechnungsvorschriften sind von Ihnen als python-Funktionen im Modul `calculation_rules` zu implementieren.\n",
    "\n",
    "### Datenblätter\n",
    "Für den Zusammenbau stehen Ihnen verschiedene Bauteile in unterschiedlichen Ausführungen zur Verfügung. Sie nutzen die Datenblätter, die als `json-Dateien` zur Verfügung gestellt werden, um Ihre Autos zusammenzubauen."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1c702114",
   "metadata": {},
   "source": [
    "## Eigene Module und Minimalbeispiel\n",
    "Für die Ausarbeitung nutzen wir zwei eigene Module (Python-Dateien), die im Ordner `functions` gespeichert sind.\n",
    "Das Modul `calculation_rules` erweitern Sie während der Ausarbeitung. Um die Änderungen zu nutzen, müssen Sie das Notebook neu starten.\n",
    "Im Modul `classes` befinden sich die komplette Klassen und Funktionen zur Verwendung.\n",
    "\n",
    "Mit einem Minimalbeispiel wird Ihnen gezeigt, wie sie die Module nutzen. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "670118b3",
   "metadata": {},
   "source": [
    "#### Versuchsaufbau:\n",
    "Die folgende Abbildung zeigt den Versuchsaufbau des Minimalbeispiels in LeoCAD. Die transparenten Bauteile sind dabei nicht Teil des hier erläuterten Zusammenbaus, können aber mit dem zur Verfügung gestellten Baukasten ergänzt werden:\n",
    "![Minimalbeispiel aus LeoCAD](figures/leocad_screenshot_cut.png)"
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6d3310be",
   "metadata": {},
   "source": [
    "### Modul classes\n",
    "Enthält `LegoComponent, LegoAssembly, AggregationLayer, KPIEncoder` und die Funktion `print_assembly_tree`.  \n",
    "`LegoComponent` bildet einzelne Komponenten ab, während `LegoAssembly` zusammengesetzte Aggregationsebenen abdeckt, also Bauteil, Baugruppe und System. Zur Unterscheidung dient die Klasse `AggregationLayer`, diese ist für `LegoComponent` immer `Component` (Komponente), muss für `LegoAssembly`  entsprechend auf `SYSTEM` (System) , `ASSEMBLY` (Baugruppe) oder `SUBASSEMBLY` (Bauteil) gesetzt werden.\n",
    "\n",
    "Wir bauen aus Achse, Rahmen und Reifen einen Tretroller zusammen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b2778dee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import modules\n",
    "import json\n",
    "import pprint\n",
    "from functions import calculation_rules\n",
    "\n",
    "# Importing all modules one by one to provide an overview\n",
    "# The next commented line would provide the same result in one line\n",
    "# from functions.classes import *\n",
    "from functions.classes import LegoComponent\n",
    "from functions.classes import LegoAssembly\n",
    "from functions.classes import AggregationLayer\n",
    "from functions.classes import KPIEncoder\n",
    "from functions.classes import print_assembly_tree\n",
    "\n",
    "# When you are writing code yourself later, you might want to copy\n",
    "# these imports to avoid rerunning the full notebook after restart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0b1f9aff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'item number': 32073, 'item description': 'Axle 5 studs', 'category': 'axle', 'price [Euro]': 0.001, 'mass [g]': 0.66, 'environmental impact [kg CO2e /kg]': 11.03, 'delivery time [days]': 3, 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=32073', 'dimension [studs]': 5}\n",
      "front axle\n",
      "{'category': 'axle',\n",
      " 'color': 'grey',\n",
      " 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=32073',\n",
      " 'delivery time [days]': 3,\n",
      " 'dimension [studs]': 5,\n",
      " 'environmental impact [kg CO2e /kg]': 11.03,\n",
      " 'item description': 'Axle 5 studs',\n",
      " 'item number': 32073,\n",
      " 'label': 'back axle',\n",
      " 'mass [g]': 0.66,\n",
      " 'price [Euro]': 0.001}\n"
     ]
    }
   ],
   "source": [
    "# Create the wheels and axles as single components first\n",
    "# Look up the specific item you want from the provided json files,\n",
    "# we can load the full file into a dict\n",
    "with open(\"datasheets/axles.json\") as json_file:\n",
    "    axles = json.load(json_file)\n",
    "# Pick a specific axle via its 'item number'\n",
    "print(axles[\"32073\"])\n",
    "\n",
    "# Create the component with the dict:\n",
    "front_axle = LegoComponent(\"front axle\", axles[\"32073\"])\n",
    "# Both label and the data dict are optional parameters.\n",
    "# You can view, add or edit properties:\n",
    "print(front_axle.properties[\"label\"])\n",
    "front_axle.properties[\"color\"] = \"light bluish grey\"\n",
    "\n",
    "\n",
    "# Create the second axle\n",
    "back_axle = LegoComponent()\n",
    "back_axle.properties[\"label\"] = \"back axle\"\n",
    "back_axle.properties.update(axles[\"32073\"])\n",
    "# Do not use = here, otherwise you'd overwrite existing properties.\n",
    "# Instead use update to have all entries added to properties\n",
    "back_axle.properties[\"color\"] = \"light bluish grey\"\n",
    "# Viewing dicts in one line is not easy to read,\n",
    "# a better output comes with pretty print (pprint):\n",
    "pprint.pprint(back_axle.properties)"
   ]
  },
  {
   "cell_type": "code",
   "id": "b4a8e8c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'category': 'rim',\n",
      " 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=56904',\n",
      " 'delivery time [days]': 5,\n",
      " 'diameter [mm]': 30.0,\n",
      " 'environmental impact [kg CO2e /kg]': 32.06,\n",
      " 'item description': 'Wheel 30mm D. x 14mm',\n",
      " 'item number': 56904,\n",
      " 'label': 'front rim',\n",
      " 'mass [g]': 4.1,\n",
      " 'paint': 'glossy',\n",
      " 'price [Euro]': 0.01,\n",
      " 'related items': 30699,\n",
      " 'surface': 'rough'}\n"
     ]
    }
   ],
   "source": [
    "# Now wheels\n",
    "# A wheel consists of a rim (Felge) and a tire (Reifen)\n",
    "with open(\"datasheets/rims.json\") as json_file:\n",
    "    rims = json.load(json_file)\n",
    "with open(\"datasheets/tires.json\") as json_file:\n",
    "    tires = json.load(json_file)\n",
    "# Adding the color here already as dict, and the surface as key-value argument.\n",
    "# Multiple of both parameters are supported, but only in this order.\n",
    "# front_wheel = LegoComponent(\n",
    "# 'front wheel', wheels['2903c02'], {'color':'yellow'},winter='true')\n",
    "\n",
    "front_rim = LegoComponent(\"front rim\", rims[\"56904\"], surface=\"rough\", paint=\"glossy\")\n",
    "front_tire = LegoComponent(\"front tire\", tires[\"30699\"], winter=True)\n",
    "pprint.pprint(front_rim.properties)\n",
    "\n",
    "# We included a clone function to both Lego classes, so you can easily\n",
    "# create duplicate objects. Passing the new label is optional\n",
    "back_rim = front_rim.clone(\"back rim\")\n",
    "back_tire = front_tire.clone(\"back tire\")"
   "id": "25bd06c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "front tire\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 35\u001b[0m\n\u001b[0;32m     30\u001b[0m front_axle_assembly\u001b[38;5;241m.\u001b[39madd([front_wheel_assembly, front_axle])\n\u001b[0;32m     32\u001b[0m back_axle_assembly \u001b[38;5;241m=\u001b[39m LegoAssembly(\n\u001b[0;32m     33\u001b[0m     AggregationLayer\u001b[38;5;241m.\u001b[39mASSEMBLY, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mback axle assembly\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     34\u001b[0m )\n\u001b[1;32m---> 35\u001b[0m \u001b[43mback_axle_assembly\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mback_axle_assembly\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mback_axle\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mc:\\Users\\Lamm\\Documents\\02_Areas\\Praktikum_Digitalisierung\\01_quality-kpi_dev\\functions\\classes.py:374\u001b[0m, in \u001b[0;36mLegoAssembly.add\u001b[1;34m(self, part)\u001b[0m\n\u001b[0;32m    372\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(part, \u001b[38;5;28mlist\u001b[39m):\n\u001b[0;32m    373\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m part:\n\u001b[1;32m--> 374\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd\u001b[49m\u001b[43m(\u001b[49m\u001b[43mp\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    375\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    376\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\n\u001b[0;32m    377\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mArgument should be of types \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mLegoAssembly\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    378\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mLegoComponent\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m or a list of them. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    379\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGot \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(part)\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m instead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    380\u001b[0m     )\n",
      "File \u001b[1;32mc:\\Users\\Lamm\\Documents\\02_Areas\\Praktikum_Digitalisierung\\01_quality-kpi_dev\\functions\\classes.py:369\u001b[0m, in \u001b[0;36mLegoAssembly.add\u001b[1;34m(self, part)\u001b[0m\n\u001b[0;32m    356\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    357\u001b[0m \u001b[38;5;124;03mAdds either a Lego component, a subassembly or a (mixed) list of them to the\u001b[39;00m\n\u001b[0;32m    358\u001b[0m \u001b[38;5;124;03mcurrent assembly. Uses internal functions add_component() and add_assembly().\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    366\u001b[0m \u001b[38;5;124;03m        instance, or a (mixed) list of them.\u001b[39;00m\n\u001b[0;32m    367\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    368\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(part, LegoComponent):\n\u001b[1;32m--> 369\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_component\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpart\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    370\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(part, LegoAssembly):\n\u001b[0;32m    371\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39madd_assembly(part)\n",
      "File \u001b[1;32mc:\\Users\\Lamm\\Documents\\02_Areas\\Praktikum_Digitalisierung\\01_quality-kpi_dev\\functions\\classes.py:310\u001b[0m, in \u001b[0;36mLegoAssembly.add_component\u001b[1;34m(self, component)\u001b[0m\n\u001b[0;32m    304\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(component, LegoComponent):\n\u001b[0;32m    305\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\n\u001b[0;32m    306\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mArgument should be of type \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mLegoComponent\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    307\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgot \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(component)\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m instead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    308\u001b[0m     )\n\u001b[1;32m--> 310\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_root_assembly\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mcontains_uuid(component\u001b[38;5;241m.\u001b[39m_uuid):\n\u001b[0;32m    311\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAssertionError\u001b[39;00m(\n\u001b[0;32m    312\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis assembly or a subassembly already contains \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    313\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe component with ID \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    314\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcomponent\u001b[38;5;241m.\u001b[39m_uuid\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    315\u001b[0m     )\n\u001b[0;32m    316\u001b[0m component\u001b[38;5;241m.\u001b[39m_parent \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\n",
      "File \u001b[1;32mc:\\Users\\Lamm\\Documents\\02_Areas\\Praktikum_Digitalisierung\\01_quality-kpi_dev\\functions\\classes.py:439\u001b[0m, in \u001b[0;36mLegoAssembly.get_root_assembly\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    431\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    432\u001b[0m \u001b[38;5;124;03mReturns the root LegoAssembly of the current assembly by recursively\u001b[39;00m\n\u001b[0;32m    433\u001b[0m \u001b[38;5;124;03msearching up the tree until the top-most parent (the root) is found.\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    436\u001b[0m \u001b[38;5;124;03m    The root LegoAssembly of the current LegoAssembly instance.\u001b[39;00m\n\u001b[0;32m    437\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    438\u001b[0m current_assembly \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\n\u001b[1;32m--> 439\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m current_assembly\u001b[38;5;241m.\u001b[39m_parent \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    440\u001b[0m     current_assembly \u001b[38;5;241m=\u001b[39m current_assembly\u001b[38;5;241m.\u001b[39m_parent\n\u001b[0;32m    441\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m current_assembly\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
   "source": [
    "# Create subassemblies and add the wheels and axles\n",
    "\n",
    "# The AggregationLayer must be used, passing the label\n",
    "# or additional properties is optional\n",
    "front_wheel_assembly = LegoAssembly(\n",
    "    AggregationLayer.SUBASSEMBLY,\n",
    "    \"front wheel assembly\",\n",
    "    assembly_method=\"stick together like lego blocks\",\n",
    ")\n",
    "# Add LegoComponents to the LegoAssembly, single or as list\n",
    "front_wheel_assembly.add([front_rim, front_tire])\n",
    "# You can access the components of an assembly like this:\n",
    "print(front_wheel_assembly.components[1].properties[\"label\"])\n",
    "\n",
    "# Assemblies can be cloned as well (including their children),\n",
    "# but don't forget to adjust labels or you might be stuck with\n",
    "# a 'front wheel' in your 'back wheel assembly'\n",
    "\n",
    "# Stick together back wheel parts\n",
    "back_wheel_assembly = LegoAssembly(\n",
    "    AggregationLayer.SUBASSEMBLY, \"back wheel assembly\"\n",
    "    )\n",
    "back_wheel_assembly.add([back_rim, back_tire])\n",
    "\n",
    "# Now combine wheels and axles\n",
    "\n",
    "front_axle_assembly = LegoAssembly(\n",
    "    AggregationLayer.ASSEMBLY, \"front axle assembly\"\n",
    ")\n",
    "front_axle_assembly.add([front_wheel_assembly, front_axle])\n",
    "\n",
    "back_axle_assembly = LegoAssembly(\n",
    "    AggregationLayer.ASSEMBLY, \"back axle assembly\"\n",
    ")\n",
    "back_axle_assembly.add([back_wheel_assembly, back_axle])"
   "execution_count": 5,
   "id": "2b6648e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create frame component and assemble the system\n",
    "\n",
    "with open(\"datasheets/frame.json\") as json_file:\n",
    "    frame = json.load(json_file)\n",
    "scooter_frame = LegoComponent(\"scooter frame\", frame[\"3703\"], {\"color\": \"red\"})\n",
    "\n",
    "# The scooter is our system level assembly, you can choose the AggregationLayer\n",
    "# But the hiercarchy (SYSTEM > ASSEMBLY > SUBASSEMBLY) must be in order.\n",
    "# Components can be added to all LegoAssembly objects\n",
    "\n",
    "scooter = LegoAssembly(\n",
    "    AggregationLayer.SYSTEM,\n",
    "    \"scooter\",\n",
    "    manufacturer=\"FST\",\n",
    "    comment=\"Faster! Harder! Scooter!\",\n",
    ")\n",
    "# add frame and subassemblies\n",
    "scooter.add([scooter_frame, front_wheel_assembly, back_wheel_assembly])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "71324895",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460]]\n",
      "[LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460], LegoComponent front wheel [b03ce5b5-9c6c-4315-a67b-902c729c3d7b], LegoComponent front axle [64771daf-733a-4a2a-8520-9fa94259e50e], LegoComponent back wheel [8fcb7db9-6fec-4e70-b274-0c4fe403550b], LegoComponent back axle [2ce57b96-64b8-48b1-aac8-b0731adaa93b]]\n"
     ]
    }
   ],
   "source": [
    "# Look at the assembly\n",
    "\n",
    "# We can get all LegoComponents from this assembly.\n",
    "# If parameter 'max_depth' is set to 0, only direct children will be listed:\n",
    "print(scooter.get_component_list(max_depth=0))\n",
    "# If it is set to -1 (default), all LegoComponents are listed:\n",
    "print(scooter.get_component_list())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "001f1c77",
   "metadata": {},
   "source": [
    "### Modul calculation_rules\n",
    "\n",
    "Um für unser System \"Tretroller\" einen KPI zu erzeugen, wird die Masse des Systems betrachtet. Die Masse der einzelnen Komponenten ist in den Datenblättern unter `mass [g]` enthalten."
   "execution_count": 7,
   "id": "7b60d0fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You called the test function.\n"
     ]
    }
   ],
   "source": [
    "# test the import\n",
    "calculation_rules.test_function()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe4edad6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gesamtmasse:  67.19 g\n"
     ]
    }
   ],
    "total_mass = calculation_rules.kpi_mass(scooter)\n",
    "print(\"Gesamtmasse: \", total_mass, \"g\")\n",
    "# Add KPI to system\n",
    "scooter.properties[\"mass [g]\"] = total_mass\n",
    "\n",
    "# We can also add the mass to the subassemblies.\n",
    "# children() returns a dict with a list of added\n",
    "# components and assemblies.\n",
    "for a in scooter.children()[\"assemblies\"]:\n",
    "    a_mass = 0\n",
    "    for c in a.get_component_list(-1):\n",
    "        a_mass += c.properties[\"mass [g]\"]\n",
    "    a.properties[\"mass [g]\"] = a_mass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c26b36c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460],\n",
       " LegoComponent front wheel [b03ce5b5-9c6c-4315-a67b-902c729c3d7b],\n",
       " LegoComponent front axle [64771daf-733a-4a2a-8520-9fa94259e50e],\n",
       " LegoComponent back wheel [8fcb7db9-6fec-4e70-b274-0c4fe403550b],\n",
       " LegoComponent back axle [2ce57b96-64b8-48b1-aac8-b0731adaa93b]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scooter.get_component_list(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4d56419f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LegoAssembly scooter [86fdda66-adb2-48c0-b6f8-f6c59d488662]\n",
      "├── LegoAssembly front wheel assembly [e02a87d2-cb0b-4695-b237-9ca17f9611da]\n",
      "│   ├── LegoComponent front wheel [b03ce5b5-9c6c-4315-a67b-902c729c3d7b]\n",
      "│   └── LegoComponent front axle [64771daf-733a-4a2a-8520-9fa94259e50e]\n",
      "├── LegoAssembly back wheel assembly [db5db4c5-36c3-4b90-88eb-299c46a30049]\n",
      "│   ├── LegoComponent back wheel [8fcb7db9-6fec-4e70-b274-0c4fe403550b]\n",
      "│   └── LegoComponent back axle [2ce57b96-64b8-48b1-aac8-b0731adaa93b]\n",
      "└── LegoComponent scooter frame [a90b291e-696c-41c7-b9b8-43d34724b460]\n"
     ]
    }
   ],
   "source": [
    "# Look at the full assembly with KPI\n",
    "\n",
    "# Print the full assembly with all levels\n",
    "print_assembly_tree(scooter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b31416d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dump to json file with to_dict() and KPIEncoder\n",
    "with open(\"scooter.json\", \"w\") as fp:\n",
    "    json.dump(scooter.to_dict(), fp, cls=KPIEncoder, indent=4)\n",
    "# full view is too big for Juypter (try it)\n",
    "# pprint.pprint(scooter.to_dict())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "53793ae8",
   "metadata": {},
   "source": [
    "In dieser exportierten json-Datei ('scooter.json') sind die Werte maschinen- und menschenlesbar hinterlegt.\n",
    "Zusammen mit der Berechnungsvorschrift in `calculation_rules` ist auch die Entstehung des KPI nachvollziehbar und wiederverwendbar dokumentiert und damit 'FAIR'."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "92c77051",
   "metadata": {},
   "source": [
    "#### Zusätzliche Details"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b91fed73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Child: LegoAssembly front wheel assembly [e02a87d2-cb0b-4695-b237-9ca17f9611da]\n",
      "Parent: LegoAssembly scooter [86fdda66-adb2-48c0-b6f8-f6c59d488662]\n",
      "Top parent:  LegoAssembly scooter [86fdda66-adb2-48c0-b6f8-f6c59d488662]\n"
     ]
    }
   ],
   "source": [
    "# Each child knows its parent\n",
    "first_child = scooter.children()[\"assemblies\"][0]\n",
    "print(\"Child:\", first_child)\n",
    "print(\"Parent:\", first_child.parent)\n",
    "# Also we can access the \"top\" parent\n",
    "latest_child = first_child.children()[\"components\"][0]\n",
    "print(\"Top parent: \", latest_child.get_root_assembly())\n",
    "\n",
    "# Each part created has a unique identifier (the long number in [])\n",
    "# Don't try to add the identical part again."
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}