Spaces:
Running
Running
Update class_diagram_generator.py
Browse files- class_diagram_generator.py +186 -0
class_diagram_generator.py
CHANGED
|
@@ -4,6 +4,192 @@ from tempfile import NamedTemporaryFile
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
if not json_input.strip():
|
| 9 |
return "Error: Empty input"
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
def generate_class_diagram(json_input: str, output_format: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Generates a class diagram from JSON input.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
json_input (str): A JSON string describing the class diagram structure.
|
| 12 |
+
It must follow the Expected JSON Format Example below.
|
| 13 |
+
|
| 14 |
+
Expected JSON Format Example:
|
| 15 |
+
{
|
| 16 |
+
"classes": [
|
| 17 |
+
{
|
| 18 |
+
"name": "Vehicle",
|
| 19 |
+
"type": "abstract",
|
| 20 |
+
"attributes": [
|
| 21 |
+
{"name": "id", "type": "String", "visibility": "-"},
|
| 22 |
+
{"name": "brand", "type": "String", "visibility": "#"},
|
| 23 |
+
{"name": "model", "type": "String", "visibility": "#"},
|
| 24 |
+
{"name": "year", "type": "int", "visibility": "#"},
|
| 25 |
+
{"name": "price", "type": "double", "visibility": "+"},
|
| 26 |
+
{"name": "vehicleCount", "type": "int", "visibility": "+", "static": true}
|
| 27 |
+
],
|
| 28 |
+
"methods": [
|
| 29 |
+
{"name": "Vehicle", "parameters": [{"name": "brand", "type": "String"}, {"name": "model", "type": "String"}], "return_type": "Vehicle", "visibility": "+"},
|
| 30 |
+
{"name": "startEngine", "return_type": "void", "visibility": "+", "abstract": true},
|
| 31 |
+
{"name": "stopEngine", "return_type": "void", "visibility": "+"},
|
| 32 |
+
{"name": "getPrice", "return_type": "double", "visibility": "+"},
|
| 33 |
+
{"name": "setPrice", "parameters": [{"name": "price", "type": "double"}], "return_type": "void", "visibility": "+"},
|
| 34 |
+
{"name": "getTotalVehicles", "return_type": "int", "visibility": "+", "static": true}
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"name": "Car",
|
| 39 |
+
"type": "class",
|
| 40 |
+
"attributes": [
|
| 41 |
+
{"name": "doors", "type": "int", "visibility": "-"},
|
| 42 |
+
{"name": "transmission", "type": "TransmissionType", "visibility": "-"},
|
| 43 |
+
{"name": "fuelType", "type": "FuelType", "visibility": "-"}
|
| 44 |
+
],
|
| 45 |
+
"methods": [
|
| 46 |
+
{"name": "Car", "parameters": [{"name": "brand", "type": "String"}, {"name": "model", "type": "String"}, {"name": "doors", "type": "int"}], "return_type": "Car", "visibility": "+"},
|
| 47 |
+
{"name": "startEngine", "return_type": "void", "visibility": "+"},
|
| 48 |
+
{"name": "openTrunk", "return_type": "void", "visibility": "+"},
|
| 49 |
+
{"name": "getDoors", "return_type": "int", "visibility": "+"},
|
| 50 |
+
{"name": "setTransmission", "parameters": [{"name": "transmission", "type": "TransmissionType"}], "return_type": "void", "visibility": "+"}
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"name": "Motorcycle",
|
| 55 |
+
"type": "class",
|
| 56 |
+
"attributes": [
|
| 57 |
+
{"name": "engineSize", "type": "int", "visibility": "-"},
|
| 58 |
+
{"name": "hasWindshield", "type": "boolean", "visibility": "-"}
|
| 59 |
+
],
|
| 60 |
+
"methods": [
|
| 61 |
+
{"name": "Motorcycle", "parameters": [{"name": "brand", "type": "String"}, {"name": "model", "type": "String"}], "return_type": "Motorcycle", "visibility": "+"},
|
| 62 |
+
{"name": "startEngine", "return_type": "void", "visibility": "+"},
|
| 63 |
+
{"name": "wheelie", "return_type": "void", "visibility": "+"},
|
| 64 |
+
{"name": "getEngineSize", "return_type": "int", "visibility": "+"}
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"name": "Engine",
|
| 69 |
+
"type": "class",
|
| 70 |
+
"attributes": [
|
| 71 |
+
{"name": "horsepower", "type": "int", "visibility": "-"},
|
| 72 |
+
{"name": "cylinders", "type": "int", "visibility": "-"},
|
| 73 |
+
{"name": "fuelType", "type": "FuelType", "visibility": "-"}
|
| 74 |
+
],
|
| 75 |
+
"methods": [
|
| 76 |
+
{"name": "Engine", "parameters": [{"name": "horsepower", "type": "int"}, {"name": "cylinders", "type": "int"}], "return_type": "Engine", "visibility": "+"},
|
| 77 |
+
{"name": "start", "return_type": "boolean", "visibility": "+"},
|
| 78 |
+
{"name": "stop", "return_type": "void", "visibility": "+"},
|
| 79 |
+
{"name": "getHorsepower", "return_type": "int", "visibility": "+"}
|
| 80 |
+
]
|
| 81 |
+
},
|
| 82 |
+
{
|
| 83 |
+
"name": "TransmissionType",
|
| 84 |
+
"type": "enum",
|
| 85 |
+
"attributes": [
|
| 86 |
+
{"name": "MANUAL", "type": "TransmissionType", "visibility": "+", "static": true},
|
| 87 |
+
{"name": "AUTOMATIC", "type": "TransmissionType", "visibility": "+", "static": true},
|
| 88 |
+
{"name": "CVT", "type": "TransmissionType", "visibility": "+", "static": true}
|
| 89 |
+
],
|
| 90 |
+
"methods": []
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"name": "FuelType",
|
| 94 |
+
"type": "enum",
|
| 95 |
+
"attributes": [
|
| 96 |
+
{"name": "GASOLINE", "type": "FuelType", "visibility": "+", "static": true},
|
| 97 |
+
{"name": "DIESEL", "type": "FuelType", "visibility": "+", "static": true},
|
| 98 |
+
{"name": "ELECTRIC", "type": "FuelType", "visibility": "+", "static": true},
|
| 99 |
+
{"name": "HYBRID", "type": "FuelType", "visibility": "+", "static": true}
|
| 100 |
+
],
|
| 101 |
+
"methods": []
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"name": "VehicleService",
|
| 105 |
+
"type": "interface",
|
| 106 |
+
"attributes": [],
|
| 107 |
+
"methods": [
|
| 108 |
+
{"name": "maintenance", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "void", "visibility": "+", "abstract": true},
|
| 109 |
+
{"name": "repair", "parameters": [{"name": "vehicle", "type": "Vehicle"}, {"name": "issue", "type": "String"}], "return_type": "boolean", "visibility": "+", "abstract": true},
|
| 110 |
+
{"name": "inspectVehicle", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "InspectionReport", "visibility": "+", "abstract": true}
|
| 111 |
+
]
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
"name": "GarageService",
|
| 115 |
+
"type": "class",
|
| 116 |
+
"attributes": [
|
| 117 |
+
{"name": "garageName", "type": "String", "visibility": "-"},
|
| 118 |
+
{"name": "location", "type": "String", "visibility": "-"}
|
| 119 |
+
],
|
| 120 |
+
"methods": [
|
| 121 |
+
{"name": "GarageService", "parameters": [{"name": "name", "type": "String"}], "return_type": "GarageService", "visibility": "+"},
|
| 122 |
+
{"name": "maintenance", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "void", "visibility": "+"},
|
| 123 |
+
{"name": "repair", "parameters": [{"name": "vehicle", "type": "Vehicle"}, {"name": "issue", "type": "String"}], "return_type": "boolean", "visibility": "+"},
|
| 124 |
+
{"name": "inspectVehicle", "parameters": [{"name": "vehicle", "type": "Vehicle"}], "return_type": "InspectionReport", "visibility": "+"}
|
| 125 |
+
]
|
| 126 |
+
}
|
| 127 |
+
],
|
| 128 |
+
"relationships": [
|
| 129 |
+
{
|
| 130 |
+
"from": "Car",
|
| 131 |
+
"to": "Vehicle",
|
| 132 |
+
"type": "inheritance",
|
| 133 |
+
"label": "extends"
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"from": "Motorcycle",
|
| 137 |
+
"to": "Vehicle",
|
| 138 |
+
"type": "inheritance",
|
| 139 |
+
"label": "extends"
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"from": "Car",
|
| 143 |
+
"to": "Engine",
|
| 144 |
+
"type": "composition",
|
| 145 |
+
"label": "has",
|
| 146 |
+
"multiplicity_from": "1",
|
| 147 |
+
"multiplicity_to": "1"
|
| 148 |
+
},
|
| 149 |
+
{
|
| 150 |
+
"from": "Motorcycle",
|
| 151 |
+
"to": "Engine",
|
| 152 |
+
"type": "composition",
|
| 153 |
+
"label": "has",
|
| 154 |
+
"multiplicity_from": "1",
|
| 155 |
+
"multiplicity_to": "1"
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"from": "Car",
|
| 159 |
+
"to": "TransmissionType",
|
| 160 |
+
"type": "association",
|
| 161 |
+
"label": "uses",
|
| 162 |
+
"multiplicity_from": "1",
|
| 163 |
+
"multiplicity_to": "1"
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"from": "Vehicle",
|
| 167 |
+
"to": "FuelType",
|
| 168 |
+
"type": "association",
|
| 169 |
+
"label": "uses",
|
| 170 |
+
"multiplicity_from": "1",
|
| 171 |
+
"multiplicity_to": "1"
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
"from": "GarageService",
|
| 175 |
+
"to": "VehicleService",
|
| 176 |
+
"type": "realization",
|
| 177 |
+
"label": "implements"
|
| 178 |
+
},
|
| 179 |
+
{
|
| 180 |
+
"from": "GarageService",
|
| 181 |
+
"to": "Vehicle",
|
| 182 |
+
"type": "dependency",
|
| 183 |
+
"label": "services",
|
| 184 |
+
"multiplicity_from": "1",
|
| 185 |
+
"multiplicity_to": "*"
|
| 186 |
+
}
|
| 187 |
+
]
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
str: The filepath to the generated PNG image file.
|
| 192 |
+
"""
|
| 193 |
try:
|
| 194 |
if not json_input.strip():
|
| 195 |
return "Error: Empty input"
|