Spaces:
Running
Running
Update class_diagram_generator.py
Browse files- class_diagram_generator.py +51 -37
class_diagram_generator.py
CHANGED
|
@@ -199,10 +199,11 @@
|
|
| 199 |
# if 'classes' not in data:
|
| 200 |
# raise ValueError("Missing required field: classes")
|
| 201 |
|
|
|
|
| 202 |
# dot = graphviz.Digraph(comment='Class Diagram')
|
| 203 |
-
# dot.attr(rankdir='TB', bgcolor='white', pad='0.5')
|
| 204 |
-
# dot.attr('node', shape='
|
| 205 |
-
# dot.attr('edge', color='black', fontname='Arial', fontsize='9')
|
| 206 |
|
| 207 |
# classes = data.get('classes', [])
|
| 208 |
# relationships = data.get('relationships', [])
|
|
@@ -216,19 +217,24 @@
|
|
| 216 |
# if not class_name:
|
| 217 |
# continue
|
| 218 |
|
| 219 |
-
#
|
|
|
|
| 220 |
|
|
|
|
| 221 |
# if class_type == 'abstract':
|
| 222 |
-
#
|
| 223 |
# elif class_type == 'interface':
|
| 224 |
-
#
|
| 225 |
# elif class_type == 'enum':
|
| 226 |
-
#
|
| 227 |
# else:
|
| 228 |
-
#
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
# if attributes:
|
| 231 |
-
# attr_lines = []
|
| 232 |
# for attr in attributes:
|
| 233 |
# visibility = attr.get('visibility', '+')
|
| 234 |
# name = attr.get('name', '')
|
|
@@ -239,15 +245,18 @@
|
|
| 239 |
# if attr_type:
|
| 240 |
# line += f" : {attr_type}"
|
| 241 |
# if is_static:
|
| 242 |
-
# line
|
| 243 |
|
| 244 |
-
#
|
| 245 |
-
|
| 246 |
-
#
|
| 247 |
-
#
|
|
|
|
|
|
|
|
|
|
| 248 |
|
|
|
|
| 249 |
# if methods:
|
| 250 |
-
# method_lines = []
|
| 251 |
# for method in methods:
|
| 252 |
# visibility = method.get('visibility', '+')
|
| 253 |
# name = method.get('name', '')
|
|
@@ -267,29 +276,21 @@
|
|
| 267 |
# line += f") : {return_type}"
|
| 268 |
|
| 269 |
# if is_static:
|
| 270 |
-
# line
|
| 271 |
# if is_abstract:
|
| 272 |
-
# line
|
| 273 |
|
| 274 |
-
#
|
| 275 |
-
|
| 276 |
-
# if method_lines:
|
| 277 |
-
# label_parts.append("\\n".join(method_lines))
|
| 278 |
-
|
| 279 |
-
# label = "\\n\\n".join(label_parts)
|
| 280 |
-
|
| 281 |
-
# if class_type == 'interface':
|
| 282 |
-
# fillcolor = '#f5f5f5'
|
| 283 |
-
# style = 'filled,dashed'
|
| 284 |
-
# elif class_type == 'abstract':
|
| 285 |
-
# fillcolor = '#eeeeee'
|
| 286 |
-
# style = 'filled'
|
| 287 |
# else:
|
| 288 |
-
#
|
| 289 |
-
#
|
| 290 |
|
| 291 |
-
#
|
|
|
|
|
|
|
|
|
|
| 292 |
|
|
|
|
| 293 |
# for relationship in relationships:
|
| 294 |
# from_class = relationship.get('from')
|
| 295 |
# to_class = relationship.get('to')
|
|
@@ -301,7 +302,11 @@
|
|
| 301 |
# if not from_class or not to_class:
|
| 302 |
# continue
|
| 303 |
|
| 304 |
-
# edge_attrs = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
# if label:
|
| 307 |
# edge_attrs['label'] = label
|
|
@@ -312,27 +317,35 @@
|
|
| 312 |
# if multiplicity_to:
|
| 313 |
# edge_attrs['headlabel'] = multiplicity_to
|
| 314 |
|
|
|
|
| 315 |
# if rel_type == 'inheritance':
|
| 316 |
# edge_attrs['arrowhead'] = 'empty'
|
|
|
|
| 317 |
# elif rel_type == 'composition':
|
| 318 |
# edge_attrs['arrowhead'] = 'normal'
|
| 319 |
# edge_attrs['arrowtail'] = 'diamond'
|
| 320 |
# edge_attrs['dir'] = 'both'
|
|
|
|
| 321 |
# elif rel_type == 'aggregation':
|
| 322 |
# edge_attrs['arrowhead'] = 'normal'
|
| 323 |
# edge_attrs['arrowtail'] = 'odiamond'
|
| 324 |
# edge_attrs['dir'] = 'both'
|
|
|
|
| 325 |
# elif rel_type == 'realization':
|
| 326 |
# edge_attrs['arrowhead'] = 'empty'
|
| 327 |
# edge_attrs['style'] = 'dashed'
|
|
|
|
| 328 |
# elif rel_type == 'dependency':
|
| 329 |
# edge_attrs['arrowhead'] = 'normal'
|
| 330 |
# edge_attrs['style'] = 'dashed'
|
| 331 |
-
#
|
|
|
|
| 332 |
# edge_attrs['arrowhead'] = 'normal'
|
|
|
|
| 333 |
|
| 334 |
# dot.edge(from_class, to_class, **edge_attrs)
|
| 335 |
|
|
|
|
| 336 |
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 337 |
# dot.render(tmp.name, format=output_format, cleanup=True)
|
| 338 |
# return f"{tmp.name}.{output_format}"
|
|
@@ -543,11 +556,12 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
| 543 |
if 'classes' not in data:
|
| 544 |
raise ValueError("Missing required field: classes")
|
| 545 |
|
| 546 |
-
# Configuración del diagrama con
|
| 547 |
dot = graphviz.Digraph(comment='Class Diagram')
|
| 548 |
dot.attr(rankdir='TB', bgcolor='white', pad='0.5', nodesep='0.8', ranksep='1.2')
|
| 549 |
dot.attr('node', shape='plaintext', fontname='Arial', fontsize='11')
|
| 550 |
dot.attr('edge', color='black', fontname='Arial', fontsize='9', minlen='1')
|
|
|
|
| 551 |
|
| 552 |
classes = data.get('classes', [])
|
| 553 |
relationships = data.get('relationships', [])
|
|
@@ -647,7 +661,7 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
| 647 |
continue
|
| 648 |
|
| 649 |
edge_attrs = {
|
| 650 |
-
'splines': '
|
| 651 |
'concentrate': 'false', # Evita que las líneas se combinen
|
| 652 |
'constraint': 'true' # Mantiene la estructura jerárquica
|
| 653 |
}
|
|
|
|
| 199 |
# if 'classes' not in data:
|
| 200 |
# raise ValueError("Missing required field: classes")
|
| 201 |
|
| 202 |
+
# # Configuración del diagrama con espaciado reducido
|
| 203 |
# dot = graphviz.Digraph(comment='Class Diagram')
|
| 204 |
+
# dot.attr(rankdir='TB', bgcolor='white', pad='0.5', nodesep='0.8', ranksep='1.2')
|
| 205 |
+
# dot.attr('node', shape='plaintext', fontname='Arial', fontsize='11')
|
| 206 |
+
# dot.attr('edge', color='black', fontname='Arial', fontsize='9', minlen='1')
|
| 207 |
|
| 208 |
# classes = data.get('classes', [])
|
| 209 |
# relationships = data.get('relationships', [])
|
|
|
|
| 217 |
# if not class_name:
|
| 218 |
# continue
|
| 219 |
|
| 220 |
+
# # Crear tabla HTML para estructura de clase
|
| 221 |
+
# html_label = '<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="5" BGCOLOR="white">'
|
| 222 |
|
| 223 |
+
# # Header con nombre de clase y estereotipo
|
| 224 |
# if class_type == 'abstract':
|
| 225 |
+
# html_label += f'<TR><TD ALIGN="CENTER"><B><<abstract>><BR/>{class_name}</B></TD></TR>'
|
| 226 |
# elif class_type == 'interface':
|
| 227 |
+
# html_label += f'<TR><TD ALIGN="CENTER"><B><<interface>><BR/>{class_name}</B></TD></TR>'
|
| 228 |
# elif class_type == 'enum':
|
| 229 |
+
# html_label += f'<TR><TD ALIGN="CENTER"><B><<enumeration>><BR/>{class_name}</B></TD></TR>'
|
| 230 |
# else:
|
| 231 |
+
# html_label += f'<TR><TD ALIGN="CENTER"><B>{class_name}</B></TD></TR>'
|
| 232 |
|
| 233 |
+
# # Línea separadora después del nombre
|
| 234 |
+
# html_label += '<HR/>'
|
| 235 |
+
|
| 236 |
+
# # Sección de atributos
|
| 237 |
# if attributes:
|
|
|
|
| 238 |
# for attr in attributes:
|
| 239 |
# visibility = attr.get('visibility', '+')
|
| 240 |
# name = attr.get('name', '')
|
|
|
|
| 245 |
# if attr_type:
|
| 246 |
# line += f" : {attr_type}"
|
| 247 |
# if is_static:
|
| 248 |
+
# line = f"<U>{line}</U>" # Subrayado para elementos estáticos
|
| 249 |
|
| 250 |
+
# html_label += f'<TR><TD ALIGN="LEFT">{line}</TD></TR>'
|
| 251 |
+
# else:
|
| 252 |
+
# # Espacio vacío si no hay atributos
|
| 253 |
+
# html_label += '<TR><TD ALIGN="LEFT"> </TD></TR>'
|
| 254 |
+
|
| 255 |
+
# # Línea separadora entre atributos y métodos
|
| 256 |
+
# html_label += '<HR/>'
|
| 257 |
|
| 258 |
+
# # Sección de métodos
|
| 259 |
# if methods:
|
|
|
|
| 260 |
# for method in methods:
|
| 261 |
# visibility = method.get('visibility', '+')
|
| 262 |
# name = method.get('name', '')
|
|
|
|
| 276 |
# line += f") : {return_type}"
|
| 277 |
|
| 278 |
# if is_static:
|
| 279 |
+
# line = f"<U>{line}</U>" # Subrayado para métodos estáticos
|
| 280 |
# if is_abstract:
|
| 281 |
+
# line = f"<I>{line}</I>" # Cursiva para métodos abstractos
|
| 282 |
|
| 283 |
+
# html_label += f'<TR><TD ALIGN="LEFT">{line}</TD></TR>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
# else:
|
| 285 |
+
# # Espacio vacío si no hay métodos
|
| 286 |
+
# html_label += '<TR><TD ALIGN="LEFT"> </TD></TR>'
|
| 287 |
|
| 288 |
+
# html_label += '</TABLE>'
|
| 289 |
+
|
| 290 |
+
# # Agregar nodo con la tabla HTML
|
| 291 |
+
# dot.node(class_name, f'<{html_label}>')
|
| 292 |
|
| 293 |
+
# # Procesar relaciones con líneas rectas
|
| 294 |
# for relationship in relationships:
|
| 295 |
# from_class = relationship.get('from')
|
| 296 |
# to_class = relationship.get('to')
|
|
|
|
| 302 |
# if not from_class or not to_class:
|
| 303 |
# continue
|
| 304 |
|
| 305 |
+
# edge_attrs = {
|
| 306 |
+
# 'splines': 'ortho', # Líneas estrictamente ortogonales (solo horizontales/verticales)
|
| 307 |
+
# 'concentrate': 'false', # Evita que las líneas se combinen
|
| 308 |
+
# 'constraint': 'true' # Mantiene la estructura jerárquica
|
| 309 |
+
# }
|
| 310 |
|
| 311 |
# if label:
|
| 312 |
# edge_attrs['label'] = label
|
|
|
|
| 317 |
# if multiplicity_to:
|
| 318 |
# edge_attrs['headlabel'] = multiplicity_to
|
| 319 |
|
| 320 |
+
# # Configurar estilos de flecha según el tipo de relación
|
| 321 |
# if rel_type == 'inheritance':
|
| 322 |
# edge_attrs['arrowhead'] = 'empty'
|
| 323 |
+
# edge_attrs['color'] = 'black'
|
| 324 |
# elif rel_type == 'composition':
|
| 325 |
# edge_attrs['arrowhead'] = 'normal'
|
| 326 |
# edge_attrs['arrowtail'] = 'diamond'
|
| 327 |
# edge_attrs['dir'] = 'both'
|
| 328 |
+
# edge_attrs['color'] = 'black'
|
| 329 |
# elif rel_type == 'aggregation':
|
| 330 |
# edge_attrs['arrowhead'] = 'normal'
|
| 331 |
# edge_attrs['arrowtail'] = 'odiamond'
|
| 332 |
# edge_attrs['dir'] = 'both'
|
| 333 |
+
# edge_attrs['color'] = 'black'
|
| 334 |
# elif rel_type == 'realization':
|
| 335 |
# edge_attrs['arrowhead'] = 'empty'
|
| 336 |
# edge_attrs['style'] = 'dashed'
|
| 337 |
+
# edge_attrs['color'] = 'black'
|
| 338 |
# elif rel_type == 'dependency':
|
| 339 |
# edge_attrs['arrowhead'] = 'normal'
|
| 340 |
# edge_attrs['style'] = 'dashed'
|
| 341 |
+
# edge_attrs['color'] = 'black'
|
| 342 |
+
# else: # association
|
| 343 |
# edge_attrs['arrowhead'] = 'normal'
|
| 344 |
+
# edge_attrs['color'] = 'black'
|
| 345 |
|
| 346 |
# dot.edge(from_class, to_class, **edge_attrs)
|
| 347 |
|
| 348 |
+
# # Renderizar el diagrama
|
| 349 |
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 350 |
# dot.render(tmp.name, format=output_format, cleanup=True)
|
| 351 |
# return f"{tmp.name}.{output_format}"
|
|
|
|
| 556 |
if 'classes' not in data:
|
| 557 |
raise ValueError("Missing required field: classes")
|
| 558 |
|
| 559 |
+
# Configuración del diagrama con líneas estrictamente rectas
|
| 560 |
dot = graphviz.Digraph(comment='Class Diagram')
|
| 561 |
dot.attr(rankdir='TB', bgcolor='white', pad='0.5', nodesep='0.8', ranksep='1.2')
|
| 562 |
dot.attr('node', shape='plaintext', fontname='Arial', fontsize='11')
|
| 563 |
dot.attr('edge', color='black', fontname='Arial', fontsize='9', minlen='1')
|
| 564 |
+
dot.attr('graph', splines='none') # Sin curvas, solo líneas rectas
|
| 565 |
|
| 566 |
classes = data.get('classes', [])
|
| 567 |
relationships = data.get('relationships', [])
|
|
|
|
| 661 |
continue
|
| 662 |
|
| 663 |
edge_attrs = {
|
| 664 |
+
'splines': 'none', # Sin curvas, líneas completamente rectas
|
| 665 |
'concentrate': 'false', # Evita que las líneas se combinen
|
| 666 |
'constraint': 'true' # Mantiene la estructura jerárquica
|
| 667 |
}
|