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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import graphviz
|
| 2 |
+
import json
|
| 3 |
+
from tempfile import NamedTemporaryFile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def generate_entity_relationship_diagram(json_input: str, output_format: str) -> str:
|
| 7 |
+
try:
|
| 8 |
+
if not json_input.strip():
|
| 9 |
+
return "Error: Empty input"
|
| 10 |
+
|
| 11 |
+
data = json.loads(json_input)
|
| 12 |
+
|
| 13 |
+
if 'entities' not in data:
|
| 14 |
+
raise ValueError("Missing required field: entities")
|
| 15 |
+
|
| 16 |
+
dot = graphviz.Graph(
|
| 17 |
+
name='ERDiagram',
|
| 18 |
+
format='png',
|
| 19 |
+
graph_attr={
|
| 20 |
+
'rankdir': 'TB',
|
| 21 |
+
'splines': 'ortho',
|
| 22 |
+
'bgcolor': 'white',
|
| 23 |
+
'pad': '0.5',
|
| 24 |
+
'nodesep': '2.0',
|
| 25 |
+
'ranksep': '2.5'
|
| 26 |
+
}
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
base_color = '#19191a'
|
| 30 |
+
lightening_factor = 0.15
|
| 31 |
+
|
| 32 |
+
entities = data.get('entities', [])
|
| 33 |
+
relationships = data.get('relationships', [])
|
| 34 |
+
|
| 35 |
+
for i, entity in enumerate(entities):
|
| 36 |
+
entity_name = entity.get('name')
|
| 37 |
+
entity_type = entity.get('type', 'strong')
|
| 38 |
+
attributes = entity.get('attributes', [])
|
| 39 |
+
|
| 40 |
+
if not entity_name:
|
| 41 |
+
raise ValueError(f"Invalid entity: {entity}")
|
| 42 |
+
|
| 43 |
+
current_depth = i % 6
|
| 44 |
+
|
| 45 |
+
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
| 46 |
+
base_color_safe = '#19191a'
|
| 47 |
+
else:
|
| 48 |
+
base_color_safe = base_color
|
| 49 |
+
|
| 50 |
+
base_r = int(base_color_safe[1:3], 16)
|
| 51 |
+
base_g = int(base_color_safe[3:5], 16)
|
| 52 |
+
base_b = int(base_color_safe[5:7], 16)
|
| 53 |
+
|
| 54 |
+
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
| 55 |
+
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
| 56 |
+
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
| 57 |
+
|
| 58 |
+
current_r = min(255, current_r)
|
| 59 |
+
current_g = min(255, current_g)
|
| 60 |
+
current_b = min(255, current_b)
|
| 61 |
+
|
| 62 |
+
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 63 |
+
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
| 64 |
+
|
| 65 |
+
entity_label = f"{entity_name}\\n"
|
| 66 |
+
|
| 67 |
+
if attributes:
|
| 68 |
+
primary_keys = []
|
| 69 |
+
foreign_keys = []
|
| 70 |
+
regular_attrs = []
|
| 71 |
+
|
| 72 |
+
for attr in attributes:
|
| 73 |
+
attr_name = attr.get('name', '')
|
| 74 |
+
attr_type = attr.get('type', 'key')
|
| 75 |
+
is_multivalued = attr.get('multivalued', False)
|
| 76 |
+
is_derived = attr.get('derived', False)
|
| 77 |
+
is_composite = attr.get('composite', False)
|
| 78 |
+
|
| 79 |
+
if attr_type == 'primary_key':
|
| 80 |
+
if is_multivalued:
|
| 81 |
+
primary_keys.append(f"{{{{ {attr_name} }}}}")
|
| 82 |
+
else:
|
| 83 |
+
primary_keys.append(f"[PK] {attr_name}")
|
| 84 |
+
elif attr_type == 'foreign_key':
|
| 85 |
+
foreign_keys.append(f"[FK] {attr_name}")
|
| 86 |
+
else:
|
| 87 |
+
attr_display = attr_name
|
| 88 |
+
if is_derived:
|
| 89 |
+
attr_display = f"/ {attr_display} /"
|
| 90 |
+
if is_multivalued:
|
| 91 |
+
attr_display = f"{{ {attr_display} }}"
|
| 92 |
+
if is_composite:
|
| 93 |
+
attr_display = f"( {attr_display} )"
|
| 94 |
+
regular_attrs.append(attr_display)
|
| 95 |
+
|
| 96 |
+
if primary_keys:
|
| 97 |
+
entity_label += "\\n".join(primary_keys) + "\\n"
|
| 98 |
+
if foreign_keys:
|
| 99 |
+
entity_label += "\\n".join(foreign_keys) + "\\n"
|
| 100 |
+
if regular_attrs:
|
| 101 |
+
entity_label += "\\n".join(regular_attrs)
|
| 102 |
+
|
| 103 |
+
if entity_type == 'weak':
|
| 104 |
+
shape = 'doubleoctagon'
|
| 105 |
+
style = 'filled'
|
| 106 |
+
else:
|
| 107 |
+
shape = 'box'
|
| 108 |
+
style = 'filled,rounded'
|
| 109 |
+
|
| 110 |
+
dot.node(
|
| 111 |
+
entity_name,
|
| 112 |
+
entity_label,
|
| 113 |
+
shape=shape,
|
| 114 |
+
style=style,
|
| 115 |
+
fillcolor=node_color,
|
| 116 |
+
fontcolor=font_color,
|
| 117 |
+
fontsize='10',
|
| 118 |
+
fontname='Helvetica'
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
for relationship in relationships:
|
| 122 |
+
rel_name = relationship.get('name')
|
| 123 |
+
rel_type = relationship.get('type', 'regular')
|
| 124 |
+
entities_involved = relationship.get('entities', [])
|
| 125 |
+
cardinalities = relationship.get('cardinalities', {})
|
| 126 |
+
|
| 127 |
+
if not rel_name or len(entities_involved) < 2:
|
| 128 |
+
raise ValueError(f"Invalid relationship: {relationship}")
|
| 129 |
+
|
| 130 |
+
rel_node_color = '#e6f3ff'
|
| 131 |
+
|
| 132 |
+
if rel_type == 'identifying':
|
| 133 |
+
rel_shape = 'diamond'
|
| 134 |
+
rel_style = 'filled,bold'
|
| 135 |
+
rel_color = '#4a90e2'
|
| 136 |
+
elif rel_type == 'weak':
|
| 137 |
+
rel_shape = 'diamond'
|
| 138 |
+
rel_style = 'filled,dashed'
|
| 139 |
+
rel_color = '#a0a0a0'
|
| 140 |
+
else:
|
| 141 |
+
rel_shape = 'diamond'
|
| 142 |
+
rel_style = 'filled'
|
| 143 |
+
rel_color = '#4a90e2'
|
| 144 |
+
|
| 145 |
+
dot.node(
|
| 146 |
+
rel_name,
|
| 147 |
+
rel_name,
|
| 148 |
+
shape=rel_shape,
|
| 149 |
+
style=rel_style,
|
| 150 |
+
fillcolor=rel_color,
|
| 151 |
+
fontcolor='white',
|
| 152 |
+
fontsize='10',
|
| 153 |
+
fontname='Helvetica'
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
for entity in entities_involved:
|
| 157 |
+
cardinality = cardinalities.get(entity, '1')
|
| 158 |
+
|
| 159 |
+
edge_label = cardinality
|
| 160 |
+
if cardinality in ['1:1', '1:N', 'M:N', '1', 'N', 'M']:
|
| 161 |
+
pass
|
| 162 |
+
else:
|
| 163 |
+
edge_label = cardinality
|
| 164 |
+
|
| 165 |
+
dot.edge(
|
| 166 |
+
entity,
|
| 167 |
+
rel_name,
|
| 168 |
+
label=edge_label,
|
| 169 |
+
color='#4a4a4a',
|
| 170 |
+
fontsize='9',
|
| 171 |
+
fontcolor='#4a4a4a'
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 175 |
+
dot.render(tmp.name, format=output_format, cleanup=True)
|
| 176 |
+
return f"{tmp.name}.{output_format}"
|
| 177 |
+
|
| 178 |
+
except json.JSONDecodeError:
|
| 179 |
+
return "Error: Invalid JSON format"
|
| 180 |
+
except Exception as e:
|
| 181 |
+
return f"Error: {str(e)}"
|