File size: 8,618 Bytes
f5754cf 836921f 1657843 cf67717 836921f cf67717 836921f 1657843 836921f f5754cf 0e101d5 f5754cf 836921f f5754cf 7474f3d f5754cf cf67717 c0d2211 836921f cf67717 836921f cf67717 1657843 cf67717 836921f f726186 836921f 0ff231b 836921f 0ff231b 836921f 0ff231b 836921f f726186 836921f f726186 836921f f726186 836921f f726186 836921f 505ff60 836921f f5754cf cf59592 |
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 |
import graphviz
import json
from tempfile import NamedTemporaryFile
import os
def generate_entity_relationship_diagram(json_input: str, output_format: str) -> str:
try:
if not json_input.strip():
return "Error: Empty input"
data = json.loads(json_input)
if 'entities' not in data:
raise ValueError("Missing required field: entities")
dot = graphviz.Graph(comment='ER Diagram', engine='neato')
dot.attr(
bgcolor='white',
pad='1.5',
overlap='false',
splines='true',
sep='+25',
esep='+15'
)
dot.attr('node', fontname='Arial', fontsize='10', color='#404040')
dot.attr('edge', fontname='Arial', fontsize='9', color='#404040')
entities = data.get('entities', [])
relationships = data.get('relationships', [])
for entity in entities:
entity_name = entity.get('name')
entity_type = entity.get('type', 'strong')
attributes = entity.get('attributes', [])
if not entity_name:
continue
if entity_type == 'weak':
dot.node(
entity_name,
entity_name,
shape='box',
style='filled',
fillcolor='#f0f0f0',
color='#404040',
penwidth='3',
width='1.8',
height='0.8'
)
else:
dot.node(
entity_name,
entity_name,
shape='box',
style='filled',
fillcolor='#e8e8e8',
color='#404040',
penwidth='1',
width='1.8',
height='0.8'
)
for i, attr in enumerate(attributes):
attr_name = attr.get('name', '')
attr_type = attr.get('type', 'regular')
attr_id = f"{entity_name}_attr_{i}"
# CAMBIO CLAVE: Removemos HTML markup que puede causar problemas
if attr_type == 'primary_key':
dot.node(
attr_id,
f'{attr_name} (PK)',
shape='ellipse',
style='filled',
fillcolor='#d8d8d8',
color='#404040',
width='1.2',
height='0.6'
)
elif attr_type == 'partial_key':
dot.node(
attr_id,
f'{attr_name} (Partial)',
shape='ellipse',
style='filled,dashed',
fillcolor='#d8d8d8',
color='#404040',
width='1.2',
height='0.6'
)
elif attr_type == 'multivalued':
dot.node(
attr_id,
attr_name,
shape='ellipse',
style='filled',
fillcolor='#d8d8d8',
color='#404040',
penwidth='3',
width='1.2',
height='0.6'
)
elif attr_type == 'derived':
dot.node(
attr_id,
f'/{attr_name}/',
shape='ellipse',
style='filled,dashed',
fillcolor='#d8d8d8',
color='#404040',
width='1.2',
height='0.6'
)
elif attr_type == 'composite':
dot.node(
attr_id,
attr_name,
shape='ellipse',
style='filled',
fillcolor='#d8d8d8',
color='#404040',
width='1.2',
height='0.6'
)
else:
dot.node(
attr_id,
attr_name,
shape='ellipse',
style='filled',
fillcolor='#d8d8d8',
color='#404040',
width='1.2',
height='0.6'
)
dot.edge(entity_name, attr_id, color='#404040', len='1.5')
for relationship in relationships:
rel_name = relationship.get('name')
rel_type = relationship.get('type', 'regular')
entities_involved = relationship.get('entities', [])
cardinalities = relationship.get('cardinalities', {})
rel_attributes = relationship.get('attributes', [])
if not rel_name:
continue
if rel_type == 'isa':
parent = relationship.get('parent')
children = relationship.get('children', [])
if parent and children:
isa_id = f"isa_{rel_name}"
dot.node(
isa_id,
'ISA',
shape='triangle',
style='filled',
fillcolor='#d0d0d0',
color='#404040',
penwidth='2',
width='1.0',
height='0.8'
)
dot.edge(parent, isa_id, color='#404040', len='2.0')
for child in children:
dot.edge(isa_id, child, color='#404040', len='2.0')
elif len(entities_involved) >= 2:
if rel_type == 'identifying':
dot.node(
rel_name,
rel_name,
shape='diamond',
style='filled',
fillcolor='#c8c8c8',
color='#404040',
penwidth='3',
width='1.8',
height='1.0'
)
else:
dot.node(
rel_name,
rel_name,
shape='diamond',
style='filled',
fillcolor='#c8c8c8',
color='#404040',
penwidth='1',
width='1.8',
height='1.0'
)
for j, attr in enumerate(rel_attributes):
attr_name = attr.get('name', '')
attr_id = f"{rel_name}_attr_{j}"
dot.node(
attr_id,
attr_name,
shape='ellipse',
style='filled',
fillcolor='#d8d8d8',
color='#404040',
width='1.0',
height='0.5'
)
dot.edge(rel_name, attr_id, color='#404040', len='1.0')
for entity in entities_involved:
cardinality = cardinalities.get(entity, '1')
dot.edge(
entity,
rel_name,
label=f' {cardinality} ',
color='#404040',
len='2.5',
fontcolor='#000000',
fontsize='11'
)
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)}" |