Spaces:
Running
Running
File size: 14,126 Bytes
5f8bd16 693cb02 53aab24 693cb02 53aab24 693cb02 53aab24 693cb02 5f8bd16 06e522b 4c56571 6ee5ad8 b2d22b3 5f8bd16 c9dc53b 5f8bd16 06e522b 5f8bd16 6ee5ad8 5f8bd16 6ee5ad8 5f8bd16 6ee5ad8 5f8bd16 6ee5ad8 06e522b 6ee5ad8 5f8bd16 06e522b 5f8bd16 06e522b c9dc53b 53aab24 dfe2cff 6ee5ad8 5f8bd16 06e522b 5f8bd16 06e522b 5f8bd16 06e522b c9dc53b 53aab24 c9dc53b 53aab24 c9dc53b 6ee5ad8 06e522b 6ee5ad8 5f8bd16 6ee5ad8 5f8bd16 06e522b 5f8bd16 6ee5ad8 53aab24 6ee5ad8 c9dc53b dfe2cff c9dc53b 5f8bd16 dfe2cff 5f8bd16 c9dc53b 6ee5ad8 5f8bd16 c9dc53b 6ee5ad8 5f8bd16 c9dc53b 6ee5ad8 5f8bd16 c9dc53b 6ee5ad8 5f8bd16 c9dc53b 6ee5ad8 53aab24 c9dc53b 6ee5ad8 c9dc53b 5f8bd16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
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='black', fontname='Arial', fontsize='9', minlen='1')
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 = '<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="5" BGCOLOR="white">'
if class_type == 'abstract':
html_label += f'<TR><TD ALIGN="CENTER"><B><<abstract>><BR/>{class_name}</B></TD></TR>'
elif class_type == 'interface':
html_label += f'<TR><TD ALIGN="CENTER"><B><<interface>><BR/>{class_name}</B></TD></TR>'
elif class_type == 'enum':
html_label += f'<TR><TD ALIGN="CENTER"><B><<enumeration>><BR/>{class_name}</B></TD></TR>'
else:
html_label += f'<TR><TD ALIGN="CENTER"><B>{class_name}</B></TD></TR>'
html_label += '<HR/>'
if attributes:
for attr in attributes:
visibility = attr.get('visibility', '+')
name = attr.get('name', '')
attr_type = attr.get('type', '')
is_static = attr.get('static', False)
line = f"{visibility} {name}"
if attr_type:
line += f" : {attr_type}"
if is_static:
line = f"<U>{line}</U>"
html_label += f'<TR><TD ALIGN="LEFT">{line}</TD></TR>'
else:
html_label += '<TR><TD ALIGN="LEFT"> </TD></TR>'
html_label += '<HR/>'
if methods:
for method in methods:
visibility = method.get('visibility', '+')
name = method.get('name', '')
parameters = method.get('parameters', [])
return_type = method.get('return_type', 'void')
is_static = method.get('static', False)
is_abstract = method.get('abstract', False)
line = f"{visibility} {name}("
if parameters:
params = []
for param in parameters:
param_name = param.get('name', '')
param_type = param.get('type', '')
params.append(f"{param_name}: {param_type}")
line += ", ".join(params)
line += f") : {return_type}"
if is_static:
line = f"<U>{line}</U>"
if is_abstract:
line = f"<I>{line}</I>"
html_label += f'<TR><TD ALIGN="LEFT">{line}</TD></TR>'
else:
html_label += '<TR><TD ALIGN="LEFT"> </TD></TR>'
html_label += '</TABLE>'
dot.node(class_name, f'<{html_label}>')
for relationship in relationships:
from_class = relationship.get('from')
to_class = relationship.get('to')
rel_type = relationship.get('type', 'association')
label = relationship.get('label', '')
multiplicity_from = relationship.get('multiplicity_from', '')
multiplicity_to = relationship.get('multiplicity_to', '')
if not from_class or not to_class:
continue
edge_attrs = {
'color': 'black'
}
if multiplicity_from:
edge_attrs['taillabel'] = multiplicity_from
if multiplicity_to:
edge_attrs['headlabel'] = multiplicity_to
if rel_type == 'inheritance':
edge_attrs['arrowhead'] = 'empty'
edge_attrs['color'] = 'black'
elif rel_type == 'composition':
edge_attrs['arrowhead'] = 'normal'
edge_attrs['arrowtail'] = 'diamond'
edge_attrs['dir'] = 'both'
edge_attrs['color'] = 'black'
elif rel_type == 'aggregation':
edge_attrs['arrowhead'] = 'normal'
edge_attrs['arrowtail'] = 'odiamond'
edge_attrs['dir'] = 'both'
edge_attrs['color'] = 'black'
elif rel_type == 'realization':
edge_attrs['arrowhead'] = 'empty'
edge_attrs['style'] = 'dashed'
edge_attrs['color'] = 'black'
elif rel_type == 'dependency':
edge_attrs['arrowhead'] = 'normal'
edge_attrs['style'] = 'dashed'
edge_attrs['color'] = 'black'
else:
edge_attrs['arrowhead'] = 'normal'
edge_attrs['color'] = 'black'
dot.edge(from_class, to_class, **edge_attrs)
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
dot.render(tmp.name, format=output_format, cleanup=True)
return f"{tmp.name}.{output_format}"
except json.JSONDecodeError:
return "Error: Invalid JSON format"
except Exception as e:
return f"Error: {str(e)}" |