Spaces:
Running
Running
Update entity_relationship_generator.py
Browse files- entity_relationship_generator.py +101 -91
entity_relationship_generator.py
CHANGED
|
@@ -319,9 +319,8 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
|
|
| 319 |
"name": "EMPLEADO",
|
| 320 |
"type": "strong",
|
| 321 |
"attributes": [
|
| 322 |
-
{"name": "
|
| 323 |
-
{"name": "nombre", "type": "regular"}
|
| 324 |
-
{"name": "direccion", "type": "regular"}
|
| 325 |
]
|
| 326 |
},
|
| 327 |
{
|
|
@@ -358,137 +357,148 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
|
|
| 358 |
if 'entities' not in data:
|
| 359 |
raise ValueError("Missing required field: entities")
|
| 360 |
|
| 361 |
-
#
|
| 362 |
-
dot = graphviz.
|
| 363 |
dot.attr(
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
splines='ortho'
|
| 370 |
)
|
| 371 |
-
dot.attr('node', fontname='Arial', fontsize='
|
| 372 |
-
dot.attr('edge', fontname='Arial', fontsize='
|
| 373 |
|
| 374 |
entities = data.get('entities', [])
|
| 375 |
relationships = data.get('relationships', [])
|
| 376 |
|
| 377 |
-
# Crear
|
| 378 |
for entity in entities:
|
| 379 |
entity_name = entity.get('name')
|
| 380 |
entity_type = entity.get('type', 'strong')
|
| 381 |
-
attributes = entity.get('attributes', [])
|
| 382 |
|
| 383 |
if not entity_name:
|
| 384 |
continue
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
attr_rows.append(f'<TR><TD>{{{attr_name}}}</TD></TR>')
|
| 400 |
-
elif attr_type == 'derived':
|
| 401 |
-
attr_rows.append(f'<TR><TD>/{attr_name}/</TD></TR>')
|
| 402 |
-
else:
|
| 403 |
-
attr_rows.append(f'<TR><TD>{attr_name}</TD></TR>')
|
| 404 |
-
|
| 405 |
-
attr_html = ''.join(attr_rows)
|
| 406 |
-
|
| 407 |
-
if entity_type == 'weak':
|
| 408 |
-
# Entidad débil con doble borde
|
| 409 |
-
label = f'<<TABLE BORDER="3" CELLBORDER="1" CELLSPACING="0" CELLPADDING="6" BGCOLOR="#e8e8e8"><TR><TD COLSPAN="1"><B>{entity_name}</B></TD></TR><HR/>{attr_html}</TABLE>>'
|
| 410 |
-
else:
|
| 411 |
-
# Entidad fuerte
|
| 412 |
-
label = f'<<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="6" BGCOLOR="#e8e8e8"><TR><TD COLSPAN="1"><B>{entity_name}</B></TD></TR><HR/>{attr_html}</TABLE>>'
|
| 413 |
-
|
| 414 |
-
dot.node(entity_name, label, shape='plaintext')
|
| 415 |
else:
|
| 416 |
-
# Entidad
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
| 424 |
-
# Crear relaciones
|
| 425 |
for relationship in relationships:
|
| 426 |
rel_name = relationship.get('name')
|
| 427 |
rel_type = relationship.get('type', 'regular')
|
| 428 |
entities_involved = relationship.get('entities', [])
|
| 429 |
cardinalities = relationship.get('cardinalities', {})
|
| 430 |
-
rel_attributes = relationship.get('attributes', [])
|
| 431 |
|
| 432 |
if not rel_name or len(entities_involved) < 2:
|
| 433 |
continue
|
| 434 |
|
| 435 |
if rel_type == 'isa':
|
| 436 |
-
# Relación ISA
|
| 437 |
parent = relationship.get('parent')
|
| 438 |
children = relationship.get('children', [])
|
| 439 |
|
| 440 |
if parent and children:
|
| 441 |
-
dot.node(
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
-
|
|
|
|
| 445 |
|
|
|
|
| 446 |
for child in children:
|
| 447 |
-
dot.edge(rel_name, child,
|
| 448 |
|
| 449 |
elif rel_type == 'identifying':
|
| 450 |
-
# Relación identificadora
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
dot.node(rel_name, rel_name, shape='diamond', style='filled',
|
| 463 |
-
fillcolor='#c8c8c8', color='#404040', penwidth='3')
|
| 464 |
|
| 465 |
-
# Conectar entidades
|
| 466 |
for entity in entities_involved:
|
| 467 |
cardinality = cardinalities.get(entity, '1')
|
| 468 |
-
dot.edge(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
else:
|
| 471 |
-
# Relación regular
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
dot.node(rel_name, rel_name, shape='diamond', style='filled',
|
| 484 |
-
fillcolor='#c8c8c8', color='#404040')
|
| 485 |
|
| 486 |
-
# Conectar entidades
|
| 487 |
for entity in entities_involved:
|
| 488 |
cardinality = cardinalities.get(entity, '1')
|
| 489 |
-
dot.edge(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
-
# Renderizar
|
| 492 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 493 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
| 494 |
return f"{tmp.name}.{output_format}"
|
|
|
|
| 319 |
"name": "EMPLEADO",
|
| 320 |
"type": "strong",
|
| 321 |
"attributes": [
|
| 322 |
+
{"name": "num_empleado", "type": "primary_key"},
|
| 323 |
+
{"name": "nombre", "type": "regular"}
|
|
|
|
| 324 |
]
|
| 325 |
},
|
| 326 |
{
|
|
|
|
| 357 |
if 'entities' not in data:
|
| 358 |
raise ValueError("Missing required field: entities")
|
| 359 |
|
| 360 |
+
# Configuración simple y limpia
|
| 361 |
+
dot = graphviz.Graph(comment='ER Diagram', engine='neato')
|
| 362 |
dot.attr(
|
| 363 |
+
bgcolor='white',
|
| 364 |
+
pad='1.0',
|
| 365 |
+
overlap='false',
|
| 366 |
+
splines='true',
|
| 367 |
+
sep='+25'
|
|
|
|
| 368 |
)
|
| 369 |
+
dot.attr('node', fontname='Arial', fontsize='11', color='#505050')
|
| 370 |
+
dot.attr('edge', fontname='Arial', fontsize='10', color='#505050')
|
| 371 |
|
| 372 |
entities = data.get('entities', [])
|
| 373 |
relationships = data.get('relationships', [])
|
| 374 |
|
| 375 |
+
# Crear solo las entidades principales (sin atributos separados)
|
| 376 |
for entity in entities:
|
| 377 |
entity_name = entity.get('name')
|
| 378 |
entity_type = entity.get('type', 'strong')
|
|
|
|
| 379 |
|
| 380 |
if not entity_name:
|
| 381 |
continue
|
| 382 |
|
| 383 |
+
if entity_type == 'weak':
|
| 384 |
+
# Entidad débil: doble rectángulo
|
| 385 |
+
dot.node(
|
| 386 |
+
entity_name,
|
| 387 |
+
entity_name,
|
| 388 |
+
shape='box',
|
| 389 |
+
style='filled,bold',
|
| 390 |
+
fillcolor='#f0f0f0',
|
| 391 |
+
color='#404040',
|
| 392 |
+
penwidth='3',
|
| 393 |
+
width='1.5',
|
| 394 |
+
height='0.8'
|
| 395 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
else:
|
| 397 |
+
# Entidad fuerte: rectángulo simple
|
| 398 |
+
dot.node(
|
| 399 |
+
entity_name,
|
| 400 |
+
entity_name,
|
| 401 |
+
shape='box',
|
| 402 |
+
style='filled',
|
| 403 |
+
fillcolor='#e8e8e8',
|
| 404 |
+
color='#404040',
|
| 405 |
+
penwidth='2',
|
| 406 |
+
width='1.5',
|
| 407 |
+
height='0.8'
|
| 408 |
+
)
|
| 409 |
|
| 410 |
+
# Crear relaciones simples
|
| 411 |
for relationship in relationships:
|
| 412 |
rel_name = relationship.get('name')
|
| 413 |
rel_type = relationship.get('type', 'regular')
|
| 414 |
entities_involved = relationship.get('entities', [])
|
| 415 |
cardinalities = relationship.get('cardinalities', {})
|
|
|
|
| 416 |
|
| 417 |
if not rel_name or len(entities_involved) < 2:
|
| 418 |
continue
|
| 419 |
|
| 420 |
if rel_type == 'isa':
|
| 421 |
+
# Relación ISA - triángulo
|
| 422 |
parent = relationship.get('parent')
|
| 423 |
children = relationship.get('children', [])
|
| 424 |
|
| 425 |
if parent and children:
|
| 426 |
+
dot.node(
|
| 427 |
+
rel_name,
|
| 428 |
+
'ISA',
|
| 429 |
+
shape='triangle',
|
| 430 |
+
style='filled',
|
| 431 |
+
fillcolor='#d0d0d0',
|
| 432 |
+
color='#404040',
|
| 433 |
+
penwidth='2',
|
| 434 |
+
width='1.0',
|
| 435 |
+
height='0.6'
|
| 436 |
+
)
|
| 437 |
|
| 438 |
+
# Conectar padre con ISA
|
| 439 |
+
dot.edge(parent, rel_name, len='1.5', color='#404040', penwidth='2')
|
| 440 |
|
| 441 |
+
# Conectar ISA con hijos
|
| 442 |
for child in children:
|
| 443 |
+
dot.edge(rel_name, child, len='1.5', color='#404040', penwidth='2')
|
| 444 |
|
| 445 |
elif rel_type == 'identifying':
|
| 446 |
+
# Relación identificadora - doble rombo
|
| 447 |
+
dot.node(
|
| 448 |
+
rel_name,
|
| 449 |
+
rel_name,
|
| 450 |
+
shape='diamond',
|
| 451 |
+
style='filled,bold',
|
| 452 |
+
fillcolor='#d8d8d8',
|
| 453 |
+
color='#404040',
|
| 454 |
+
penwidth='3',
|
| 455 |
+
width='1.8',
|
| 456 |
+
height='1.0'
|
| 457 |
+
)
|
|
|
|
|
|
|
| 458 |
|
| 459 |
+
# Conectar entidades con cardinalidades
|
| 460 |
for entity in entities_involved:
|
| 461 |
cardinality = cardinalities.get(entity, '1')
|
| 462 |
+
dot.edge(
|
| 463 |
+
entity,
|
| 464 |
+
rel_name,
|
| 465 |
+
label=f' {cardinality} ',
|
| 466 |
+
len='2.0',
|
| 467 |
+
color='#404040',
|
| 468 |
+
penwidth='2',
|
| 469 |
+
fontcolor='#000000',
|
| 470 |
+
fontsize='12'
|
| 471 |
+
)
|
| 472 |
|
| 473 |
else:
|
| 474 |
+
# Relación regular - rombo simple
|
| 475 |
+
dot.node(
|
| 476 |
+
rel_name,
|
| 477 |
+
rel_name,
|
| 478 |
+
shape='diamond',
|
| 479 |
+
style='filled',
|
| 480 |
+
fillcolor='#d8d8d8',
|
| 481 |
+
color='#404040',
|
| 482 |
+
penwidth='2',
|
| 483 |
+
width='1.8',
|
| 484 |
+
height='1.0'
|
| 485 |
+
)
|
|
|
|
|
|
|
| 486 |
|
| 487 |
+
# Conectar entidades con cardinalidades bien posicionadas
|
| 488 |
for entity in entities_involved:
|
| 489 |
cardinality = cardinalities.get(entity, '1')
|
| 490 |
+
dot.edge(
|
| 491 |
+
entity,
|
| 492 |
+
rel_name,
|
| 493 |
+
label=f' {cardinality} ',
|
| 494 |
+
len='2.0',
|
| 495 |
+
color='#404040',
|
| 496 |
+
penwidth='2',
|
| 497 |
+
fontcolor='#000000',
|
| 498 |
+
fontsize='12'
|
| 499 |
+
)
|
| 500 |
|
| 501 |
+
# Renderizar
|
| 502 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 503 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
| 504 |
return f"{tmp.name}.{output_format}"
|