ZahirJS commited on
Commit
c0d2211
·
verified ·
1 Parent(s): 5adf149

Update entity_relationship_generator.py

Browse files
Files changed (1) hide show
  1. entity_relationship_generator.py +70 -110
entity_relationship_generator.py CHANGED
@@ -310,36 +310,27 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
310
 
311
  Args:
312
  json_input (str): A JSON string describing the entity relationship diagram structure.
313
- It must follow the Expected JSON Format Example below.
314
 
315
  Expected JSON Format Example:
316
  {
317
  "entities": [
318
  {
319
- "name": "EMPLEADO",
320
- "type": "strong",
321
- "attributes": [
322
- {"name": "num_empleado", "type": "primary_key"},
323
- {"name": "nombre", "type": "regular"}
324
- ]
325
  },
326
  {
327
- "name": "DEPARTAMENTO",
328
- "type": "strong",
329
- "attributes": [
330
- {"name": "cod_depto", "type": "primary_key"},
331
- {"name": "nombre_depto", "type": "regular"}
332
- ]
333
  }
334
  ],
335
  "relationships": [
336
  {
337
- "name": "Pertenece",
338
  "type": "regular",
339
- "entities": ["EMPLEADO", "DEPARTAMENTO"],
340
  "cardinalities": {
341
- "EMPLEADO": "N",
342
- "DEPARTAMENTO": "1"
343
  }
344
  }
345
  ]
@@ -357,22 +348,22 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
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')
@@ -381,13 +372,13 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
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',
@@ -402,133 +393,102 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
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 (sin cardinalidad)
439
- dot.edge(parent, rel_name, len='1.5', color='#404040', penwidth='2')
440
 
441
- # Conectar ISA con hijos (sin cardinalidad)
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 posicionadas correctamente
460
- if len(entities_involved) >= 2:
461
- entity1, entity2 = entities_involved[0], entities_involved[1]
462
- card1 = cardinalities.get(entity1, '1')
463
- card2 = cardinalities.get(entity2, '1')
464
-
465
- # Primera entidad -> relación (cardinalidad cerca de la entidad)
466
- dot.edge(
467
- entity1,
468
  rel_name,
469
- taillabel=card1,
470
- len='2.0',
 
 
471
  color='#404040',
472
- penwidth='2',
473
- fontcolor='#000000',
474
- fontsize='12'
475
  )
476
-
477
- # Relación -> segunda entidad (cardinalidad cerca de la entidad)
478
- dot.edge(
479
- rel_name,
480
- entity2,
481
- headlabel=card2,
482
- len='2.0',
 
483
  color='#404040',
484
- penwidth='2',
485
- fontcolor='#000000',
486
- fontsize='12'
487
  )
488
 
489
- else:
490
- # Relación regular - rombo simple
491
- dot.node(
492
  rel_name,
493
- rel_name,
494
- shape='diamond',
495
- style='filled',
496
- fillcolor='#d8d8d8',
497
  color='#404040',
498
  penwidth='2',
499
- width='1.8',
500
- height='1.0'
501
  )
502
 
503
- # Conectar entidades con cardinalidades posicionadas correctamente
504
- if len(entities_involved) >= 2:
505
- entity1, entity2 = entities_involved[0], entities_involved[1]
506
- card1 = cardinalities.get(entity1, '1')
507
- card2 = cardinalities.get(entity2, '1')
508
-
509
- # Primera entidad -> relación (cardinalidad cerca de la entidad)
510
- dot.edge(
511
- entity1,
512
- rel_name,
513
- taillabel=card1,
514
- len='2.0',
515
- color='#404040',
516
- penwidth='2',
517
- fontcolor='#000000',
518
- fontsize='12'
519
- )
520
-
521
- # Relación -> segunda entidad (cardinalidad cerca de la entidad)
522
- dot.edge(
523
- rel_name,
524
- entity2,
525
- headlabel=card2,
526
- len='2.0',
527
- color='#404040',
528
- penwidth='2',
529
- fontcolor='#000000',
530
- fontsize='12'
531
- )
532
 
533
  # Renderizar
534
  with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
 
310
 
311
  Args:
312
  json_input (str): A JSON string describing the entity relationship diagram structure.
 
313
 
314
  Expected JSON Format Example:
315
  {
316
  "entities": [
317
  {
318
+ "name": "CUSTOMER",
319
+ "type": "strong"
 
 
 
 
320
  },
321
  {
322
+ "name": "ORDER",
323
+ "type": "strong"
 
 
 
 
324
  }
325
  ],
326
  "relationships": [
327
  {
328
+ "name": "Places",
329
  "type": "regular",
330
+ "entities": ["CUSTOMER", "ORDER"],
331
  "cardinalities": {
332
+ "CUSTOMER": "1",
333
+ "ORDER": "N"
334
  }
335
  }
336
  ]
 
348
  if 'entities' not in data:
349
  raise ValueError("Missing required field: entities")
350
 
351
+ # Configuración simple y funcional
352
  dot = graphviz.Graph(comment='ER Diagram', engine='neato')
353
  dot.attr(
354
  bgcolor='white',
355
  pad='1.0',
356
  overlap='false',
357
  splines='true',
358
+ sep='+15'
359
  )
360
+ dot.attr('node', fontname='Arial', fontsize='11', color='#404040')
361
+ dot.attr('edge', fontname='Arial', fontsize='10', color='#404040')
362
 
363
  entities = data.get('entities', [])
364
  relationships = data.get('relationships', [])
365
 
366
+ # Crear solo las entidades principales (sin atributos por ahora)
367
  for entity in entities:
368
  entity_name = entity.get('name')
369
  entity_type = entity.get('type', 'strong')
 
372
  continue
373
 
374
  if entity_type == 'weak':
375
+ # Entidad débil: rectángulo con doble borde
376
  dot.node(
377
  entity_name,
378
  entity_name,
379
  shape='box',
380
+ style='filled',
381
+ fillcolor='#e8e8e8',
382
  color='#404040',
383
  penwidth='3',
384
  width='1.5',
 
393
  style='filled',
394
  fillcolor='#e8e8e8',
395
  color='#404040',
396
+ penwidth='1',
397
  width='1.5',
398
  height='0.8'
399
  )
400
 
401
+ # Crear relaciones
402
  for relationship in relationships:
403
  rel_name = relationship.get('name')
404
  rel_type = relationship.get('type', 'regular')
405
  entities_involved = relationship.get('entities', [])
406
  cardinalities = relationship.get('cardinalities', {})
407
 
408
+ if not rel_name:
409
  continue
410
 
411
  if rel_type == 'isa':
412
+ # Relación ISA - crear triángulo y conectar correctamente
413
  parent = relationship.get('parent')
414
  children = relationship.get('children', [])
415
 
416
  if parent and children:
417
+ # Crear triángulo ISA
418
+ isa_node = f"ISA_{rel_name}"
419
  dot.node(
420
+ isa_node,
421
  'ISA',
422
  shape='triangle',
423
  style='filled',
424
  fillcolor='#d0d0d0',
425
  color='#404040',
426
  penwidth='2',
427
+ width='0.8',
428
  height='0.6'
429
  )
430
 
431
+ # Conectar padre con triángulo ISA
432
+ dot.edge(parent, isa_node, color='#404040', penwidth='2')
433
 
434
+ # Conectar triángulo ISA con cada hijo
435
  for child in children:
436
+ dot.edge(isa_node, child, color='#404040', penwidth='2')
437
 
438
+ elif len(entities_involved) >= 2:
439
+ # Relaciones regulares e identificadoras
440
+ entity1, entity2 = entities_involved[0], entities_involved[1]
441
+ card1 = cardinalities.get(entity1, '1')
442
+ card2 = cardinalities.get(entity2, '1')
 
 
 
 
 
 
 
 
443
 
444
+ if rel_type == 'identifying':
445
+ # Relación identificadora - rombo con doble borde
446
+ dot.node(
 
 
 
 
 
 
447
  rel_name,
448
+ rel_name,
449
+ shape='diamond',
450
+ style='filled',
451
+ fillcolor='#d8d8d8',
452
  color='#404040',
453
+ penwidth='3',
454
+ width='1.5',
455
+ height='0.8'
456
  )
457
+ else:
458
+ # Relación regular - rombo simple
459
+ dot.node(
460
+ rel_name,
461
+ rel_name,
462
+ shape='diamond',
463
+ style='filled',
464
+ fillcolor='#d8d8d8',
465
  color='#404040',
466
+ penwidth='1',
467
+ width='1.5',
468
+ height='0.8'
469
  )
470
 
471
+ # Conectar primera entidad con relación
472
+ dot.edge(
473
+ entity1,
474
  rel_name,
475
+ taillabel=f' {card1} ',
 
 
 
476
  color='#404040',
477
  penwidth='2',
478
+ fontcolor='#000000',
479
+ fontsize='12'
480
  )
481
 
482
+ # Conectar relación con segunda entidad
483
+ dot.edge(
484
+ rel_name,
485
+ entity2,
486
+ headlabel=f' {card2} ',
487
+ color='#404040',
488
+ penwidth='2',
489
+ fontcolor='#000000',
490
+ fontsize='12'
491
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
 
493
  # Renderizar
494
  with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp: