Skip to content
Snippets Groups Projects
test-classes.py 4.44 KiB
Newer Older
# import standard libraries
import json
import pprint
# import classes from the classes module in functions package
from functions.classes import LegoComponent
from functions.classes import LegoAssembly
from functions.classes import ComponentCategory
from functions.classes import AggregationLayer
from functions.classes import KPIEncoder
from functions.classes import print_assembly_tree

from functions.calculation_rules import kpi_sum
# Test manually creating some assemblies and components
battery = LegoComponent("nice battery", ComponentCategory.BATTERY, "bat42", 1, 2, 3)
motor = LegoComponent("motor goes brrr", ComponentCategory.MOTOR, "motor", 1, 2, 3)
wheel = LegoComponent("much round wheel", ComponentCategory.WHEEL, "round", 1, 2, 3)


# Create subassemblies and combine to assembly (currently no parts present for this)
chassis = LegoAssembly("Chassis", AggregationLayer.ASSEMBLY)
door1 = LegoAssembly("Door 1", AggregationLayer.SUBASSEMBLY)
door2 = LegoAssembly("Door 2", AggregationLayer.SUBASSEMBLY)
chassis.add_assembly(door1)
chassis.add_assembly(door2)

engine = LegoAssembly("Engine", AggregationLayer.ASSEMBLY)
# Showcase cloning - one motor for each axis?
engine.add_component(motor.clone())

fuel_tank = LegoAssembly("Fuel Tank", AggregationLayer.ASSEMBLY)
fuel_tank.add_component(battery.clone())
wheels = LegoAssembly("Wheels", AggregationLayer.ASSEMBLY)
for _ in range(4):
    wheels.add_component(wheel.clone())

# Create and assemble the system level car
car = LegoAssembly("Car", AggregationLayer.SYSTEM)
car.add_assembly(chassis)
car.add_assembly(engine)
car.add_assembly(fuel_tank)
car.add_assembly(wheels)
print_assembly_tree(car)
# Dump to json file
with open("car.json", "w") as fp:
    json.dump(car.to_dict(), fp, cls=KPIEncoder)
pprint.pprint(car.to_dict())
pass


## Create components from datasheet
# Need to run the script from datasheets folder first
# Test loading dicts from datasheet jsons

with open('datasheets/Achsen.json') as json_file:
    Achsen = json.load(json_file)

print((Achsen["Achse 5 studs"]))

# This is hard to read, prettier please!

pprint.pprint(Achsen)

# Okay I'll use the 5 stud one
Achse5 = Achsen["Achse 5 studs"] # I'd prefer to call it via "Designnummer"
print(Achsen["Achse 5 studs"]["Designnummer"])

with open('datasheets/Räder.json') as json_file:
    Raeder = json.load(json_file)
# Get me the expensive one
Radpreis = []
for key in Raeder: # a list would work easier here than a dict
    Radpreis.append(Raeder[key]["Preis [Euro]"])
Radpreis = max(Radpreis)
for key in Raeder:
    if Raeder[key]["Preis [Euro]"] == Radpreis:
        TeuresRad=Raeder[key]

# Also need a frame
with open('datasheets/Gestell.json') as json_file:
    Gestelle = json.load(json_file)
Gestellbeschreibung = "Technic, Brick 1 x 8 with Holes"
Gestell = Gestelle[Gestellbeschreibung]

# Create Components from these items
# LegoComponent(name: str, category: ComponentCategory, lego_id: str, cost: float,
# mass: float, delivery_time: int, layer: AggregationLayer = AggregationLayer.COMPONENT, **properties)
scooterframe = LegoComponent("running board", ComponentCategory.FRAME,
                            Gestell["Designnummer"],
                            Gestell["Preis [Euro]"], Gestell["Gewicht [g]"],
                            0)

scooterwheel = LegoComponent("Vorderrad", ComponentCategory.WHEEL,
                            TeuresRad["Designnummer"],
                            TeuresRad["Preis [Euro]"], TeuresRad["Gewicht [g]"],
                            0)
Hock, Martin's avatar
Hock, Martin committed

# TODO component with copy of dict from json
# Cloning is necessary because each lego object gets a unique id so you can't use the same part twice
scooterwheel2 = scooterwheel.clone()
scooterwheel2.description = "Hinterrad"
print([scooterwheel.uuid, scooterwheel2.uuid])


# Lets Assembly, first the Assembly object, directly add the frame?
scooter = LegoAssembly("my first scooter", AggregationLayer.SYSTEM)
scooter.add_component(scooterframe) # only one per call for now
scooter.add_component(scooterwheel)
scooter.add_component(scooterwheel2)
# <Assembly>.add_assembly() works in the same way

# Look at our work:
print(scooter)
print(scooter.children())

## First KPI mass should be a sum
listofmasses= []
for c in scooter.components:
    listofmasses.append(c.mass)
for a in scooter.assemblies:
    # only checking one layer deep here
    listofmasses.append(a.mass)
Hock, Martin's avatar
Hock, Martin committed

# TODO example with get_component_list

print(listofmasses)
print(kpi_sum(listofmasses))

print("theend")