Update entity_relationship_generator.py
Browse files- entity_relationship_generator.py +448 -181
entity_relationship_generator.py
CHANGED
|
@@ -1,3 +1,304 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import graphviz
|
| 2 |
import json
|
| 3 |
from tempfile import NamedTemporaryFile
|
|
@@ -15,179 +316,110 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
|
|
| 15 |
{
|
| 16 |
"entities": [
|
| 17 |
{
|
| 18 |
-
"name": "
|
| 19 |
-
"type": "strong",
|
| 20 |
-
"attributes": [
|
| 21 |
-
{"name": "customer_id", "type": "primary_key"},
|
| 22 |
-
{"name": "first_name", "type": "regular"},
|
| 23 |
-
{"name": "last_name", "type": "regular"},
|
| 24 |
-
{"name": "email", "type": "regular"},
|
| 25 |
-
{"name": "phone", "type": "multivalued", "multivalued": true},
|
| 26 |
-
{"name": "age", "type": "derived", "derived": true},
|
| 27 |
-
{"name": "address", "type": "composite", "composite": true}
|
| 28 |
-
]
|
| 29 |
-
},
|
| 30 |
-
{
|
| 31 |
-
"name": "Order",
|
| 32 |
-
"type": "strong",
|
| 33 |
-
"attributes": [
|
| 34 |
-
{"name": "order_id", "type": "primary_key"},
|
| 35 |
-
{"name": "customer_id", "type": "foreign_key"},
|
| 36 |
-
{"name": "order_date", "type": "regular"},
|
| 37 |
-
{"name": "total_amount", "type": "regular"},
|
| 38 |
-
{"name": "status", "type": "regular"},
|
| 39 |
-
{"name": "shipping_address", "type": "composite", "composite": true}
|
| 40 |
-
]
|
| 41 |
-
},
|
| 42 |
-
{
|
| 43 |
-
"name": "Product",
|
| 44 |
"type": "strong",
|
| 45 |
"attributes": [
|
| 46 |
-
{"name": "
|
| 47 |
-
{"name": "
|
| 48 |
-
{"name": "
|
| 49 |
-
{"name": "
|
| 50 |
-
{"name": "
|
| 51 |
-
{"name": "
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
{
|
| 55 |
-
"name": "Category",
|
| 56 |
-
"type": "strong",
|
| 57 |
-
"attributes": [
|
| 58 |
-
{"name": "category_id", "type": "primary_key"},
|
| 59 |
-
{"name": "name", "type": "regular"},
|
| 60 |
-
{"name": "description", "type": "regular"},
|
| 61 |
-
{"name": "parent_category_id", "type": "foreign_key"}
|
| 62 |
-
]
|
| 63 |
-
},
|
| 64 |
-
{
|
| 65 |
-
"name": "OrderItem",
|
| 66 |
-
"type": "weak",
|
| 67 |
-
"attributes": [
|
| 68 |
-
{"name": "order_id", "type": "primary_key"},
|
| 69 |
-
{"name": "product_id", "type": "primary_key"},
|
| 70 |
-
{"name": "quantity", "type": "regular"},
|
| 71 |
-
{"name": "unit_price", "type": "regular"},
|
| 72 |
-
{"name": "discount", "type": "regular"}
|
| 73 |
]
|
| 74 |
},
|
| 75 |
{
|
| 76 |
-
"name": "
|
| 77 |
"type": "strong",
|
| 78 |
"attributes": [
|
| 79 |
-
{"name": "
|
| 80 |
-
{"name": "
|
| 81 |
-
{"name": "
|
| 82 |
-
{"name": "amount", "type": "regular"},
|
| 83 |
-
{"name": "payment_date", "type": "regular"},
|
| 84 |
-
{"name": "transaction_id", "type": "regular"}
|
| 85 |
]
|
| 86 |
},
|
| 87 |
{
|
| 88 |
-
"name": "
|
| 89 |
"type": "strong",
|
| 90 |
"attributes": [
|
| 91 |
-
{"name": "
|
| 92 |
-
{"name": "
|
| 93 |
-
{"name": "
|
| 94 |
-
{"name": "email", "type": "regular"},
|
| 95 |
-
{"name": "phone", "type": "multivalued", "multivalued": true},
|
| 96 |
-
{"name": "address", "type": "composite", "composite": true}
|
| 97 |
]
|
| 98 |
},
|
| 99 |
{
|
| 100 |
-
"name": "
|
| 101 |
"type": "weak",
|
| 102 |
"attributes": [
|
| 103 |
-
{"name": "
|
| 104 |
-
{"name": "
|
| 105 |
-
{"name": "
|
| 106 |
-
{"name": "
|
| 107 |
-
{"name": "review_date", "type": "regular"}
|
| 108 |
]
|
| 109 |
}
|
| 110 |
],
|
| 111 |
"relationships": [
|
| 112 |
{
|
| 113 |
-
"name": "
|
| 114 |
-
"type": "regular",
|
| 115 |
-
"entities": ["Customer", "Order"],
|
| 116 |
-
"cardinalities": {
|
| 117 |
-
"Customer": "1",
|
| 118 |
-
"Order": "N"
|
| 119 |
-
}
|
| 120 |
-
},
|
| 121 |
-
{
|
| 122 |
-
"name": "Contains",
|
| 123 |
-
"type": "identifying",
|
| 124 |
-
"entities": ["Order", "OrderItem"],
|
| 125 |
-
"cardinalities": {
|
| 126 |
-
"Order": "1",
|
| 127 |
-
"OrderItem": "N"
|
| 128 |
-
}
|
| 129 |
-
},
|
| 130 |
-
{
|
| 131 |
-
"name": "Includes",
|
| 132 |
"type": "regular",
|
| 133 |
-
"entities": ["
|
| 134 |
"cardinalities": {
|
| 135 |
-
"
|
| 136 |
-
"
|
| 137 |
}
|
| 138 |
},
|
| 139 |
{
|
| 140 |
-
"name": "
|
| 141 |
"type": "regular",
|
| 142 |
-
"entities": ["
|
| 143 |
"cardinalities": {
|
| 144 |
-
"
|
| 145 |
-
"
|
| 146 |
-
}
|
|
|
|
|
|
|
|
|
|
| 147 |
},
|
| 148 |
{
|
| 149 |
-
"name": "
|
| 150 |
"type": "regular",
|
| 151 |
-
"entities": ["
|
| 152 |
"cardinalities": {
|
| 153 |
-
"
|
| 154 |
-
"
|
| 155 |
}
|
| 156 |
},
|
| 157 |
{
|
| 158 |
-
"name": "
|
| 159 |
"type": "regular",
|
| 160 |
-
"entities": ["
|
| 161 |
"cardinalities": {
|
| 162 |
-
"
|
| 163 |
-
"
|
|
|
|
|
|
|
|
|
|
| 164 |
}
|
| 165 |
},
|
| 166 |
{
|
| 167 |
-
"name": "
|
| 168 |
-
"type": "
|
| 169 |
-
"entities": ["
|
| 170 |
"cardinalities": {
|
| 171 |
-
"
|
| 172 |
-
"
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
"type": "weak",
|
| 178 |
-
"entities": ["Review", "Product"],
|
| 179 |
-
"cardinalities": {
|
| 180 |
-
"Review": "N",
|
| 181 |
-
"Product": "1"
|
| 182 |
-
}
|
| 183 |
},
|
| 184 |
{
|
| 185 |
-
"name": "
|
| 186 |
-
"type": "
|
| 187 |
-
"entities": ["
|
| 188 |
"cardinalities": {
|
| 189 |
-
"
|
| 190 |
-
"
|
| 191 |
}
|
| 192 |
}
|
| 193 |
]
|
|
@@ -206,89 +438,124 @@ def generate_entity_relationship_diagram(json_input: str, output_format: str) ->
|
|
| 206 |
raise ValueError("Missing required field: entities")
|
| 207 |
|
| 208 |
dot = graphviz.Graph(comment='ER Diagram', engine='dot')
|
| 209 |
-
dot.attr(rankdir='TB', bgcolor='white', pad='0.5')
|
| 210 |
dot.attr('node', fontname='Arial', fontsize='10', color='black')
|
| 211 |
dot.attr('edge', fontname='Arial', fontsize='9', color='black')
|
| 212 |
|
| 213 |
entities = data.get('entities', [])
|
| 214 |
relationships = data.get('relationships', [])
|
| 215 |
|
|
|
|
| 216 |
for entity in entities:
|
| 217 |
entity_name = entity.get('name')
|
| 218 |
entity_type = entity.get('type', 'strong')
|
| 219 |
-
attributes = entity.get('attributes', [])
|
| 220 |
|
| 221 |
if not entity_name:
|
| 222 |
continue
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
if attributes:
|
| 227 |
-
attr_lines = []
|
| 228 |
-
for attr in attributes:
|
| 229 |
-
attr_name = attr.get('name', '')
|
| 230 |
-
attr_type = attr.get('type', 'regular')
|
| 231 |
-
is_multivalued = attr.get('multivalued', False)
|
| 232 |
-
is_derived = attr.get('derived', False)
|
| 233 |
-
is_composite = attr.get('composite', False)
|
| 234 |
-
|
| 235 |
-
if attr_type == 'primary_key':
|
| 236 |
-
if is_multivalued:
|
| 237 |
-
attr_display = f"PK: {{ {attr_name} }}"
|
| 238 |
-
else:
|
| 239 |
-
attr_display = f"PK: {attr_name}"
|
| 240 |
-
elif attr_type == 'foreign_key':
|
| 241 |
-
attr_display = f"FK: {attr_name}"
|
| 242 |
-
else:
|
| 243 |
-
attr_display = attr_name
|
| 244 |
-
if is_derived:
|
| 245 |
-
attr_display = f"/ {attr_display} /"
|
| 246 |
-
if is_multivalued:
|
| 247 |
-
attr_display = f"{{ {attr_display} }}"
|
| 248 |
-
if is_composite:
|
| 249 |
-
attr_display = f"( {attr_display} )"
|
| 250 |
-
|
| 251 |
-
attr_lines.append(attr_display)
|
| 252 |
-
|
| 253 |
-
if attr_lines:
|
| 254 |
-
label_parts.extend(attr_lines)
|
| 255 |
-
|
| 256 |
-
label = "\\n".join(label_parts)
|
| 257 |
-
|
| 258 |
if entity_type == 'weak':
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
penwidth = '2'
|
| 262 |
else:
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
|
|
|
| 269 |
for relationship in relationships:
|
| 270 |
rel_name = relationship.get('name')
|
| 271 |
rel_type = relationship.get('type', 'regular')
|
| 272 |
entities_involved = relationship.get('entities', [])
|
| 273 |
cardinalities = relationship.get('cardinalities', {})
|
|
|
|
|
|
|
| 274 |
|
| 275 |
if not rel_name or len(entities_involved) < 2:
|
| 276 |
continue
|
| 277 |
|
|
|
|
|
|
|
| 278 |
if rel_type == 'identifying':
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
penwidth = '2'
|
| 282 |
else:
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
penwidth = '1'
|
| 286 |
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 294 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
|
|
|
| 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 |
+
# """
|
| 8 |
+
# Generates an entity relationship diagram from JSON input.
|
| 9 |
+
|
| 10 |
+
# Args:
|
| 11 |
+
# json_input (str): A JSON string describing the entity relationship diagram structure.
|
| 12 |
+
# It must follow the Expected JSON Format Example below.
|
| 13 |
+
|
| 14 |
+
# Expected JSON Format Example:
|
| 15 |
+
# {
|
| 16 |
+
# "entities": [
|
| 17 |
+
# {
|
| 18 |
+
# "name": "Customer",
|
| 19 |
+
# "type": "strong",
|
| 20 |
+
# "attributes": [
|
| 21 |
+
# {"name": "customer_id", "type": "primary_key"},
|
| 22 |
+
# {"name": "first_name", "type": "regular"},
|
| 23 |
+
# {"name": "last_name", "type": "regular"},
|
| 24 |
+
# {"name": "email", "type": "regular"},
|
| 25 |
+
# {"name": "phone", "type": "multivalued", "multivalued": true},
|
| 26 |
+
# {"name": "age", "type": "derived", "derived": true},
|
| 27 |
+
# {"name": "address", "type": "composite", "composite": true}
|
| 28 |
+
# ]
|
| 29 |
+
# },
|
| 30 |
+
# {
|
| 31 |
+
# "name": "Order",
|
| 32 |
+
# "type": "strong",
|
| 33 |
+
# "attributes": [
|
| 34 |
+
# {"name": "order_id", "type": "primary_key"},
|
| 35 |
+
# {"name": "customer_id", "type": "foreign_key"},
|
| 36 |
+
# {"name": "order_date", "type": "regular"},
|
| 37 |
+
# {"name": "total_amount", "type": "regular"},
|
| 38 |
+
# {"name": "status", "type": "regular"},
|
| 39 |
+
# {"name": "shipping_address", "type": "composite", "composite": true}
|
| 40 |
+
# ]
|
| 41 |
+
# },
|
| 42 |
+
# {
|
| 43 |
+
# "name": "Product",
|
| 44 |
+
# "type": "strong",
|
| 45 |
+
# "attributes": [
|
| 46 |
+
# {"name": "product_id", "type": "primary_key"},
|
| 47 |
+
# {"name": "name", "type": "regular"},
|
| 48 |
+
# {"name": "description", "type": "regular"},
|
| 49 |
+
# {"name": "price", "type": "regular"},
|
| 50 |
+
# {"name": "category_id", "type": "foreign_key"},
|
| 51 |
+
# {"name": "stock_quantity", "type": "regular"}
|
| 52 |
+
# ]
|
| 53 |
+
# },
|
| 54 |
+
# {
|
| 55 |
+
# "name": "Category",
|
| 56 |
+
# "type": "strong",
|
| 57 |
+
# "attributes": [
|
| 58 |
+
# {"name": "category_id", "type": "primary_key"},
|
| 59 |
+
# {"name": "name", "type": "regular"},
|
| 60 |
+
# {"name": "description", "type": "regular"},
|
| 61 |
+
# {"name": "parent_category_id", "type": "foreign_key"}
|
| 62 |
+
# ]
|
| 63 |
+
# },
|
| 64 |
+
# {
|
| 65 |
+
# "name": "OrderItem",
|
| 66 |
+
# "type": "weak",
|
| 67 |
+
# "attributes": [
|
| 68 |
+
# {"name": "order_id", "type": "primary_key"},
|
| 69 |
+
# {"name": "product_id", "type": "primary_key"},
|
| 70 |
+
# {"name": "quantity", "type": "regular"},
|
| 71 |
+
# {"name": "unit_price", "type": "regular"},
|
| 72 |
+
# {"name": "discount", "type": "regular"}
|
| 73 |
+
# ]
|
| 74 |
+
# },
|
| 75 |
+
# {
|
| 76 |
+
# "name": "Payment",
|
| 77 |
+
# "type": "strong",
|
| 78 |
+
# "attributes": [
|
| 79 |
+
# {"name": "payment_id", "type": "primary_key"},
|
| 80 |
+
# {"name": "order_id", "type": "foreign_key"},
|
| 81 |
+
# {"name": "payment_method", "type": "regular"},
|
| 82 |
+
# {"name": "amount", "type": "regular"},
|
| 83 |
+
# {"name": "payment_date", "type": "regular"},
|
| 84 |
+
# {"name": "transaction_id", "type": "regular"}
|
| 85 |
+
# ]
|
| 86 |
+
# },
|
| 87 |
+
# {
|
| 88 |
+
# "name": "Supplier",
|
| 89 |
+
# "type": "strong",
|
| 90 |
+
# "attributes": [
|
| 91 |
+
# {"name": "supplier_id", "type": "primary_key"},
|
| 92 |
+
# {"name": "company_name", "type": "regular"},
|
| 93 |
+
# {"name": "contact_person", "type": "regular"},
|
| 94 |
+
# {"name": "email", "type": "regular"},
|
| 95 |
+
# {"name": "phone", "type": "multivalued", "multivalued": true},
|
| 96 |
+
# {"name": "address", "type": "composite", "composite": true}
|
| 97 |
+
# ]
|
| 98 |
+
# },
|
| 99 |
+
# {
|
| 100 |
+
# "name": "Review",
|
| 101 |
+
# "type": "weak",
|
| 102 |
+
# "attributes": [
|
| 103 |
+
# {"name": "customer_id", "type": "primary_key"},
|
| 104 |
+
# {"name": "product_id", "type": "primary_key"},
|
| 105 |
+
# {"name": "rating", "type": "regular"},
|
| 106 |
+
# {"name": "comment", "type": "regular"},
|
| 107 |
+
# {"name": "review_date", "type": "regular"}
|
| 108 |
+
# ]
|
| 109 |
+
# }
|
| 110 |
+
# ],
|
| 111 |
+
# "relationships": [
|
| 112 |
+
# {
|
| 113 |
+
# "name": "Places",
|
| 114 |
+
# "type": "regular",
|
| 115 |
+
# "entities": ["Customer", "Order"],
|
| 116 |
+
# "cardinalities": {
|
| 117 |
+
# "Customer": "1",
|
| 118 |
+
# "Order": "N"
|
| 119 |
+
# }
|
| 120 |
+
# },
|
| 121 |
+
# {
|
| 122 |
+
# "name": "Contains",
|
| 123 |
+
# "type": "identifying",
|
| 124 |
+
# "entities": ["Order", "OrderItem"],
|
| 125 |
+
# "cardinalities": {
|
| 126 |
+
# "Order": "1",
|
| 127 |
+
# "OrderItem": "N"
|
| 128 |
+
# }
|
| 129 |
+
# },
|
| 130 |
+
# {
|
| 131 |
+
# "name": "Includes",
|
| 132 |
+
# "type": "regular",
|
| 133 |
+
# "entities": ["OrderItem", "Product"],
|
| 134 |
+
# "cardinalities": {
|
| 135 |
+
# "OrderItem": "N",
|
| 136 |
+
# "Product": "1"
|
| 137 |
+
# }
|
| 138 |
+
# },
|
| 139 |
+
# {
|
| 140 |
+
# "name": "BelongsTo",
|
| 141 |
+
# "type": "regular",
|
| 142 |
+
# "entities": ["Product", "Category"],
|
| 143 |
+
# "cardinalities": {
|
| 144 |
+
# "Product": "N",
|
| 145 |
+
# "Category": "1"
|
| 146 |
+
# }
|
| 147 |
+
# },
|
| 148 |
+
# {
|
| 149 |
+
# "name": "ProcessedBy",
|
| 150 |
+
# "type": "regular",
|
| 151 |
+
# "entities": ["Order", "Payment"],
|
| 152 |
+
# "cardinalities": {
|
| 153 |
+
# "Order": "1",
|
| 154 |
+
# "Payment": "1"
|
| 155 |
+
# }
|
| 156 |
+
# },
|
| 157 |
+
# {
|
| 158 |
+
# "name": "Supplies",
|
| 159 |
+
# "type": "regular",
|
| 160 |
+
# "entities": ["Supplier", "Product"],
|
| 161 |
+
# "cardinalities": {
|
| 162 |
+
# "Supplier": "N",
|
| 163 |
+
# "Product": "M"
|
| 164 |
+
# }
|
| 165 |
+
# },
|
| 166 |
+
# {
|
| 167 |
+
# "name": "Writes",
|
| 168 |
+
# "type": "weak",
|
| 169 |
+
# "entities": ["Customer", "Review"],
|
| 170 |
+
# "cardinalities": {
|
| 171 |
+
# "Customer": "1",
|
| 172 |
+
# "Review": "N"
|
| 173 |
+
# }
|
| 174 |
+
# },
|
| 175 |
+
# {
|
| 176 |
+
# "name": "ReviewsProduct",
|
| 177 |
+
# "type": "weak",
|
| 178 |
+
# "entities": ["Review", "Product"],
|
| 179 |
+
# "cardinalities": {
|
| 180 |
+
# "Review": "N",
|
| 181 |
+
# "Product": "1"
|
| 182 |
+
# }
|
| 183 |
+
# },
|
| 184 |
+
# {
|
| 185 |
+
# "name": "Subcategory",
|
| 186 |
+
# "type": "regular",
|
| 187 |
+
# "entities": ["Category", "Category"],
|
| 188 |
+
# "cardinalities": {
|
| 189 |
+
# "Category": "1",
|
| 190 |
+
# "Category": "N"
|
| 191 |
+
# }
|
| 192 |
+
# }
|
| 193 |
+
# ]
|
| 194 |
+
# }
|
| 195 |
+
|
| 196 |
+
# Returns:
|
| 197 |
+
# str: The filepath to the generated PNG image file.
|
| 198 |
+
# """
|
| 199 |
+
# try:
|
| 200 |
+
# if not json_input.strip():
|
| 201 |
+
# return "Error: Empty input"
|
| 202 |
+
|
| 203 |
+
# data = json.loads(json_input)
|
| 204 |
+
|
| 205 |
+
# if 'entities' not in data:
|
| 206 |
+
# raise ValueError("Missing required field: entities")
|
| 207 |
+
|
| 208 |
+
# dot = graphviz.Graph(comment='ER Diagram', engine='dot')
|
| 209 |
+
# dot.attr(rankdir='TB', bgcolor='white', pad='0.5')
|
| 210 |
+
# dot.attr('node', fontname='Arial', fontsize='10', color='black')
|
| 211 |
+
# dot.attr('edge', fontname='Arial', fontsize='9', color='black')
|
| 212 |
+
|
| 213 |
+
# entities = data.get('entities', [])
|
| 214 |
+
# relationships = data.get('relationships', [])
|
| 215 |
+
|
| 216 |
+
# for entity in entities:
|
| 217 |
+
# entity_name = entity.get('name')
|
| 218 |
+
# entity_type = entity.get('type', 'strong')
|
| 219 |
+
# attributes = entity.get('attributes', [])
|
| 220 |
+
|
| 221 |
+
# if not entity_name:
|
| 222 |
+
# continue
|
| 223 |
+
|
| 224 |
+
# label_parts = [entity_name]
|
| 225 |
+
|
| 226 |
+
# if attributes:
|
| 227 |
+
# attr_lines = []
|
| 228 |
+
# for attr in attributes:
|
| 229 |
+
# attr_name = attr.get('name', '')
|
| 230 |
+
# attr_type = attr.get('type', 'regular')
|
| 231 |
+
# is_multivalued = attr.get('multivalued', False)
|
| 232 |
+
# is_derived = attr.get('derived', False)
|
| 233 |
+
# is_composite = attr.get('composite', False)
|
| 234 |
+
|
| 235 |
+
# if attr_type == 'primary_key':
|
| 236 |
+
# if is_multivalued:
|
| 237 |
+
# attr_display = f"PK: {{ {attr_name} }}"
|
| 238 |
+
# else:
|
| 239 |
+
# attr_display = f"PK: {attr_name}"
|
| 240 |
+
# elif attr_type == 'foreign_key':
|
| 241 |
+
# attr_display = f"FK: {attr_name}"
|
| 242 |
+
# else:
|
| 243 |
+
# attr_display = attr_name
|
| 244 |
+
# if is_derived:
|
| 245 |
+
# attr_display = f"/ {attr_display} /"
|
| 246 |
+
# if is_multivalued:
|
| 247 |
+
# attr_display = f"{{ {attr_display} }}"
|
| 248 |
+
# if is_composite:
|
| 249 |
+
# attr_display = f"( {attr_display} )"
|
| 250 |
+
|
| 251 |
+
# attr_lines.append(attr_display)
|
| 252 |
+
|
| 253 |
+
# if attr_lines:
|
| 254 |
+
# label_parts.extend(attr_lines)
|
| 255 |
+
|
| 256 |
+
# label = "\\n".join(label_parts)
|
| 257 |
+
|
| 258 |
+
# if entity_type == 'weak':
|
| 259 |
+
# style = 'filled'
|
| 260 |
+
# fillcolor = '#e8e8e8'
|
| 261 |
+
# penwidth = '2'
|
| 262 |
+
# else:
|
| 263 |
+
# style = 'filled'
|
| 264 |
+
# fillcolor = '#d0d0d0'
|
| 265 |
+
# penwidth = '1'
|
| 266 |
+
|
| 267 |
+
# dot.node(entity_name, label, shape='box', style=style, fillcolor=fillcolor, penwidth=penwidth)
|
| 268 |
+
|
| 269 |
+
# for relationship in relationships:
|
| 270 |
+
# rel_name = relationship.get('name')
|
| 271 |
+
# rel_type = relationship.get('type', 'regular')
|
| 272 |
+
# entities_involved = relationship.get('entities', [])
|
| 273 |
+
# cardinalities = relationship.get('cardinalities', {})
|
| 274 |
+
|
| 275 |
+
# if not rel_name or len(entities_involved) < 2:
|
| 276 |
+
# continue
|
| 277 |
+
|
| 278 |
+
# if rel_type == 'identifying':
|
| 279 |
+
# style = 'filled'
|
| 280 |
+
# fillcolor = '#c0c0c0'
|
| 281 |
+
# penwidth = '2'
|
| 282 |
+
# else:
|
| 283 |
+
# style = 'filled'
|
| 284 |
+
# fillcolor = '#c0c0c0'
|
| 285 |
+
# penwidth = '1'
|
| 286 |
+
|
| 287 |
+
# dot.node(rel_name, rel_name, shape='diamond', style=style, fillcolor=fillcolor, penwidth=penwidth)
|
| 288 |
+
|
| 289 |
+
# for entity in entities_involved:
|
| 290 |
+
# cardinality = cardinalities.get(entity, '1')
|
| 291 |
+
# dot.edge(entity, rel_name, label=cardinality)
|
| 292 |
+
|
| 293 |
+
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 294 |
+
# dot.render(tmp.name, format=output_format, cleanup=True)
|
| 295 |
+
# return f"{tmp.name}.{output_format}"
|
| 296 |
+
|
| 297 |
+
# except json.JSONDecodeError:
|
| 298 |
+
# return "Error: Invalid JSON format"
|
| 299 |
+
# except Exception as e:
|
| 300 |
+
# return f"Error: {str(e)}"
|
| 301 |
+
|
| 302 |
import graphviz
|
| 303 |
import json
|
| 304 |
from tempfile import NamedTemporaryFile
|
|
|
|
| 316 |
{
|
| 317 |
"entities": [
|
| 318 |
{
|
| 319 |
+
"name": "EMPLEADO",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
"type": "strong",
|
| 321 |
"attributes": [
|
| 322 |
+
{"name": "RUT", "type": "primary_key"},
|
| 323 |
+
{"name": "Nombre", "type": "regular"},
|
| 324 |
+
{"name": "Apellido1", "type": "regular"},
|
| 325 |
+
{"name": "Apellido2", "type": "regular"},
|
| 326 |
+
{"name": "Dirección", "type": "regular"},
|
| 327 |
+
{"name": "Sexo", "type": "regular"},
|
| 328 |
+
{"name": "Salario", "type": "regular"},
|
| 329 |
+
{"name": "FechaN", "type": "regular"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
]
|
| 331 |
},
|
| 332 |
{
|
| 333 |
+
"name": "DEPARTAMENTO",
|
| 334 |
"type": "strong",
|
| 335 |
"attributes": [
|
| 336 |
+
{"name": "Número", "type": "primary_key"},
|
| 337 |
+
{"name": "Nombre", "type": "regular"},
|
| 338 |
+
{"name": "Localizaciones", "type": "multivalued"}
|
|
|
|
|
|
|
|
|
|
| 339 |
]
|
| 340 |
},
|
| 341 |
{
|
| 342 |
+
"name": "PROYECTO",
|
| 343 |
"type": "strong",
|
| 344 |
"attributes": [
|
| 345 |
+
{"name": "Número", "type": "primary_key"},
|
| 346 |
+
{"name": "Nombre", "type": "regular"},
|
| 347 |
+
{"name": "Localización", "type": "regular"}
|
|
|
|
|
|
|
|
|
|
| 348 |
]
|
| 349 |
},
|
| 350 |
{
|
| 351 |
+
"name": "DEPENDIENTE",
|
| 352 |
"type": "weak",
|
| 353 |
"attributes": [
|
| 354 |
+
{"name": "Nombre", "type": "partial_key"},
|
| 355 |
+
{"name": "Sexo", "type": "regular"},
|
| 356 |
+
{"name": "FechaN", "type": "regular"},
|
| 357 |
+
{"name": "Relación", "type": "regular"}
|
|
|
|
| 358 |
]
|
| 359 |
}
|
| 360 |
],
|
| 361 |
"relationships": [
|
| 362 |
{
|
| 363 |
+
"name": "TRABAJA_PARA",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
"type": "regular",
|
| 365 |
+
"entities": ["EMPLEADO", "DEPARTAMENTO"],
|
| 366 |
"cardinalities": {
|
| 367 |
+
"EMPLEADO": "N",
|
| 368 |
+
"DEPARTAMENTO": "1"
|
| 369 |
}
|
| 370 |
},
|
| 371 |
{
|
| 372 |
+
"name": "ADMINISTRA",
|
| 373 |
"type": "regular",
|
| 374 |
+
"entities": ["EMPLEADO", "DEPARTAMENTO"],
|
| 375 |
"cardinalities": {
|
| 376 |
+
"EMPLEADO": "1",
|
| 377 |
+
"DEPARTAMENTO": "1"
|
| 378 |
+
},
|
| 379 |
+
"attributes": [
|
| 380 |
+
{"name": "FechaInicio", "type": "regular"}
|
| 381 |
+
]
|
| 382 |
},
|
| 383 |
{
|
| 384 |
+
"name": "CONTROLA",
|
| 385 |
"type": "regular",
|
| 386 |
+
"entities": ["DEPARTAMENTO", "PROYECTO"],
|
| 387 |
"cardinalities": {
|
| 388 |
+
"DEPARTAMENTO": "1",
|
| 389 |
+
"PROYECTO": "N"
|
| 390 |
}
|
| 391 |
},
|
| 392 |
{
|
| 393 |
+
"name": "SUPERVISIÓN",
|
| 394 |
"type": "regular",
|
| 395 |
+
"entities": ["EMPLEADO", "EMPLEADO"],
|
| 396 |
"cardinalities": {
|
| 397 |
+
"EMPLEADO": "1",
|
| 398 |
+
"EMPLEADO": "N"
|
| 399 |
+
},
|
| 400 |
+
"roles": {
|
| 401 |
+
"EMPLEADO": ["supervisor", "supervisado"]
|
| 402 |
}
|
| 403 |
},
|
| 404 |
{
|
| 405 |
+
"name": "TRABAJA_PARA_PROYECTO",
|
| 406 |
+
"type": "ternary",
|
| 407 |
+
"entities": ["EMPLEADO", "PROYECTO"],
|
| 408 |
"cardinalities": {
|
| 409 |
+
"EMPLEADO": "M",
|
| 410 |
+
"PROYECTO": "N"
|
| 411 |
+
},
|
| 412 |
+
"attributes": [
|
| 413 |
+
{"name": "Horas", "type": "regular"}
|
| 414 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
},
|
| 416 |
{
|
| 417 |
+
"name": "DEPENDIENTES",
|
| 418 |
+
"type": "identifying",
|
| 419 |
+
"entities": ["EMPLEADO", "DEPENDIENTE"],
|
| 420 |
"cardinalities": {
|
| 421 |
+
"EMPLEADO": "1",
|
| 422 |
+
"DEPENDIENTE": "N"
|
| 423 |
}
|
| 424 |
}
|
| 425 |
]
|
|
|
|
| 438 |
raise ValueError("Missing required field: entities")
|
| 439 |
|
| 440 |
dot = graphviz.Graph(comment='ER Diagram', engine='dot')
|
| 441 |
+
dot.attr(rankdir='TB', bgcolor='white', pad='0.5', nodesep='1.0', ranksep='1.5')
|
| 442 |
dot.attr('node', fontname='Arial', fontsize='10', color='black')
|
| 443 |
dot.attr('edge', fontname='Arial', fontsize='9', color='black')
|
| 444 |
|
| 445 |
entities = data.get('entities', [])
|
| 446 |
relationships = data.get('relationships', [])
|
| 447 |
|
| 448 |
+
# Crear entidades
|
| 449 |
for entity in entities:
|
| 450 |
entity_name = entity.get('name')
|
| 451 |
entity_type = entity.get('type', 'strong')
|
|
|
|
| 452 |
|
| 453 |
if not entity_name:
|
| 454 |
continue
|
| 455 |
|
| 456 |
+
# Entidades fuertes: rectángulo simple
|
| 457 |
+
# Entidades débiles: rectángulo doble (simulado con penwidth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
if entity_type == 'weak':
|
| 459 |
+
dot.node(entity_name, entity_name, shape='box', style='filled',
|
| 460 |
+
fillcolor='white', color='black', penwidth='3')
|
|
|
|
| 461 |
else:
|
| 462 |
+
dot.node(entity_name, entity_name, shape='box', style='filled',
|
| 463 |
+
fillcolor='white', color='black', penwidth='1')
|
| 464 |
+
|
| 465 |
+
# Crear atributos para cada entidad
|
| 466 |
+
for entity in entities:
|
| 467 |
+
entity_name = entity.get('name')
|
| 468 |
+
attributes = entity.get('attributes', [])
|
| 469 |
|
| 470 |
+
for attr in attributes:
|
| 471 |
+
attr_name = attr.get('name', '')
|
| 472 |
+
attr_type = attr.get('type', 'regular')
|
| 473 |
+
|
| 474 |
+
attr_id = f"{entity_name}_{attr_name}"
|
| 475 |
+
|
| 476 |
+
if attr_type == 'primary_key':
|
| 477 |
+
# Atributo clave: elipse con texto subrayado
|
| 478 |
+
dot.node(attr_id, f"<U>{attr_name}</U>", shape='ellipse',
|
| 479 |
+
style='filled', fillcolor='white', color='black')
|
| 480 |
+
elif attr_type == 'partial_key':
|
| 481 |
+
# Clave parcial: elipse con línea punteada
|
| 482 |
+
dot.node(attr_id, f"<U>{attr_name}</U>", shape='ellipse',
|
| 483 |
+
style='filled,dashed', fillcolor='white', color='black')
|
| 484 |
+
elif attr_type == 'multivalued':
|
| 485 |
+
# Atributo multivaluado: doble elipse (simulado con penwidth)
|
| 486 |
+
dot.node(attr_id, attr_name, shape='ellipse',
|
| 487 |
+
style='filled', fillcolor='white', color='black', penwidth='3')
|
| 488 |
+
elif attr_type == 'derived':
|
| 489 |
+
# Atributo derivado: elipse con línea punteada
|
| 490 |
+
dot.node(attr_id, attr_name, shape='ellipse',
|
| 491 |
+
style='filled,dashed', fillcolor='white', color='black')
|
| 492 |
+
elif attr_type == 'composite':
|
| 493 |
+
# Atributo compuesto: elipse normal (los componentes se conectarían por separado)
|
| 494 |
+
dot.node(attr_id, attr_name, shape='ellipse',
|
| 495 |
+
style='filled', fillcolor='white', color='black')
|
| 496 |
+
else:
|
| 497 |
+
# Atributo regular: elipse simple
|
| 498 |
+
dot.node(attr_id, attr_name, shape='ellipse',
|
| 499 |
+
style='filled', fillcolor='white', color='black')
|
| 500 |
+
|
| 501 |
+
# Conectar atributo con entidad
|
| 502 |
+
dot.edge(entity_name, attr_id, color='black')
|
| 503 |
|
| 504 |
+
# Crear relaciones
|
| 505 |
for relationship in relationships:
|
| 506 |
rel_name = relationship.get('name')
|
| 507 |
rel_type = relationship.get('type', 'regular')
|
| 508 |
entities_involved = relationship.get('entities', [])
|
| 509 |
cardinalities = relationship.get('cardinalities', {})
|
| 510 |
+
rel_attributes = relationship.get('attributes', [])
|
| 511 |
+
roles = relationship.get('roles', {})
|
| 512 |
|
| 513 |
if not rel_name or len(entities_involved) < 2:
|
| 514 |
continue
|
| 515 |
|
| 516 |
+
# Relaciones identificadoras: rombo doble
|
| 517 |
+
# Relaciones regulares: rombo simple
|
| 518 |
if rel_type == 'identifying':
|
| 519 |
+
dot.node(rel_name, rel_name, shape='diamond', style='filled',
|
| 520 |
+
fillcolor='white', color='black', penwidth='3')
|
|
|
|
| 521 |
else:
|
| 522 |
+
dot.node(rel_name, rel_name, shape='diamond', style='filled',
|
| 523 |
+
fillcolor='white', color='black', penwidth='1')
|
|
|
|
| 524 |
|
| 525 |
+
# Crear atributos de la relación
|
| 526 |
+
for attr in rel_attributes:
|
| 527 |
+
attr_name = attr.get('name', '')
|
| 528 |
+
attr_type = attr.get('type', 'regular')
|
| 529 |
+
attr_id = f"{rel_name}_{attr_name}"
|
| 530 |
+
|
| 531 |
+
dot.node(attr_id, attr_name, shape='ellipse',
|
| 532 |
+
style='filled', fillcolor='white', color='black')
|
| 533 |
+
dot.edge(rel_name, attr_id, color='black')
|
| 534 |
|
| 535 |
+
# Conectar entidades con la relación
|
| 536 |
+
if len(entities_involved) == 2 and entities_involved[0] == entities_involved[1]:
|
| 537 |
+
# Relación reflexiva (como supervisión)
|
| 538 |
+
entity = entities_involved[0]
|
| 539 |
+
cardinality1 = cardinalities.get(entity, '1')
|
| 540 |
+
|
| 541 |
+
# Para relaciones reflexivas, necesitamos crear dos conexiones
|
| 542 |
+
dot.edge(entity, rel_name, label=cardinality1, color='black')
|
| 543 |
+
dot.edge(entity, rel_name, label='N', color='black')
|
| 544 |
+
|
| 545 |
+
# Agregar roles si están definidos
|
| 546 |
+
if roles and entity in roles:
|
| 547 |
+
role_list = roles[entity]
|
| 548 |
+
if len(role_list) >= 2:
|
| 549 |
+
# Crear nodos de texto para los roles
|
| 550 |
+
role1_id = f"{rel_name}_role1"
|
| 551 |
+
role2_id = f"{rel_name}_role2"
|
| 552 |
+
dot.node(role1_id, role_list[0], shape='plaintext', fontsize='8')
|
| 553 |
+
dot.node(role2_id, role_list[1], shape='plaintext', fontsize='8')
|
| 554 |
+
else:
|
| 555 |
+
# Relación normal entre diferentes entidades
|
| 556 |
+
for entity in entities_involved:
|
| 557 |
+
cardinality = cardinalities.get(entity, '1')
|
| 558 |
+
dot.edge(entity, rel_name, label=cardinality, color='black')
|
| 559 |
|
| 560 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 561 |
dot.render(tmp.name, format=output_format, cleanup=True)
|