import graphviz import json from tempfile import NamedTemporaryFile import os def generate_class_diagram(json_input: str, output_format: str) -> str: """ Generates a class diagram from JSON input. Args: json_input (str): A JSON string describing the class diagram structure. It must follow the Expected JSON Format Example below. Expected JSON Format Example: { "classes": [ { "name": "Vehicle", "type": "abstract", "attributes": [ {"name": "id", "type": "String", "visibility": "-"}, {"name": "brand", "type": "String", "visibility": "#"}, {"name": "model", "type": "String", "visibility": "#"}, {"name": "year", "type": "int", "visibility": "#"}, {"name": "price", "type": "double", "visibility": "+"}, {"name": "vehicleCount", "type": "int", "visibility": "+", "static": true} ], "methods": [ {"name": "Vehicle", "parameters": [{"name": "brand", "type": "String"}, {"name": "model", "type": "String"}], "return_type": "Vehicle", "visibility": "+"}, {"name": "startEngine", "return_type": "void", "visibility": "+", "abstract": true}, {"name": "stopEngine", "return_type": "void", "visibility": "+"}, {"name": "getPrice", "return_type": "double", "visibility": "+"}, {"name": "setPrice", "parameters": [{"name": "price", "type": "double"}], "return_type": "void", "visibility": "+"}, {"name": "getTotalVehicles", "return_type": "int", "visibility": "+", "static": true} ] }, { "name": "Car", "type": "class", "attributes": [ {"name": "doors", "type": "int", "visibility": "-"}, {"name": "transmission", "type": "TransmissionType", "visibility": "-"}, {"name": "fuelType", "type": "FuelType", "visibility": "-"} ], "methods": [ {"name": "Car", "parameters": [{"name": "brand", "type": "String"}, {"name": "model", "type": "String"}, {"name": "doors", "type": "int"}], "return_type": "Car", "visibility": "+"}, {"name": "startEngine", "return_type": "void", "visibility": "+"}, {"name": "openTrunk", "return_type": "void", "visibility": "+"}, {"name": "getDoors", "return_type": "int", "visibility": "+"}, {"name": "setTransmission", "parameters": [{"name": "transmission", "type": "TransmissionType"}], "return_type": "void", "visibility": "+"} ] }, { "name": "Motorcycle", "type": "class", "attributes": [ {"name": "engineSize", "type": "int", "visibility": "-"}, {"name": "hasWindshield", "type": "boolean", "visibility": "-"} ], "methods": [ {"name": "Motorcycle", "parameters": [{"name": "brand", "type": "String"}, {"name": "model", "type": "String"}], "return_type": "Motorcycle", "visibility": "+"}, {"name": "startEngine", "return_type": "void", "visibility": "+"}, {"name": "wheelie", "return_type": "void", "visibility": "+"}, {"name": "getEngineSize", "return_type": "int", "visibility": "+"} ] }, { "name": "Engine", "type": "class", "attributes": [ {"name": "horsepower", "type": "int", "visibility": "-"}, {"name": "cylinders", "type": "int", "visibility": "-"}, {"name": "fuelType", "type": "FuelType", "visibility": "-"} ], "methods": [ {"name": "Engine", "parameters": [{"name": "horsepower", "type": "int"}, {"name": "cylinders", "type": "int"}], "return_type": "Engine", "visibility": "+"}, {"name": "start", "return_type": "boolean", "visibility": "+"}, {"name": "stop", "return_type": "void", "visibility": "+"}, {"name": "getHorsepower", "return_type": "int", "visibility": "+"} ] }, { "name": "TransmissionType", "type": "enum", "attributes": [ {"name": "MANUAL", "type": "TransmissionType", "visibility": "+", "static": true}, {"name": "AUTOMATIC", "type": "TransmissionType", "visibility": "+", "static": true}, {"name": "CVT", "type": "TransmissionType", "visibility": "+", "static": true} ], "methods": [] }, { "name": "FuelType", "type": "enum", "attributes": [ {"name": "GASOLINE", "type": "FuelType", "visibility": "+", "static": true}, {"name": "DIESEL", "type": "FuelType", "visibility": "+", "static": true}, {"name": "ELECTRIC", "type": "FuelType", "visibility": "+", "static": true}, {"name": "HYBRID", "type": "FuelType", "visibility": "+", "static": true} ], "methods": [] }, { "name": "VehicleService", "type": "interface", "attributes": [], "methods": [ {"name": "maintenance", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "void", "visibility": "+", "abstract": true}, {"name": "repair", "parameters": [{"name": "vehicle", "type": "Vehicle"}, {"name": "issue", "type": "String"}], "return_type": "boolean", "visibility": "+", "abstract": true}, {"name": "inspectVehicle", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "InspectionReport", "visibility": "+", "abstract": true} ] }, { "name": "GarageService", "type": "class", "attributes": [ {"name": "garageName", "type": "String", "visibility": "-"}, {"name": "location", "type": "String", "visibility": "-"} ], "methods": [ {"name": "GarageService", "parameters": [{"name": "name", "type": "String"}], "return_type": "GarageService", "visibility": "+"}, {"name": "maintenance", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "void", "visibility": "+"}, {"name": "repair", "parameters": [{"name": "vehicle", "type": "Vehicle"}, {"name": "issue", "type": "String"}], "return_type": "boolean", "visibility": "+"}, {"name": "inspectVehicle", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "InspectionReport", "visibility": "+"} ] } ], "relationships": [ { "from": "Car", "to": "Vehicle", "type": "inheritance" }, { "from": "Motorcycle", "to": "Vehicle", "type": "inheritance" }, { "from": "Car", "to": "Engine", "type": "composition", "multiplicity_from": "1", "multiplicity_to": "1" }, { "from": "Motorcycle", "to": "Engine", "type": "composition", "multiplicity_from": "1", "multiplicity_to": "1" }, { "from": "Car", "to": "TransmissionType", "type": "association", "multiplicity_from": "1", "multiplicity_to": "1" }, { "from": "Vehicle", "to": "FuelType", "type": "association", "multiplicity_from": "1", "multiplicity_to": "1" }, { "from": "GarageService", "to": "VehicleService", "type": "realization" }, { "from": "GarageService", "to": "Vehicle", "type": "dependency", "multiplicity_from": "1", "multiplicity_to": "*" } ] } Returns: str: The filepath to the generated PNG image file. """ try: if not json_input.strip(): return "Error: Empty input" data = json.loads(json_input) if 'classes' not in data: raise ValueError("Missing required field: classes") dot = graphviz.Digraph(comment='Class Diagram') dot.attr(rankdir='TB', bgcolor='white', pad='0.5', nodesep='0.8', ranksep='1.2', splines='ortho') dot.attr('node', shape='plaintext', fontname='Arial', fontsize='11') dot.attr('edge', color='#4a4a4a', fontname='Arial', fontsize='9', minlen='1') header_color = '#BEBEBE' body_color = '#E8E8E8' classes = data.get('classes', []) relationships = data.get('relationships', []) for cls in classes: class_name = cls.get('name') class_type = cls.get('type', 'class') attributes = cls.get('attributes', []) methods = cls.get('methods', []) if not class_name: continue html_label = f'
<<abstract>> {class_name} |
<<interface>> {class_name} |
<<enumeration>> {class_name} |
{class_name} |
{line} |
{line} |