Skip to content

Metadata

Bases: ServiceClient

Source code in blue/metadata.py
  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
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
class MetaData(ServiceClient):

    SEMANTIC_ROLES = [
        "IDENTIFIER",            # IDs, UUIDs
        "EVENT_TIME",             # e.g. transaction_time, crash_time
        "EVIDENCE",               # e.g. skill_months
        "SEGMENTATION_DRIVER",    # e.g. country of high-value customers
        "DESCRIPTIVE"             # weak explanatory power
    ]

    # ------------------------------------------------------------------
    # Value Semantics type taxonomy (domain-agnostic, value-centric)
    # ------------------------------------------------------------------
    VALUE_SEMANTIC_TYPES = [
        "UNKNOWN",

        # structural
        "BOOLEAN",
        "INTEGER",
        "FLOAT",
        "NUMERIC_GENERAL",
        "STRING",
        "FREE_TEXT",

        # temporal
        "DATE",
        "DATETIME",
        "TIME",
        "YEAR",
        "MONTH",
        "DAY_OF_WEEK",
        "DURATION",

        # location / codes
        "US_STATE_CODE",
        "US_STATE_NAME",
        "COUNTRY_CODE_ISO2",
        "COUNTRY_CODE_ISO3",
        "CITY_NAME",
        "POSTAL_CODE",
        "ADDRESS_FREEFORM",

        # financial / numeric semantics
        "CURRENCY_AMOUNT",
        "PERCENTAGE",
        "RATIO",

        # identity / ids
        "ID_NUMERIC",
        "ID_STRING",
        "UUID",
        "HASH",

        # contact / web
        "EMAIL",
        "PHONE_NUMBER",
        "URL",

        # categories & labels
        "ENUM_CATEGORY",
        "TEXT_CATEGORY",
        "TAG",
        "LABEL",

        # human / skills-ish / terms
        "PERSON_NAME",
        "ORG_NAME",
        "JOB_TITLE",
        "SKILL_TERM",
        "CONTROLLED_TERM",

        # demographic-ish but still value-level
        "AGE",
        "AGE_GROUP",
        "DEMOGRAPHIC_TERM",
    ]

    def __init__(self, properties=None):
        self.name = "metadata"
        super().__init__(self.name, properties=properties)
        self._init_metadata_properties()

    ###### initialization
    def _init_metadata_properties(self):

        self.properties['openai.api'] = 'ChatCompletion'
        self.properties['openai.model'] = "gpt-4o"
        self.properties['input_json'] = "[{\"role\": \"user\"}]"
        self.properties['input_context'] = "$[0]"
        self.properties['input_context_field'] = "content"
        self.properties['input_field'] = "messages"
        self.properties['input_template'] = "${input}"
        self.properties['output_path'] = '$.choices[0].message.content'
        self.properties['openai.stream'] = False
        self.properties['openai.max_tokens'] = 300

        # prefix for service specific properties
        self.properties['service_prefix'] = 'openai'
        # service_url, set as default
        self.properties["service_url"] = PROPERTIES["services.openai.service_url"]

        # transformations
        self.properties['output_transformations'] = [{"transformation": "replace", "from": "```", "to": ""}, {"transformation": "replace", "from": "json", "to": ""}]
        self.properties['output_strip'] = True

        self.properties['enable_entity_description_generation'] = True
        self.properties['enable_attribute_description_generation'] = True

        # Description aggregation from children
        self.properties['aggregation_prompt'] = AGGREGATION_PROMPT
        self.properties['enable_database_description_generation'] = True
        self.properties['enable_collection_description_generation'] = True

        self.properties['enable_value_semantics_inference'] = True
        self.properties['enable_semantic_discovery_inference'] = True

        self.properties['enable_domain_concept_mapping'] = True

        self.properties['enable_value_axis_inference'] = True
        self.properties['enable_semantic_links_inference'] = True
        self.properties['enable_interpretive_semantics'] = True

        self.properties["concept_taxonomy_path"] = "/blue_data/config/concept_taxonomy.json"
        self.properties["concept_taxonomy"] = self._load_concept_taxonomy()


    def mean_safe(self, vals):
        return sum(vals) / len(vals) if vals else None

    def safe_pearson(self, x, y):
        if len(x) < 5 or len(y) < 5:
            return 0.0
        mx, my = sum(x)/len(x), sum(y)/len(y)
        num = sum((a-mx)*(b-my) for a,b in zip(x,y))
        denx = math.sqrt(sum((a-mx)**2 for a in x))
        deny = math.sqrt(sum((b-my)**2 for b in y))
        if denx == 0 or deny == 0:
            return 0.0
        return num / (denx * deny)


    def get_numeric_samples(self, attr):
        stats = attr.get("properties", {}).get("stats", {})
        samples = stats.get("sample_values", [])
        nums = []
        for v in samples:
            try:
                nums.append(float(v))
            except Exception:
                pass
        return nums

    def has_row_cooccurrence(self, a, b, row_samples, min_rows=5):
        """
        Check if two attributes co-occur in at least min_rows sample rows.
        """

        a_name = a["name"]
        b_name = b["name"]

        count = 0
        for row in row_samples:
            if row.get(a_name) is not None and row.get(b_name) is not None:
                count += 1
                if count >= min_rows:
                    return True
        return False

    def functional_dependency_signal(self, a, b):
        """
        Detect potential functional / monotonic dependency between two attributes.
        """
        a_vals = self.get_numeric_samples(a)
        b_vals = self.get_numeric_samples(b)

        if len(a_vals) < 5 or len(b_vals) < 5:
            return False

        corr = abs(self.safe_pearson(a_vals, b_vals))
        return corr > 0.7

    def temporal_alignment_signal(self, a, b):
        """
        Detect weak temporal alignment without semantic commitment.
        """
        a_type = a.get("properties", {}).get("value_semantics", {}).get("semantic_type")
        b_type = b.get("properties", {}).get("value_semantics", {}).get("semantic_type")

        temporal_types = {"DATE", "DATETIME", "TIME", "DURATION"}
        return a_type in temporal_types and b_type not in temporal_types


    def _load_concept_taxonomy(self):
        """
        Load domain concept taxonomy from the shared /blue_data/config folder.
        """
        taxonomy_path = self.properties.get(
            "concept_taxonomy_path",
            "/blue_data/config/concept_taxonomy.json"
        )

        if not os.path.exists(taxonomy_path):
            logging.warning(f"[MetaData] Domain taxonomy not found at: {taxonomy_path}")
            return []

        try:
            with open(taxonomy_path, "r") as f:
                data = json.load(f)
                return data.get("concepts", [])
        except Exception as e:
            logging.error(f"[MetaData] Failed loading taxonomy: {e}")
            return []


    def build_entity_description_prompt(self, entity_obj, attributes):
        """
        Build a prompt for generating an entity description using an LLM.

        Constructs a structured text prompt containing entity metadata and attribute
        information, suitable for guiding an LLM to produce a JSON-formatted
        description of the entity and its attributes.

        Parameters:
            entity_obj (dict): A dictionary representing the entity, from
                the data registry.
            attributes (list[dict]): A list of attribute definitions.

        Returns:
            str: A formatted multi-line string prompt, instructing the LLM to produce
            a JSON object with:
                - "table_description": Human-readable description of the entity.
                - "attributes": Mapping of attribute names to their descriptions.

        """

        # Extract basic info
        name = entity_obj.get("name", "Unknown")
        scope = entity_obj.get("scope", "Unknown")
        etype = entity_obj.get("type", "Unknown")

        attr_lines = []

        for attr in attributes:
            attr_properties = attr.get("properties", {})
            attr_properties_info = attr_properties.get("info", {})
            attr_type = attr_properties_info.get("type", "unknown")

            attr_name = attr.get("name")
            attr_stats = attr_properties.get("stats", {})

            sample_values = attr_stats.get("sample_values", [])

            attr_lines.append(f"- {attr_name} ({attr_type}), samples: {', '.join(map(str, sample_values[:3]))}")

        # Build the final prompt
        prompt = f"""
        You are given a database entity definition with its attributes and metadata.
        Your task is to generate a structured JSON output with:
        1. A concise human-readable description of what this table/entity represents.
        2. Concise descriptions of each attribute.

        Entity Name: {name}
        Scope: {scope}
        Type: {etype}

        Attributes:
        {chr(10).join(attr_lines)}

        Output JSON format (do not include extra commentary, only valid JSON):

        {{
        "table_description": "string",
        "attributes": {{
            "attr_name": "description of attribute",
            ...
        }}
        }}
        """
        return prompt

    def enrich_entity(self, entity, attributes):
        """
        Generate an enriched description for an entity using its attributes.

        Builds a prompt from the entity and its attributes, then calls the
        external LLM API to produce the enriched description.

        Parameters:
            entity (dict): The entity metadata to enrich.
            attributes (dict): Attribute data associated with the entity.

        Returns:
            str: Enriched description text generated by the API.
        """
        entity_prompt = self.build_entity_description_prompt(entity, attributes)
        return self.execute_api_call(entity_prompt, properties=self.properties, additional_data={})

    def collect_source_metadata(self, data_registry, source, recursive=False, rebuild=False):
        """
        Collect and optionally recursively enrich metadata for a data source.

        If recursive is True, iterates through all databases under the source
        and collects/enriches their metadata.

        Parameters:
            data_registry (DataRegistry): Registry instance for metadata access/storage.
            source (str): Identifier of the data source.
            recursive (bool, optional): Whether to include child databases. Defaults to False.
            rebuild (bool, optional): Whether to regenerate existing descriptions. Defaults to False.

        Returns:
            None
        """
        if recursive:
            databases = data_registry.get_source_databases(source)
            for database in databases:
                self.collect_source_database_metadata(data_registry, source, database, recursive=recursive, rebuild=rebuild)
        return

    def collect_source_database_metadata(self, data_registry, source, database, recursive=False, rebuild=False):
        """
        Collect and enrich metadata for a database within a data source.

        This method checks whether the database already has a description. If not,
        it uses available metadata and collection descriptions to generate an
        enriched description (via `enrich_database_description`) and stores it
        back into the data registry. Optionally, it can also recurse into
        collections to collect their metadata.

        Parameters:
            data_registry (DataRegistry): The registry object that manages sources,
                databases, collections, and metadata.
            source (str): Identifier for the data source.
            database (str): Name of the database to collect metadata for.
            recursive (bool, optional): If True, also collect metadata for all
                collections within the database. Defaults to False.
            rebuild (bool, optional): If True, forces metadata to be rebuilt or
                refreshed even if it already exists. Defaults to False.

        Returns:
            None

        """
        collections = data_registry.get_source_database_collections(source, database)
        collection_descriptions = {}

        if recursive:
            for collection in collections:
                collection_name = collection.get("name")
                self.collect_source_database_collection_metadata(
                    data_registry,
                    source,
                    database,
                    collection_name,
                    recursive=True,
                    rebuild=rebuild
                )


        if self.properties.get('enable_database_description_generation', True):
            current_description = data_registry.get_source_database_description(source, database)
            if rebuild or not current_description or current_description.strip() == "":
                database_metadata = data_registry.get_source_database_property(source, database, "metadata")

                if not database_metadata:
                    database_metadata = {"name": database, "type": "database"}

                for collection in collections:
                    collection_name = collection.get("name")
                    collection_desc = collection.get("description")
                    collection_descriptions[collection_name] = collection_desc

                database_desc = self.enrich_database_description(database, collection_descriptions, database_metadata)

                data_registry.set_source_database_description(source, database, database_desc, rebuild=rebuild)

        return

    def collect_source_database_collection_metadata(self, data_registry, source, database, collection, recursive=False, rebuild=False):
        """
        Collect and enrich metadata for a specific collection and its entities within a database.

        For each entity in the collection, generates enriched table and attribute descriptions
        using the LLM-based enrichment process, and stores them in the data registry if missing.
        Optionally, also generates a collection-level description.

        Parameters:
            data_registry (DataRegistry): Registry instance for accessing and storing metadata.
            source (str): Identifier of the data source.
            database (str): Name of the database containing the collection.
            collection (str): Name of the collection to process.
            recursive (bool, optional): Whether to process nested collections or entities. Defaults to False.
            rebuild (bool, optional): Whether to regenerate existing descriptions. Defaults to False.

        Returns:
            None
        """

        entities = data_registry.get_source_database_collection_entities(source, database, collection)

        entity_descriptions = {}
        for entity in entities:
            entity_name = entity.get("name")

            attributes = data_registry.get_source_database_collection_entity_attributes(source, database, collection, entity_name)

            entity_stats = entity.get("properties", {}).get("stats", {})
            row_samples = entity_stats.get("row_samples", [])

            self.current_entity_row_samples = row_samples

            entity_attribute_description = self.enrich_entity(entity, attributes)

            try:
                parsed = json_utils.safe_json_parse(entity_attribute_description)
                if not parsed:
                    logging.warning(f"Entity {entity} returned invalid or empty JSON.")
                    continue
            except json.JSONDecodeError:
                logging.warning("LLM did not return valid JSON. Skipping entity enrichment.")
                parsed = {}

            table_desc = parsed.get("table_description", "")
            attribute_descs = parsed.get("attributes", {})
            entity_descriptions[entity_name] = table_desc

            if self.properties.get('enable_entity_description_generation', True):
                current_description = data_registry.get_source_database_collection_entity_description(source, database, collection, entity_name)

                if not current_description or current_description.strip() == "":
                    data_registry.set_source_database_collection_entity_description(source, database, collection, entity_name, table_desc, rebuild=rebuild)

            if self.properties.get('enable_attribute_description_generation', True):
                for attr, desc in attribute_descs.items():
                    current_description = data_registry.get_source_database_collection_entity_attribute_description(source, database, collection, entity_name, attr)
                    if not current_description or current_description.strip() == "":
                        data_registry.set_source_database_collection_entity_attribute_description(source, database, collection, entity_name, attr, desc, rebuild=rebuild)

            self.current_entity_attributes = attributes

            if self.properties.get("enable_value_semantics_inference", True):
                for attr_obj in attributes:
                    attr_name = attr_obj.get("name")

                    # Skip attributes without stats or samples
                    attr_stats = attr_obj.get("properties", {}).get("stats", {})
                    if not attr_stats:
                        continue

                    # Check existing semantics
                    existing = data_registry.get_source_database_collection_entity_attribute_property(
                        source, database, collection, entity_name, attr_name, "value_semantics"
                    )

                    # Infer only if missing or rebuild=True
                    if existing and not rebuild:
                        continue

                    inferred = self.infer_attribute_vsi(entity_name, attr_obj)

                    if inferred:
                        data_registry.set_source_database_collection_entity_attribute_property(
                            source,
                            database,
                            collection,
                            entity_name,
                            attr_name,
                            "value_semantics",
                            inferred,
                            rebuild=rebuild
                        )

                    sdi = self.infer_attribute_sdi(entity_name, attr_obj)
                    if sdi:
                        data_registry.set_source_database_collection_entity_attribute_property(
                            source, database, collection, entity_name, attr_name,
                            "semantic_discovery", sdi, rebuild=rebuild
                        )


                ### refresh attributes after value semantics inference
                attributes = data_registry.get_source_database_collection_entity_attributes(
                    source, database, collection, entity_name
                )

            if self.properties.get("enable_domain_concept_mapping", True):
                for attr_obj in attributes:
                    attr_name = attr_obj.get("name")

                    # Skip attributes without value semantics
                    attr_properties = attr_obj.get("properties", {})
                    if "value_semantics" not in attr_properties:
                        continue

                    existing = data_registry.get_source_database_collection_entity_attribute_property(
                        source, database, collection, entity_name, attr_name, "domain_concept"
                    )

                    if existing and not rebuild:
                        continue

                    inferred = self.infer_domain_concept(entity_name, attr_obj)
                    if inferred:
                        data_registry.set_source_database_collection_entity_attribute_property(
                            source,
                            database,
                            collection,
                            entity_name,
                            attr_name,
                            "domain_concept",
                            inferred,
                            rebuild=rebuild
                        )

            if self.properties.get("enable_interpretive_semantics", True):
                for attr in attributes:
                    ivs = self.infer_interpretive_semantics(entity_name, attr)
                    if ivs:
                        data_registry.set_source_database_collection_entity_attribute_property(
                            source,
                            database,
                            collection,
                            entity_name,
                            attr["name"],
                            "interpretive_semantics",
                            ivs,
                            rebuild=rebuild
                        )

                        logging.info(
                            f"[IVS] Inferred interpretive semantics for "
                            f"{entity_name}.{attr['name']}: {ivs}"
                        )

            # Refresh attributes after IVS
            attributes = data_registry.get_source_database_collection_entity_attributes(
                source, database, collection, entity_name
            )

            if self.properties.get("enable_value_axis_inference", True):
                axes = self.infer_value_axis(
                    attributes,
                    self.current_entity_row_samples
                )

                for attr_name, axis in axes.items():
                    data_registry.set_source_database_collection_entity_attribute_property(
                        source, database, collection, entity_name,
                        attr_name, "value_axis", axis, rebuild=rebuild
                    )

            if self.properties.get("enable_semantic_links_inference", True):
                # -------------------------------------------------
                # SEMANTIC LINK DISCOVERY (STRUCTURAL + SOFT LLM)
                # -------------------------------------------------
                semantic_links = self.infer_semantic_links(attributes)

                row_links = self.infer_row_based_links(
                    attributes,
                    self.current_entity_row_samples
                )

                # TAG row-grounded links
                for l in row_links:
                    l["row_support"] = True

                semantic_links = semantic_links + row_links

                validated_links = []

                for link in semantic_links:
                    try:
                        src = next(a for a in attributes if a["name"] == link["source"])
                        tgt = next(a for a in attributes if a["name"] == link["target"])
                    except StopIteration:
                        continue

                    result = self.validate_semantic_link(src, tgt, link["signal"])

                    relationship = result.get("relationship") if result else None

                    signal_type = (
                        link["signal"].get("type")
                        if isinstance(link["signal"], dict)
                        else link["signal"]
                    )

                    if relationship == "UNRELATED" and signal_type in {
                        "grouped_distribution",
                        "functional_dependency",
                        "temporal_alignment",
                    }:
                        relationship = {
                            "grouped_distribution": "SEGMENTS",
                            "functional_dependency": "DERIVES",
                            "temporal_alignment": "SUPPORTS",
                        }[signal_type]


                    # -------------------------------------------------
                    # FINALIZE LINK
                    # -------------------------------------------------
                    if relationship:
                        if link.get("row_support"):
                            # Row-grounded links dominate LLM disagreement
                            confidence = max(
                                0.7,
                                result.get("confidence", 0.7) if result else 0.7
                            )
                        else:
                            confidence = (
                                result.get("confidence", 0.55)
                                if result and relationship == result.get("relationship")
                                else 0.55
                            )

                        validated_links.append({
                            "source": link["source"],
                            "target": link["target"],
                            "relationship": relationship,
                            "confidence": confidence,
                            "rationale": (
                                result.get("rationale")
                                if result else "Structure-backed semantic signal"
                            ),
                        })

                # Persist links
                if validated_links:
                    data_registry.set_source_database_collection_entity_property(
                        source,
                        database,
                        collection,
                        entity_name,
                        "semantic_links",
                        validated_links,
                        rebuild=rebuild
                    )

                # -------------------------------------------------
                # CONDITIONAL VALUE DISTRIBUTIONS 
                # -------------------------------------------------
                # Only infer conditional distributions for validated SEGMENTS links
                if validated_links and self.current_entity_row_samples:

                    for link in validated_links:
                        if link.get("relationship") != "SEGMENTS":
                            continue

                        try:
                            group_attr = next(
                                a for a in attributes if a["name"] == link["source"]
                            )
                            value_attr = next(
                                a for a in attributes if a["name"] == link["target"]
                            )

                        except StopIteration:
                            continue

                        group_role = (
                                group_attr
                                .get("properties", {})
                                .get("semantic_role", {})
                            )

                        if group_role.get("primary_role") != "SEGMENTATION_DRIVER":
                            continue


                        group_vsi = group_attr.get("properties", {}).get("value_semantics", {})
                        value_vsi = value_attr.get("properties", {}).get("value_semantics", {})

                        # Hard safety gate
                        if value_vsi.get("semantic_type") not in {
                            "CURRENCY_AMOUNT",
                            "INTEGER",
                            "FLOAT",
                            "NUMERIC_GENERAL",
                            "PERCENTAGE",
                            "RATIO",
                            "DURATION"
                        }:
                            continue

                        cds = self.infer_conditional_distributions(
                            group_attr,
                            value_attr,
                            self.current_entity_row_samples
                        )

                        if not cds:
                            continue

                        logging.info(
                            f"[MetaData] Inferred conditional distributions: "
                            f"{entity_name}.{value_attr['name']} | grouped by {group_attr['name']}"
                        )

                        existing = data_registry.get_source_database_collection_entity_attribute_property(
                            source, database, collection, entity_name,
                            value_attr["name"], "conditional_value_distribution"
                            ) or {}

                        existing[group_attr["name"]] = cds

                        data_registry.set_source_database_collection_entity_attribute_property(
                            source,
                            database,
                            collection,
                            entity_name,
                            value_attr["name"],
                            "conditional_value_distribution",
                            existing,
                            rebuild=rebuild
                        )


                # -------------------------------------------------
                # SEMANTIC ROLE INFERENCE (LINK-DRIVEN)
                # -------------------------------------------------
                for attr in attributes:
                    attr_name = attr["name"]

                    role = self.infer_semantic_role(
                        attr,
                        attributes,
                        semantic_links=(validated_links or semantic_links)
                    )

                    if role:
                        data_registry.set_source_database_collection_entity_attribute_property(
                            source, database, collection, entity_name,
                            attr_name, "semantic_role", role, rebuild=rebuild
                        )

        if self.properties.get('enable_collection_description_generation', True):
            current_description = data_registry.get_source_database_collection_description(source, database, collection)
            if not current_description or current_description.strip() == "":

                collection_metadata = data_registry.get_source_database_collection_property(source, database, collection, "metadata")

                if not collection_metadata:
                    collection_metadata = {"name": collection, "type": "collection"}

                collection_desc = self.enrich_collection_description(collection, entity_descriptions, collection_metadata)

                data_registry.set_source_database_collection_description(source, database, collection, collection_desc, rebuild=rebuild)

    ###### Aggregation
    def build_collection_description_prompt(self, collection_name, entity_descriptions, collection_metadata):
        """
        Build a prompt string for generating or enriching a collection description.

        Constructs a formatted text prompt using entity-level descriptions and
        metadata, suitable for passing to an LLM or enrichment API.

        Parameters:
            collection_name (str): The name of the collection.
            entity_descriptions (dict): Mapping of entity names to their descriptions.
            collection_metadata (dict or str): Additional metadata for the collection.

        Returns:
            str: A formatted prompt string for collection-level description enrichment.
        """
        child_descriptions = [f"{name}: {desc}" for name, desc in entity_descriptions.items() if desc]
        if not child_descriptions:
            child_descriptions = ["No entity descriptions available"]

        return self.properties['aggregation_prompt'].format(
            child_type='entity',
            parent_type='collection',
            child_descriptions='\n'.join(child_descriptions),
            parent_metadata=f"Collection name: {collection_name}\nMetadata: {collection_metadata}",
        )

    def build_database_description_prompt(self, database_name, collection_descriptions, database_metadata):
        """
        Build a prompt string for generating or enriching a database description.

        Constructs a formatted text prompt using collection-level descriptions and
        metadata, suitable for passing to an LLM or enrichment API.

        Parameters:
            database_name (str): The name of the database.
            collection_descriptions (dict): Mapping of collection names to their descriptions.
            database_metadata (dict or str): Additional metadata for the database.

        Returns:
            str: A formatted prompt string for database-level description enrichment.
        """

        child_descriptions = [f"{name}: {desc}" for name, desc in collection_descriptions.items() if desc]
        if not child_descriptions:
            child_descriptions = ["No collection descriptions available"]

        return self.properties['aggregation_prompt'].format(
            child_type='collection', parent_type='database', child_descriptions='\n'.join(child_descriptions), parent_metadata=f"Database name: {database_name}\nMetadata: {database_metadata}"
        )

    def enrich_collection_description(self, collection_name, entity_descriptions, collection_metadata):
        """
        Enrich a collection description using LLM.

        Builds a prompt from the provided entity descriptions and metadata, then
        executes an LLM call to generate or refine the collection-level description.

        Parameters:
            collection_name (str): The name of the collection.
            entity_descriptions (dict): Mapping of entity names to their descriptions.
            collection_metadata (dict or str): Additional metadata for the collection.

        Returns:
            Any: The enriched collection description, as returned by the LLM.
        """
        prompt = self.build_collection_description_prompt(collection_name, entity_descriptions, collection_metadata)
        return self.execute_api_call(prompt, properties=self.properties, additional_data={})

    def enrich_database_description(self, database_name, collection_descriptions, database_metadata):
        """
        Enrich a database description using LLM.

        Builds a prompt from the provided collection descriptions and metadata, then
        executes an API call to generate or refine the database-level description.

        Parameters:
            database_name (str): The name of the database.
            collection_descriptions (dict): Mapping of collection names to their descriptions.
            database_metadata (dict or str): Additional metadata for the database.

        Returns:
            Any: The enriched database description.
        """

        prompt = self.build_database_description_prompt(database_name, collection_descriptions, database_metadata)
        return self.execute_api_call(prompt, properties=self.properties, additional_data={})


    def build_attribute_context(self, attributes, target_attr_name):
        """
        Build lightweight cross-attribute context for VSI / SDI.

        Design principles:
        - Descriptive, not prescriptive (no hard semantic claims)
        - Distributional, not role-based
        - Column-attached, not joint inference (no relationships inferred)
        - Safe for first-pass VSI / SDI
        """

        # Context object passed downstream to VSI / SDI.
        # Each field captures a different *type* of weak signal.
        context = {
            "sibling_attributes": [],
            "numeric_distributions": {},
            "temporal_hints": [],
            "co_occurrence_hints": [],
            "relative_behavior": []   # distributional semantics
        }

        for attr in attributes:
            name = attr.get("name")
            if name == target_attr_name:
                continue

            props = attr.get("properties", {}) or {}
            stats = props.get("stats", {}) or {}
            sem = props.get("value_semantics", {}) or {}

            samples = stats.get("sample_values", [])[:5]

            # -------------------------------------------------
            # Sibling attribute names
            # -------------------------------------------------
            context["sibling_attributes"].append(name)

            # -------------------------------------------------
            # Robust numeric detection (NO VSI DEPENDENCY)
            # -------------------------------------------------
            min_val = stats.get("min")
            max_val = stats.get("max")

            is_numeric = (
                isinstance(min_val, (int, float))
                or isinstance(max_val, (int, float))
            )

            if is_numeric:
                context["numeric_distributions"][name] = {
                    "min": min_val,
                    "max": max_val,
                    "distinct": stats.get("distinct_count")
                }

                # -------------------------------------------------
                # Distributional role hints (NOT semantic roles)
                # Skip identifiers — they distort scale semantics
                # Provide very coarse magnitude hints to support
                # relative scale reasoning (e.g., thresholds, extremes).
                # -------------------------------------------------
                if not sem.get("is_identifier"):
                    magnitude = max_val
                    if isinstance(magnitude, (int, float)):
                        context["relative_behavior"].append({
                            "attribute": name,
                            "scale_hint": (
                                "small_range" if magnitude < 100
                                else "medium_range" if magnitude < 10000
                                else "large_range"
                            )
                        })

            # -------------------------------------------------
            # Temporal hints (weak, non-binding)
            # Record presence of time-like attributes nearby.
            # This does NOT imply that the target attribute
            # participates in temporal reasoning.
            # -------------------------------------------------
            if sem.get("semantic_type") in ("DATE", "DATETIME", "DURATION"):
                context["temporal_hints"].append(name)

            # -------------------------------------------------
            # Co-occurrence hints (cheap evidence only)
            # -------------------------------------------------
            if samples:
                context["co_occurrence_hints"].append({
                    "attribute": name,
                    "sample_values": samples
                })

        # -------------------------------------------------
        # OPTIONAL: relative scale comparison across attributes
        # (strengthens distributional semantics without roles)
        # -------------------------------------------------
        if len(context["relative_behavior"]) >= 2:
            context["relative_behavior_summary"] = {
                "comparison": [
                    (rb["attribute"], rb["scale_hint"])
                    for rb in context["relative_behavior"]
                ]
            }

        return context

    def build_vsi_prompt(self, entity_name, attr_name, attr_properties, context=None):
        """
        VSI (Value Semantics Inference) — deterministic, bounded.
        Uses stats + sample values + bounded semantic types.
        """

        stats = attr_properties.get("stats", {}) or {}
        sample_values = stats.get("sample_values", [])[:10]
        if not sample_values:
            sample_values = ["<NO SAMPLE VALUES AVAILABLE>"]

        attr_type = attr_properties.get("info", {}).get("type", "unknown")

        stats_json = json.dumps(stats, indent=2)
        samples_json = json.dumps(sample_values, indent=2)
        allowed_json = json.dumps(self.VALUE_SEMANTIC_TYPES, indent=2)
        context_json = json.dumps(context or {}, indent=2)


        schema_json = """{
  "value_semantics": {
    "primary_type": "<ONE_OF_ALLOWED_TYPES>",
    "secondary_types": [],
    "is_categorical": false,
    "is_identifier": false,
    "is_free_text": false,
    "numeric": {
      "is_numeric": false,
      "is_continuous": false,
      "is_discrete": false,
      "min": null,
      "max": null
    },
    "temporal": {
      "is_date": false,
      "is_datetime": false,
      "is_duration": false,
      "granularity": null
    },
    "normalization": {
      "can_normalize": false,
      "normalized_examples": [],
      "normalization_strategy": null
    },
    "confidence": 0.0,
    "examples_used": [],
    "notes": []
  }
}"""

        prompt = f"""
You are performing Value Semantics Inference (VSI), a deterministic and bounded semantic classification
module used by autonomous agents. You MUST infer WHAT THE VALUES *ARE*, not what they represent in the domain.

Rules:
- Use ONLY value patterns + statistics.
- You MAY use other attributes ONLY for structural AND distributional disambiguation
  (e.g., distinguishing durations vs counts, identifiers vs categories,
   event times vs boundaries).
- You MUST NOT infer business, policy, or domain concepts from context.
- Cross-attribute context is evidence, not ground truth.
- Use ONLY allowed semantic types.
- You MUST be deterministic, safe, and predictable.
- You MUST produce machine-usable semantics.
- DO NOT invent new types. Stay within allowed types.
- DO NOT infer business/domain meanings.

───────────────────────────────
ATTRIBUTE CONTEXT
───────────────────────────────
Entity: {entity_name}
Attribute: {attr_name}
Declared Type: {attr_type}

ATTRIBUTE_STATS:
{stats_json}

SAMPLE_VALUES:
{samples_json}

OTHER_ATTRIBUTES_IN_ENTITY (contextual structural hints only):
{context_json}


ALLOWED_SEMANTIC_TYPES:
{allowed_json}

───────────────────────────────
OUTPUT FORMAT (STRICT JSON ONLY)
───────────────────────────────

{schema_json}

Return ONLY this JSON structure, filled in appropriately.
"""
        return prompt.strip()


    def build_sdi_prompt(self, entity_name, attr_name, attr_properties, context=None):
        """
        SDI (Semantic Discovery Inference) — 
        This discovers fuzzy/emergent semantic concepts unconstrained by taxonomy.
        """

        return self.build_value_semantics_prompt(entity_name, attr_name, attr_properties, context=context)



    def build_value_semantics_prompt(self, entity_name, attr_name, attr_properties, context=None):
        """
        Build a generalized and production-grade prompt for Value Semantics Inference (VST).
        This design supports: open-world semantic categories, linguistic and structural inference,
        fuzzy categories, ranges, thresholds, and dynamic concept generation without relying on
        enumerated domain lists. Suitable for agentic systems and evolving datasets.
        """

        attr_stats = attr_properties.get("stats", {})
        sample_values = attr_stats.get("sample_values", [])[:10]
        if not sample_values:
            sample_values = ["<NO SAMPLE VALUES AVAILABLE>"]

        attr_type = attr_properties.get("info", {}).get("type", "unknown")

        prompt = f"""
        You are an advanced VALUE SEMANTICS inference engine designed for enterprise-scale,
        heterogeneous data. Your goal is to infer the **intrinsic semantic meaning** of the VALUES
        in a database attribute—based only on the values themselves, not business rules.

        ──────────────────────────────────────────────
        VALUE SEMANTICS (VST) — CORE PRINCIPLES
        ──────────────────────────────────────────────
        You operate under an OPEN-WORLD semantic model:

        1. VST is NOT a closed taxonomy.
        2. You may generate new semantic types if supported by patterns in the values.
        3. You infer meaning by analyzing:
        • linguistic patterns
        • structural patterns
        • categorical signals
        • statistical regularities
        • latent real-world semantics

        Never restrict yourself to predefined categories.

        ──────────────────────────────────────────────
        THE THREE-LAYER SEMANTIC MODEL
        ──────────────────────────────────────────────

        You infer semantics across three conceptual layers:

        LAYER 1 — STRUCTURAL SEMANTICS
            What *shape* do the values have?
            Examples: STRING, NUMBER, INTEGER, DATE, URL, EMAIL, ID_STRING, BOOLEAN, CODE

        LAYER 2 — LINGUISTIC / ENTITY SEMANTICS
            What *type of entity or label* do the values resemble?
            Examples (not exhaustive):
            - person names
            - job titles
            - certification names
            - skills or competencies
            - locations (city/state/country)
            - product names
            - organizational entities
            These categories are open-world: you may create new ones.

        LAYER 3 — CONCEPTUAL / CATEGORY SEMANTICS
            What *semantic class or conceptual grouping* do the values imply?
            This includes:
            - demographic groups (teenagers, seniors)
            - vehicle classes (heavy truck, light truck)
            - risk/quality tiers
            - product tiers (premium, basic)
            - salary or value levels (high/low/medium)
            - measurement categories (speed, volume, weight)
            - domain-relevant categories emerging from patterns
            Again, this is open-world: you may generate new conceptual classes.

        IMPORTANT:
            These layers are not mutually exclusive — you produce the *most informative*
            semantic type that best describes the values.

        ──────────────────────────────────────────────
        GENERALIZED DETECTION PATTERNS
        ──────────────────────────────────────────────
        Use the following reasoning strategies:

        • STRUCTURAL SIGNALS:
            - formats (dates, codes, emails, numbers)
            - token patterns
            - length consistency
            - character composition

        • LINGUISTIC SIGNALS:
            - multi-word phrases
            - professional titles
            - credential-like structures (“Certified…”, “Diploma in…”)
            - named entities (locations, organizations)
            - labels with standardized wording (hazard class, tier labels)

        • CATEGORY SIGNALS:
            - group names (“teenagers”, “seniors”)
            - categorical adjectives (high, low, premium, hazardous)
            - domain-like clusters (“heavy truck”, “backup technologies certifications”)
            - ordinal or tiered values

        • NUMERIC SEMANTICS:
            If values *represent* categories (not numbers), infer:
                – ranges (age group, tier)
                – thresholds (heavy truck → payload > X)
                – levels (risk, salary, quality)

        ──────────────────────────────────────────────
        WHEN TO USE FREE_TEXT
        ──────────────────────────────────────────────
        ONLY use FREE_TEXT if:

        1. Values show no consistent structure,
        2. No stable linguistic or categorical signals exist,
        3. Values are arbitrary human-written sentences with no common semantic class.

        This should be rare.

        ──────────────────────────────────────────────
        OUTPUT FORMAT (STRICT JSON ONLY)
        ──────────────────────────────────────────────

        {{
        "semantic_type": "string",
        "subcategory": "string or null",
        "confidence": 0.0,
        "range": {{
            "min": number or null,
            "max": number or null
        }},
        "criteria": "string or null",
        "rationale": "short explanation of the inferred semantics",
        "examples": []
        }}

        INTERPRETATION GUIDELINES:
        - semantic_type is the most informative object-level category.
        - subcategory is a refinement (e.g., HEAVY_TRUCK, TEENAGER, PREMIUM).
        - range is used only when applicable (ages, thresholds, numeric groups).
        - criteria is used for inferred thresholds or rules.
        - examples must be from the provided values.

        ──────────────────────────────────────────────
        ATTRIBUTE CONTEXT
        ──────────────────────────────────────────────
        Entity: {entity_name}
        Attribute: {attr_name}
        Declared Type: {attr_type}

        Sample Values:
        {json.dumps(sample_values, indent=2)}

        ───────────────────────────────
        CROSS-ATTRIBUTE CONTEXT (OPTIONAL EVIDENCE)
        ───────────────────────────────
        {json.dumps(context or {}, indent=2)}

        Guidelines:
        - Use this context ONLY to discover latent groupings, thresholds,
        or fuzzy categories.
        - Do NOT assign roles or enforce consistency across columns.

        Now infer the most accurate semantics and return ONLY valid JSON.
        """
        return prompt.strip()


    def infer_attribute_vsi(self, entity_name, attr):
        """
        Infer value semantics for an attribute."""

        attr_name = attr.get("name")
        props = attr.get("properties", {})

        context = self.build_attribute_context(
            self.current_entity_attributes, attr_name
        )

        prompt = self.build_vsi_prompt(entity_name, attr_name, props, context=context)
        llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

        try:
            parsed = json_utils.safe_json_parse(llm_output)
            if not parsed:
                return None

            # unwrap
            if "value_semantics" in parsed:
                vs = parsed["value_semantics"]
            else:
                vs = parsed

            # ensure compatibility with old DCM (“semantic_type”)
            if "primary_type" in vs:
                vs["semantic_type"] = vs.get("primary_type")

            return vs

        except Exception:
            logging.warning(f"Invalid VSI JSON for {entity_name}.{attr_name}")
            return None


    def infer_attribute_sdi(self, entity_name, attr):
        """
        Infer semantic discovery information for an attribute.
        """
        attr_name = attr.get("name")
        props = attr.get("properties", {})

        context = self.build_attribute_context(
            self.current_entity_attributes, attr_name
        )

        prompt = self.build_sdi_prompt(entity_name, attr_name, props, context=context)
        llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

        try:
            parsed = json_utils.safe_json_parse(llm_output)
            return parsed
        except Exception:
            logging.warning(f"Invalid SDI JSON for {entity_name}.{attr_name}")
            return None

    def infer_attribute_value_semantics(self, entity_name, attr):
        """
        Infer value semantics for an attribute.
        """
        attr_name = attr.get("name")
        attr_properties = attr.get("properties", {})

        # Build prompt
        prompt = self.build_value_semantics_prompt(entity_name, attr_name, attr_properties)

        # Call LLM
        llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

        # Parse LLM output
        try:
            semantics = json_utils.safe_json_parse(llm_output)
            if not semantics:
                logging.warning(f"Value semantics inference returned empty for {entity_name}.{attr_name}")
                return None
            return semantics
        except Exception:
            logging.warning(f"Invalid JSON from value semantics inference for {entity_name}.{attr_name}")
            return None

    def build_domain_concept_prompt(self, entity_name, attr_name, attr_properties):
        """
        Build a prompt for mapping an attribute to a domain concept.
        """
        taxonomy = self.properties.get("concept_taxonomy", [])
        taxonomy_text = "\n".join([f"- {c}" for c in taxonomy]) if taxonomy else "- CONCEPT.UNKNOWN"

        semantics = attr_properties.get("value_semantics", {})
        semantic_type = semantics.get("semantic_type", "UNKNOWN")

        # incorporate SDI
        sdi = attr_properties.get("semantic_discovery", {})
        sdi_text = json.dumps(sdi, indent=2) if sdi else "None"

        sample_values = semantics.get("examples", [])
        if not sample_values:
            sample_values = attr_properties.get("stats", {}).get("sample_values", [])[:5]

        if not sample_values:
            sample_values = ["<NO SAMPLE VALUES AVAILABLE>"]

        prompt = f"""
        You are performing domain concept mapping for a data registry.

        Map the attribute to a canonical DOMAIN CONCEPT.

        Available domain concepts:
        {taxonomy_text}

        Attribute Context:
        - Entity: {entity_name}
        - Attribute Name: {attr_name}
        - Semantic Type: {semantic_type}

        Semantic Discovery Output (SDI):
        {sdi_text}

        Sample Values:
        {json.dumps(sample_values, indent=2)}

        Output JSON ONLY:

        {{
            "concept": "CONCEPT.X.Y",
            "confidence": 0.0,
            "rationale": "explain briefly"
        }}
        """

        return prompt.strip()

    def infer_domain_concept(self, entity_name, attr):
        """
        Infer a domain concept for an attribute.
        """
        attr_name = attr.get("name")
        attr_properties = attr.get("properties", {})

        # Skip if no value semantics yet
        if "value_semantics" not in attr_properties:
            return None

        prompt = self.build_domain_concept_prompt(entity_name, attr_name, attr_properties)

        llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

        try:
            concept = json_utils.safe_json_parse(llm_output)
            if not concept:
                logging.warning(f"Domain concept inference empty for {entity_name}.{attr_name}")
                return None
            return concept
        except Exception:
            logging.warning(f"Invalid JSON from domain concept inference for {entity_name}.{attr_name}")
            return None


    def grouped_distribution_signal(self, group_attr, value_attr):
        """
        Detect whether grouping by group_attr produces
        meaningfully different distributions of value_attr.

        This version is calibrated for small sample_values and
        enables SEGMENTATION_DRIVER to emerge without hallucination.
        """

        g_vals = group_attr.get("properties", {}).get("stats", {}).get("sample_values", [])
        v_vals = value_attr.get("properties", {}).get("stats", {}).get("sample_values", [])

        if not g_vals or not v_vals:
            return False

        try:
            groups = {}

            for g, v in zip(g_vals, v_vals):
                try:
                    v = float(v)
                except Exception:
                    continue
                groups.setdefault(g, []).append(v)

            # Require enough groups and per-group support
            min_groups = 2   # instead of 3
            if len(groups) < min_groups:
                return False

            supported_groups = [vals for vals in groups.values() if len(vals) >= 2]
            if len(supported_groups) < 2:
                return False

            means = [sum(vals) / len(vals) for vals in groups.values()]

            max_mean = max(abs(m) for m in means) if means else 0.0
            if max_mean == 0:
                return False

            spread = max(means) - min(means)
            avg_mean = sum(abs(m) for m in means) / len(means)
            relative_spread = spread / avg_mean if avg_mean > 0 else 0.0

            # Two-tier acceptance:
            # 1. Absolute spread (classic signal)
            # 2. Relative spread (robust for small/noisy samples)
            return (
                spread > 0.12 * max_mean
                or relative_spread > 0.18
            )

        except Exception:
            return False

    def grouped_distribution_signal_row_aligned(self, group_attr, value_attr,row_samples):
        """
        Row-grounded version of grouped_distribution_signal.
        Uses real co-occurrence, not column samples.
        """

        g_name = group_attr["name"]
        v_name = value_attr["name"]

        g_vals = []
        v_vals = []

        for row in row_samples:
            if g_name not in row or v_name not in row:
                continue

            g = row.get(g_name)
            v = row.get(v_name)

            if g is None or v is None:
                continue

            try:
                v = float(v)
            except Exception:
                continue

            g_vals.append(g)
            v_vals.append(v)

        if len(g_vals) < 5:
            return False

        return self.grouped_distribution_signal(
            {"properties": {"stats": {"sample_values": g_vals}}},
            {"properties": {"stats": {"sample_values": v_vals}}},
        )

    def infer_semantic_links(self, attributes):
        """
        Find candidate semantic relationships between attributes.
        Returns a list of {source, target, signal}.
        """
        links = []

        for a in attributes:
            for b in attributes:
                if a["name"] == b["name"]:
                    continue

                # SEGMENTATION (distributional, data-driven)
                if self.grouped_distribution_signal(a, b):
                    links.append({
                        "source": a["name"],
                        "target": b["name"],
                        "signal": "grouped_distribution",
                        "row_support": False
                    })

                # ----------------------------------
                # FUNCTIONAL DEPENDENCY (numeric)
                # ----------------------------------
                if self.functional_dependency_signal(a, b):
                    links.append({
                        "source": a["name"],
                        "target": b["name"],
                        "signal": {
                            "type": "functional_dependency",
                            "strength": 0.7
                        }
                    })

        return links

    def validate_semantic_link(self, source, target, signal):
        """
        Validate a semantic link between two attributes.
        """
        src_props = source.get("properties", {})
        tgt_props = target.get("properties", {})

        src_vs = src_props.get("value_semantics", {})
        tgt_vs = tgt_props.get("value_semantics", {})

        src_semantic_type = src_vs.get("semantic_type", "UNKNOWN")
        tgt_semantic_type = tgt_vs.get("semantic_type", "UNKNOWN")

        # ---- Prompt ------------------------------------------------------
        prompt = f"""
    You are validating a semantic relationship between two attributes.

    Source attribute:
    - Name: {source.get('name', 'UNKNOWN')}
    - Semantic type: {src_semantic_type}

    Target attribute:
    - Name: {target.get('name', 'UNKNOWN')}
    - Semantic type: {tgt_semantic_type}

    Observed signal: {signal}

    Choose ONE relationship:
    - SEGMENTS
    - DERIVES
    - SUPPORTS
    - ASSOCIATED
    - UNRELATED

    If semantic types are UNKNOWN, rely primarily on the observed signal.

    Return JSON ONLY:
    {{
    "relationship": "...",
    "confidence": 0.0,
    "rationale": "short explanation"
    }}
    """

        out = self.execute_api_call(
            prompt,
            properties=self.properties,
            additional_data={}
        )

        return json_utils.safe_json_parse(out)


    def infer_semantic_role(self, target_attr, all_attrs, semantic_links=None):
        """
        Infer the semantic role of a target attribute based on its relationships.
        """

        semantic_links = semantic_links or []

        attr_name = target_attr["name"]
        vsi = target_attr.get("properties", {}).get("value_semantics", {})
        primary_type = vsi.get("semantic_type", "UNKNOWN")

        # -------------------------------------------------
        # 1. IDENTIFIER (hard rule)
        # -------------------------------------------------
        if vsi.get("is_identifier"):
            return {
                "primary_role": "IDENTIFIER",
                "confidence": 0.95,
                "role_arguments": {},
                "justification": {
                    "rule": "identifier_flag"
                }
            }

        outgoing = [l for l in semantic_links if l["source"] == attr_name]
        incoming = [l for l in semantic_links if l["target"] == attr_name]

        # -------------------------------------------------
        # 2. SEGMENTATION DRIVER
        # -------------------------------------------------
        segment_links = []
        for l in outgoing:
            if l["relationship"] != "SEGMENTS":
                continue

            target = next(
                (a for a in all_attrs if a["name"] == l["target"]),
                None
            )
            if not target:
                continue

            target_vsi = target.get("properties", {}).get("value_semantics", {})
            if target_vsi.get("is_identifier"):
                continue  

            segment_links.append(l)

        if segment_links:
            return {
                "primary_role": "SEGMENTATION_DRIVER",
                "confidence": min(
                    0.95,
                    sum(l.get("confidence", 0.7) for l in segment_links) / len(segment_links)
                ),
                "role_arguments": {
                    "segments": [l["target"] for l in segment_links]
                },
                "justification": {
                    "signals": ["SEGMENTS"],
                    "link_count": len(segment_links)
                }
            }

        # -------------------------------------------------
        # RELATIONSHIP-DRIVEN FALLBACK (GENERIC)
        # -------------------------------------------------
        supports = [l for l in outgoing if l["relationship"] == "SUPPORTS"]

        if supports:
            return {
                "primary_role": "EVIDENCE",
                "confidence": min(
                    0.9,
                    sum(l.get("confidence", 0.7) for l in supports) / len(supports)
                ),
                "role_arguments": {
                    "supports": [l["target"] for l in supports]
                },
                "justification": {
                    "signals": ["SUPPORTS"]
                }
            }

        # -------------------------------------------------
        # DERIVED MEASURE
        # -------------------------------------------------
        #derive_links = [l for l in incoming if l["relationship"] == "DERIVES"]
        #if derive_links:
        #    return {
        #        "primary_role": "DERIVED_MEASURE",
        #        "confidence": min(
        #            0.95,
        #            sum(l.get("confidence", 0.7) for l in derive_links) / len(derive_links)
        #        ),
        #        "role_arguments": {
        #            "derived_from": [l["source"] for l in derive_links]
        #        },
        #        "justification": {
        #            "signals": ["DERIVES"],
        #            "link_count": len(derive_links)
        #        }
        #    }


        # -------------------------------------------------
        # WEAK TYPE PRIOR
        # -------------------------------------------------
        if primary_type in ("DATE", "DATETIME"):
            return {
                "primary_role": "EVENT_TIME",
                "confidence": 0.55,
                "role_arguments": {},
                "justification": {
                    "type_prior": primary_type
                }
            }

        # -------------------------------------------------
        # FALLBACK
        # -------------------------------------------------
        return {
            "primary_role": "DESCRIPTIVE",
            "confidence": 0.4,
            "role_arguments": {},
            "justification": {
                "reason": "no causal or statistical signals detected"
            }
        }

    def infer_row_based_links(self, attributes, row_samples):
        """
        Infer semantic links using row-grounded evidence.
         Returns a list of {source, target, signal}.
         """

        links = []

        if not row_samples:
            return links

        for a in attributes:
            for b in attributes:
                if a["name"] == b["name"]:
                    continue

                a_type = a.get("properties", {}).get("value_semantics", {}).get("semantic_type")
                b_type = b.get("properties", {}).get("value_semantics", {}).get("semantic_type")

                # ----------------------------------
                # SEGMENTATION (row-grounded)
                # ----------------------------------
                if self.grouped_distribution_signal_row_aligned(a, b, row_samples):
                    links.append({
                        "source": a["name"],
                        "target": b["name"],
                        "signal": "grouped_distribution",
                        "row_support": True
                    })


                # TEMPORAL ALIGNMENT (row-grounded, generic)
                if (
                a_type in ("DATE", "DATETIME", "TIME", "DURATION")
                and b_type not in ("DATE", "DATETIME", "TIME", "DURATION")
                and self.has_row_cooccurrence(a, b, row_samples)
                ):
                    links.append({
                        "source": a["name"],
                        "target": b["name"],
                        "signal": {
                            "type": "temporal_alignment",
                            "strength": 0.6,
                            "row_support": True
                        }
                    })

        return links


    def is_value_axis_eligible(self, attr):
        """
        Decide whether an attribute is eligible for value-axis inference.
        This is a SEMANTIC gate, not a statistical one.
        """

        props = attr.get("properties", {})
        vsi = props.get("value_semantics", {}) or {}

        semantic_type = vsi.get("semantic_type", "UNKNOWN")

        # ---- HARD EXCLUSIONS ----------------------------------
        # Identifiers, opaque codes, labels, join keys
        if vsi.get("is_identifier"):
            return False

        if semantic_type in {
            "ID_STRING",
            "ID_NUMERIC",
            "UUID",
            "HASH",
            "CODE",
            "TAG",
            "LABEL",
            "ENUM_CATEGORY",   
            "TEXT_CATEGORY"
        }:
            return False

        # ---- ALLOWED NUMERIC AXES -----------------------------
        if semantic_type in {
            "CURRENCY_AMOUNT",
            "INTEGER",
            "FLOAT",
            "NUMERIC_GENERAL",
            "PERCENTAGE",
            "RATIO",
            "DURATION"
        }:
            return True

        return False


    def infer_numeric_value_axis(self, attr, row_samples):
        """
        Infer magnitude-based axis for numeric attributes
        """

        name = attr["name"]
        values = []

        for row in row_samples:
            try:
                values.append(float(row.get(name)))
            except Exception:
                pass

        if len(values) < 20:
            return None

        # semantic gate
        if self.is_sparse_count_axis(attr, values):
            threshold = self.infer_tail_threshold(values)
            if threshold is None:
                threshold = 1

            return {
                "axis_type": "DISCRETE_COUNT",
                "polarity": {
                    "direction": "increasing",
                    "meaning": "severity"
                },
                "extreme_regions": {
                    "large": f">= {threshold}"
                },
                "confidence": 0.85,
                "rationale": (
                    "Sparse, zero-inflated discrete counts detected; "
                    "extremes inferred via tail-regime semantics instead of percentiles"
                )
            }


        p10 = np.percentile(values, 10)
        p90 = np.percentile(values, 90)

        return {
            "axis_type": "CONTINUOUS",
            "polarity": {
                "direction": "increasing",
                "meaning": "magnitude"
            },
            "extreme_regions": {
                "small": f"< {round(p10, 2)}",
                "large": f"> {round(p90, 2)}"
            },
            "confidence": 0.7,
            "rationale": "Distributional extremes inferred via quantiles"
        }


    def infer_value_axis(self, attributes, row_samples):
        """
        Infer value axes for attributes when supported.
        Returns dict: {attr_name: axis_metadata}
        """

        axes = {}

        # -------------------------------------------------
        # 1. NUMERIC MAGNITUDE AXES 
        # -------------------------------------------------
        for attr in attributes:
            if not self.is_value_axis_eligible(attr):
                continue

            semantic_type = (
                attr.get("properties", {})
                    .get("value_semantics", {})
                    .get("semantic_type")
            )

            if semantic_type in (
                "CURRENCY_AMOUNT",
                "INTEGER",
                "FLOAT",
                "NUMERIC_GENERAL",
                "PERCENTAGE",
                "RATIO",
                "DURATION"
            ):
                axis = self.infer_numeric_value_axis(attr, row_samples)
                if axis:
                    axes[attr["name"]] = axis

        # -------------------------------------------------
        # 2. ORDINAL AXES FROM IVS 
        # -------------------------------------------------
        for attr in attributes:
            name = attr["name"]

            if name in axes:
                continue  

            if not self.is_ivs_promotable_to_value_axis(attr):
                continue

            #ivs = attr["properties"]["interpretive_semantics"]
            ivs = attr.get("properties", {}).get("interpretive_semantics")
            if not ivs:
                continue

            axis = {
                "axis_type": "ORDINAL",
                "ordering": ivs.get("ordering", []),
                "polarity": ivs.get("polarity", {}),
                "extreme_regions": {
                    "low": ivs.get("ordering", [])[:1],
                    "high": ivs.get("ordering", [])[-1:]
                },
                "confidence": ivs.get("confidence", 0.7),
                "rationale": (
                    "Promoted from interpretive semantics "
                    "after confidence and semantic eligibility checks"
                ),
                "source": "interpretive_semantics"
            }

            axes[name] = axis

            logging.info(
                f"[ValueAxis] Promoted IVS → ORDINAL axis for {name}: {axis}"
            )

        return axes

    def is_ivs_promotable_to_value_axis(self, attr):
        """
        Decide whether interpretive semantics may be promoted
        to a VALUE AXIS (ORDINAL).

        This is a STRICT gate.
        """

        props = attr.get("properties", {})
        ivs = props.get("interpretive_semantics", {})
        vsi = props.get("value_semantics", {})

        if not ivs:
            return False

        if ivs.get("interpretation_type") not in ("ORDINAL", "TIER", "SEVERITY"):
            return False

        if ivs.get("confidence", 0.0) < 0.7:
            return False

        # Identifiers never qualify
        if vsi.get("is_identifier"):
            return False

        # Only categorical value types may enter this path
        if vsi.get("semantic_type") not in (
            "ENUM_CATEGORY",
            "TEXT_CATEGORY",
            "LABEL"
        ):
            return False

        return True

    def build_interpretive_semantics_prompt(
        self,
        entity_name,
        attr_name,
        attr_properties
    ):
        """
        Build a prompt for inferring interpretive semantics.
        """

        stats = attr_properties.get("stats", {})
        samples = stats.get("sample_values", [])[:10]

        sdi = attr_properties.get("semantic_discovery", {})
        vsi = attr_properties.get("value_semantics", {})

        return f"""
    You are performing INTERPRETIVE VALUE SEMANTICS (IVS).

    Your task:
    Infer WHETHER the categorical values imply an ORDERING, SEVERITY, or TIER
    that an intelligent agent *might* use when reasoning.

    IMPORTANT SAFETY RULES:
    - Do NOT assume meaning unless strongly implied.
    - If ordering is ambiguous, return interpretation_type = "NONE".
    - This output is OPTIONAL and MUST NOT be treated as ground truth.
    - Use SDI and linguistic signals as evidence, not authority.

    ────────────────────────────
    ATTRIBUTE CONTEXT
    ────────────────────────────
    Entity: {entity_name}
    Attribute: {attr_name}

    Sample Values:
    {json.dumps(samples, indent=2)}

    Value Semantics (VSI):
    {json.dumps(vsi, indent=2)}

    Semantic Discovery (SDI):
    {json.dumps(sdi, indent=2)}

    ────────────────────────────
    INTERPRETATION TYPES
    ────────────────────────────
    ORDINAL   → ordered categories (A < B < C)
    SEVERITY  → worse/better progression
    TIER      → product / quality levels
    NONE      → no safe interpretation

    ────────────────────────────
    OUTPUT FORMAT (STRICT JSON)
    ────────────────────────────
    {{
    "interpretation_type": "ORDINAL | SEVERITY | TIER | NONE",
    "ordering": [],
    "polarity": {{
        "direction": "increasing | decreasing",
        "meaning": "risk | severity | quality | preference | level"
    }},
    "confidence": 0.0,
    "rationale": "short explanation",
    "evidence": {{
        "examples": [],
        "source": "labels | SDI | linguistic"
    }}
    }}
    """

    def infer_interpretive_semantics(self, entity_name, attr):
        """
        Infer interpretive semantics for an attribute.
        Returns interpretive semantics dict or None.
        """
        attr_name = attr["name"]
        props = attr.get("properties", {})

        # Hard gate: numeric & identifiers NEVER qualify
        vsi = props.get("value_semantics", {})
        if vsi.get("is_identifier"):
            return None

        semantic_type = vsi.get("semantic_type")
        if semantic_type not in ("ENUM_CATEGORY", "TEXT_CATEGORY"):
            return None

        prompt = self.build_interpretive_semantics_prompt(
            entity_name, attr_name, props
        )

        out = self.execute_api_call(
            prompt,
            properties=self.properties,
            additional_data={}
        )

        try:
            parsed = json_utils.safe_json_parse(out)

            if not parsed:
                return None

            # Strong safety filter
            if parsed.get("interpretation_type") == "NONE":
                return None

            if parsed.get("confidence", 0) < 0.6:
                return None

            return parsed

        except Exception:
            logging.warning(
                f"[IVS] Invalid interpretive semantics for {entity_name}.{attr_name}"
            )
            return None

    def infer_conditional_distributions(self, group_attr, value_attr, row_samples):
        """
        Discover conditional distribution semantics:
        P(value | group)
        """

        g_name = group_attr["name"]
        v_name = value_attr["name"]

        groups = {}

        for row in row_samples:
            g = row.get(g_name)
            v = row.get(v_name)

            if g is None or v is None:
                continue

            try:
                v = float(v)
            except Exception:
                continue

            groups.setdefault(g, []).append(v)

        profiles = {}
        for g, vals in groups.items():
            if len(vals) < 10:
                continue

            p50 = np.percentile(vals, 50)
            p90 = np.percentile(vals, 90)
            p99 = np.percentile(vals, 99)

            profiles[str(g)] = {   
                "count": len(vals),
                "p50": round(p50, 2),
                "p90": round(p90, 2),
                "p99": round(p99, 2),
                "typical_range": [round(np.min(vals), 2), round(p90, 2)],
                "extreme_range": [round(p90, 2), round(np.max(vals), 2)],
            }


        if len(profiles) < 2:
            return None

        MAX_GROUPS = 10
        if len(profiles) > MAX_GROUPS:
            return None

        return {
            "group_attribute": g_name,
            "value_attribute": v_name,
            "profiles": profiles,
            "recommended_stat": "p99",
            "confidence": 0.7,
            "rationale": "Conditional distributions inferred from row-aligned numeric behavior"
        }

    def is_sparse_count_axis(self, attr, values):
        """
        Detect sparse, zero-inflated, discrete count variables
        where percentile-based extremes are semantically invalid.
        """

        if not values:
            return False

        vsi = attr.get("properties", {}).get("value_semantics", {})
        semantic_type = vsi.get("semantic_type")

        # ---- semantic gate ---------------------------------
        if semantic_type not in {"INTEGER", "NUMERIC_GENERAL"}:
            return False

        # ---- hard exclude spatial / continuous numerics ----
        name = attr.get("name", "").lower()
        if name in {"latitude", "longitude", "lat", "lon", "lng"}:
            return False

        numeric = vsi.get("numeric", {}) or {}
        if not numeric.get("is_discrete", True):
            return False

        # ---- distributional signals ------------------------
        n = len(values)
        zero_frac = sum(v == 0 for v in values) / n
        non_zero = [v for v in values if v > 0]

        #if not non_zero:
        #    return False

        max_val = max(values)
        distinct = len(set(values))

        # High zero mass + discrete support => sparse count
        if zero_frac >= 0.7 and distinct <= 10:
            return True

        if zero_frac >= 0.4 and max_val >= 5:
            return True

        return False

    def infer_tail_threshold(self, values):
        """
        Given a numeric column that has already been classified as a sparse discrete count, 
        infer a threshold that marks the beginning of the “extreme” tail.
        Automatically find a cutoff that separates:
        normal / common values from rare, extreme, or regime-changing values.
        This is classic long-tail / power-law count data behavior:
        many zeros, many small counts, very few large counts, but those large counts are semantically important (e.g., mass casualty events)
        """

        values = sorted(values)
        n = len(values)

        non_zero = [v for v in values if v > 0]
        if not non_zero:
            return None

        # ---- Case 1: binary-tail  ---------------
        ## “Presence vs absence is the meaningful distinction.”
        if max(non_zero) <= 3:
            return 1

        # ---- Case 2: scalable long-tail discrete counts ---------------------
        # Extreme tail detection
        # We search for the smallest interpretable count threshold t
        # such that only a very small fraction of rows (e.g., <1%)
        # reach or exceed t. This identifies a regime change where
        # values transition from common occurrences to rare, extreme events.
        # Unlike percentiles, this tail-frequency criterion remains stable
        # under heavy zero-inflation and preserves integer semantics.
        for t in [5, 10, 15, 20]:
            frac = sum(v >= t for v in values) / n
            if frac < 0.01:
                return t

        # Fallback: just take the extreme
        return max(values)

build_attribute_context(attributes, target_attr_name)

Build lightweight cross-attribute context for VSI / SDI.

Design principles: - Descriptive, not prescriptive (no hard semantic claims) - Distributional, not role-based - Column-attached, not joint inference (no relationships inferred) - Safe for first-pass VSI / SDI

Source code in blue/metadata.py
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
def build_attribute_context(self, attributes, target_attr_name):
    """
    Build lightweight cross-attribute context for VSI / SDI.

    Design principles:
    - Descriptive, not prescriptive (no hard semantic claims)
    - Distributional, not role-based
    - Column-attached, not joint inference (no relationships inferred)
    - Safe for first-pass VSI / SDI
    """

    # Context object passed downstream to VSI / SDI.
    # Each field captures a different *type* of weak signal.
    context = {
        "sibling_attributes": [],
        "numeric_distributions": {},
        "temporal_hints": [],
        "co_occurrence_hints": [],
        "relative_behavior": []   # distributional semantics
    }

    for attr in attributes:
        name = attr.get("name")
        if name == target_attr_name:
            continue

        props = attr.get("properties", {}) or {}
        stats = props.get("stats", {}) or {}
        sem = props.get("value_semantics", {}) or {}

        samples = stats.get("sample_values", [])[:5]

        # -------------------------------------------------
        # Sibling attribute names
        # -------------------------------------------------
        context["sibling_attributes"].append(name)

        # -------------------------------------------------
        # Robust numeric detection (NO VSI DEPENDENCY)
        # -------------------------------------------------
        min_val = stats.get("min")
        max_val = stats.get("max")

        is_numeric = (
            isinstance(min_val, (int, float))
            or isinstance(max_val, (int, float))
        )

        if is_numeric:
            context["numeric_distributions"][name] = {
                "min": min_val,
                "max": max_val,
                "distinct": stats.get("distinct_count")
            }

            # -------------------------------------------------
            # Distributional role hints (NOT semantic roles)
            # Skip identifiers — they distort scale semantics
            # Provide very coarse magnitude hints to support
            # relative scale reasoning (e.g., thresholds, extremes).
            # -------------------------------------------------
            if not sem.get("is_identifier"):
                magnitude = max_val
                if isinstance(magnitude, (int, float)):
                    context["relative_behavior"].append({
                        "attribute": name,
                        "scale_hint": (
                            "small_range" if magnitude < 100
                            else "medium_range" if magnitude < 10000
                            else "large_range"
                        )
                    })

        # -------------------------------------------------
        # Temporal hints (weak, non-binding)
        # Record presence of time-like attributes nearby.
        # This does NOT imply that the target attribute
        # participates in temporal reasoning.
        # -------------------------------------------------
        if sem.get("semantic_type") in ("DATE", "DATETIME", "DURATION"):
            context["temporal_hints"].append(name)

        # -------------------------------------------------
        # Co-occurrence hints (cheap evidence only)
        # -------------------------------------------------
        if samples:
            context["co_occurrence_hints"].append({
                "attribute": name,
                "sample_values": samples
            })

    # -------------------------------------------------
    # OPTIONAL: relative scale comparison across attributes
    # (strengthens distributional semantics without roles)
    # -------------------------------------------------
    if len(context["relative_behavior"]) >= 2:
        context["relative_behavior_summary"] = {
            "comparison": [
                (rb["attribute"], rb["scale_hint"])
                for rb in context["relative_behavior"]
            ]
        }

    return context

build_collection_description_prompt(collection_name, entity_descriptions, collection_metadata)

Build a prompt string for generating or enriching a collection description.

Constructs a formatted text prompt using entity-level descriptions and metadata, suitable for passing to an LLM or enrichment API.

Parameters:

Name Type Description Default
collection_name str

The name of the collection.

required
entity_descriptions dict

Mapping of entity names to their descriptions.

required
collection_metadata dict or str

Additional metadata for the collection.

required

Returns:

Name Type Description
str

A formatted prompt string for collection-level description enrichment.

Source code in blue/metadata.py
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
def build_collection_description_prompt(self, collection_name, entity_descriptions, collection_metadata):
    """
    Build a prompt string for generating or enriching a collection description.

    Constructs a formatted text prompt using entity-level descriptions and
    metadata, suitable for passing to an LLM or enrichment API.

    Parameters:
        collection_name (str): The name of the collection.
        entity_descriptions (dict): Mapping of entity names to their descriptions.
        collection_metadata (dict or str): Additional metadata for the collection.

    Returns:
        str: A formatted prompt string for collection-level description enrichment.
    """
    child_descriptions = [f"{name}: {desc}" for name, desc in entity_descriptions.items() if desc]
    if not child_descriptions:
        child_descriptions = ["No entity descriptions available"]

    return self.properties['aggregation_prompt'].format(
        child_type='entity',
        parent_type='collection',
        child_descriptions='\n'.join(child_descriptions),
        parent_metadata=f"Collection name: {collection_name}\nMetadata: {collection_metadata}",
    )

build_database_description_prompt(database_name, collection_descriptions, database_metadata)

Build a prompt string for generating or enriching a database description.

Constructs a formatted text prompt using collection-level descriptions and metadata, suitable for passing to an LLM or enrichment API.

Parameters:

Name Type Description Default
database_name str

The name of the database.

required
collection_descriptions dict

Mapping of collection names to their descriptions.

required
database_metadata dict or str

Additional metadata for the database.

required

Returns:

Name Type Description
str

A formatted prompt string for database-level description enrichment.

Source code in blue/metadata.py
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
def build_database_description_prompt(self, database_name, collection_descriptions, database_metadata):
    """
    Build a prompt string for generating or enriching a database description.

    Constructs a formatted text prompt using collection-level descriptions and
    metadata, suitable for passing to an LLM or enrichment API.

    Parameters:
        database_name (str): The name of the database.
        collection_descriptions (dict): Mapping of collection names to their descriptions.
        database_metadata (dict or str): Additional metadata for the database.

    Returns:
        str: A formatted prompt string for database-level description enrichment.
    """

    child_descriptions = [f"{name}: {desc}" for name, desc in collection_descriptions.items() if desc]
    if not child_descriptions:
        child_descriptions = ["No collection descriptions available"]

    return self.properties['aggregation_prompt'].format(
        child_type='collection', parent_type='database', child_descriptions='\n'.join(child_descriptions), parent_metadata=f"Database name: {database_name}\nMetadata: {database_metadata}"
    )

build_domain_concept_prompt(entity_name, attr_name, attr_properties)

Build a prompt for mapping an attribute to a domain concept.

Source code in blue/metadata.py
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
def build_domain_concept_prompt(self, entity_name, attr_name, attr_properties):
    """
    Build a prompt for mapping an attribute to a domain concept.
    """
    taxonomy = self.properties.get("concept_taxonomy", [])
    taxonomy_text = "\n".join([f"- {c}" for c in taxonomy]) if taxonomy else "- CONCEPT.UNKNOWN"

    semantics = attr_properties.get("value_semantics", {})
    semantic_type = semantics.get("semantic_type", "UNKNOWN")

    # incorporate SDI
    sdi = attr_properties.get("semantic_discovery", {})
    sdi_text = json.dumps(sdi, indent=2) if sdi else "None"

    sample_values = semantics.get("examples", [])
    if not sample_values:
        sample_values = attr_properties.get("stats", {}).get("sample_values", [])[:5]

    if not sample_values:
        sample_values = ["<NO SAMPLE VALUES AVAILABLE>"]

    prompt = f"""
    You are performing domain concept mapping for a data registry.

    Map the attribute to a canonical DOMAIN CONCEPT.

    Available domain concepts:
    {taxonomy_text}

    Attribute Context:
    - Entity: {entity_name}
    - Attribute Name: {attr_name}
    - Semantic Type: {semantic_type}

    Semantic Discovery Output (SDI):
    {sdi_text}

    Sample Values:
    {json.dumps(sample_values, indent=2)}

    Output JSON ONLY:

    {{
        "concept": "CONCEPT.X.Y",
        "confidence": 0.0,
        "rationale": "explain briefly"
    }}
    """

    return prompt.strip()

build_entity_description_prompt(entity_obj, attributes)

Build a prompt for generating an entity description using an LLM.

Constructs a structured text prompt containing entity metadata and attribute information, suitable for guiding an LLM to produce a JSON-formatted description of the entity and its attributes.

Parameters:

Name Type Description Default
entity_obj dict

A dictionary representing the entity, from the data registry.

required
attributes list[dict]

A list of attribute definitions.

required

Returns:

Name Type Description
str

A formatted multi-line string prompt, instructing the LLM to produce

a JSON object with: - "table_description": Human-readable description of the entity. - "attributes": Mapping of attribute names to their descriptions.

Source code in blue/metadata.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def build_entity_description_prompt(self, entity_obj, attributes):
    """
    Build a prompt for generating an entity description using an LLM.

    Constructs a structured text prompt containing entity metadata and attribute
    information, suitable for guiding an LLM to produce a JSON-formatted
    description of the entity and its attributes.

    Parameters:
        entity_obj (dict): A dictionary representing the entity, from
            the data registry.
        attributes (list[dict]): A list of attribute definitions.

    Returns:
        str: A formatted multi-line string prompt, instructing the LLM to produce
        a JSON object with:
            - "table_description": Human-readable description of the entity.
            - "attributes": Mapping of attribute names to their descriptions.

    """

    # Extract basic info
    name = entity_obj.get("name", "Unknown")
    scope = entity_obj.get("scope", "Unknown")
    etype = entity_obj.get("type", "Unknown")

    attr_lines = []

    for attr in attributes:
        attr_properties = attr.get("properties", {})
        attr_properties_info = attr_properties.get("info", {})
        attr_type = attr_properties_info.get("type", "unknown")

        attr_name = attr.get("name")
        attr_stats = attr_properties.get("stats", {})

        sample_values = attr_stats.get("sample_values", [])

        attr_lines.append(f"- {attr_name} ({attr_type}), samples: {', '.join(map(str, sample_values[:3]))}")

    # Build the final prompt
    prompt = f"""
    You are given a database entity definition with its attributes and metadata.
    Your task is to generate a structured JSON output with:
    1. A concise human-readable description of what this table/entity represents.
    2. Concise descriptions of each attribute.

    Entity Name: {name}
    Scope: {scope}
    Type: {etype}

    Attributes:
    {chr(10).join(attr_lines)}

    Output JSON format (do not include extra commentary, only valid JSON):

    {{
    "table_description": "string",
    "attributes": {{
        "attr_name": "description of attribute",
        ...
    }}
    }}
    """
    return prompt

build_interpretive_semantics_prompt(entity_name, attr_name, attr_properties)

Build a prompt for inferring interpretive semantics.

Source code in blue/metadata.py
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
def build_interpretive_semantics_prompt(
    self,
    entity_name,
    attr_name,
    attr_properties
):
    """
    Build a prompt for inferring interpretive semantics.
    """

    stats = attr_properties.get("stats", {})
    samples = stats.get("sample_values", [])[:10]

    sdi = attr_properties.get("semantic_discovery", {})
    vsi = attr_properties.get("value_semantics", {})

    return f"""
You are performing INTERPRETIVE VALUE SEMANTICS (IVS).

Your task:
Infer WHETHER the categorical values imply an ORDERING, SEVERITY, or TIER
that an intelligent agent *might* use when reasoning.

IMPORTANT SAFETY RULES:
- Do NOT assume meaning unless strongly implied.
- If ordering is ambiguous, return interpretation_type = "NONE".
- This output is OPTIONAL and MUST NOT be treated as ground truth.
- Use SDI and linguistic signals as evidence, not authority.

────────────────────────────
ATTRIBUTE CONTEXT
────────────────────────────
Entity: {entity_name}
Attribute: {attr_name}

Sample Values:
{json.dumps(samples, indent=2)}

Value Semantics (VSI):
{json.dumps(vsi, indent=2)}

Semantic Discovery (SDI):
{json.dumps(sdi, indent=2)}

────────────────────────────
INTERPRETATION TYPES
────────────────────────────
ORDINAL   → ordered categories (A < B < C)
SEVERITY  → worse/better progression
TIER      → product / quality levels
NONE      → no safe interpretation

────────────────────────────
OUTPUT FORMAT (STRICT JSON)
────────────────────────────
{{
"interpretation_type": "ORDINAL | SEVERITY | TIER | NONE",
"ordering": [],
"polarity": {{
    "direction": "increasing | decreasing",
    "meaning": "risk | severity | quality | preference | level"
}},
"confidence": 0.0,
"rationale": "short explanation",
"evidence": {{
    "examples": [],
    "source": "labels | SDI | linguistic"
}}
}}
"""

build_sdi_prompt(entity_name, attr_name, attr_properties, context=None)

SDI (Semantic Discovery Inference) — This discovers fuzzy/emergent semantic concepts unconstrained by taxonomy.

Source code in blue/metadata.py
1056
1057
1058
1059
1060
1061
1062
def build_sdi_prompt(self, entity_name, attr_name, attr_properties, context=None):
    """
    SDI (Semantic Discovery Inference) — 
    This discovers fuzzy/emergent semantic concepts unconstrained by taxonomy.
    """

    return self.build_value_semantics_prompt(entity_name, attr_name, attr_properties, context=context)

build_value_semantics_prompt(entity_name, attr_name, attr_properties, context=None)

Build a generalized and production-grade prompt for Value Semantics Inference (VST). This design supports: open-world semantic categories, linguistic and structural inference, fuzzy categories, ranges, thresholds, and dynamic concept generation without relying on enumerated domain lists. Suitable for agentic systems and evolving datasets.

Source code in blue/metadata.py
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
def build_value_semantics_prompt(self, entity_name, attr_name, attr_properties, context=None):
    """
    Build a generalized and production-grade prompt for Value Semantics Inference (VST).
    This design supports: open-world semantic categories, linguistic and structural inference,
    fuzzy categories, ranges, thresholds, and dynamic concept generation without relying on
    enumerated domain lists. Suitable for agentic systems and evolving datasets.
    """

    attr_stats = attr_properties.get("stats", {})
    sample_values = attr_stats.get("sample_values", [])[:10]
    if not sample_values:
        sample_values = ["<NO SAMPLE VALUES AVAILABLE>"]

    attr_type = attr_properties.get("info", {}).get("type", "unknown")

    prompt = f"""
    You are an advanced VALUE SEMANTICS inference engine designed for enterprise-scale,
    heterogeneous data. Your goal is to infer the **intrinsic semantic meaning** of the VALUES
    in a database attribute—based only on the values themselves, not business rules.

    ──────────────────────────────────────────────
    VALUE SEMANTICS (VST) — CORE PRINCIPLES
    ──────────────────────────────────────────────
    You operate under an OPEN-WORLD semantic model:

    1. VST is NOT a closed taxonomy.
    2. You may generate new semantic types if supported by patterns in the values.
    3. You infer meaning by analyzing:
    • linguistic patterns
    • structural patterns
    • categorical signals
    • statistical regularities
    • latent real-world semantics

    Never restrict yourself to predefined categories.

    ──────────────────────────────────────────────
    THE THREE-LAYER SEMANTIC MODEL
    ──────────────────────────────────────────────

    You infer semantics across three conceptual layers:

    LAYER 1 — STRUCTURAL SEMANTICS
        What *shape* do the values have?
        Examples: STRING, NUMBER, INTEGER, DATE, URL, EMAIL, ID_STRING, BOOLEAN, CODE

    LAYER 2 — LINGUISTIC / ENTITY SEMANTICS
        What *type of entity or label* do the values resemble?
        Examples (not exhaustive):
        - person names
        - job titles
        - certification names
        - skills or competencies
        - locations (city/state/country)
        - product names
        - organizational entities
        These categories are open-world: you may create new ones.

    LAYER 3 — CONCEPTUAL / CATEGORY SEMANTICS
        What *semantic class or conceptual grouping* do the values imply?
        This includes:
        - demographic groups (teenagers, seniors)
        - vehicle classes (heavy truck, light truck)
        - risk/quality tiers
        - product tiers (premium, basic)
        - salary or value levels (high/low/medium)
        - measurement categories (speed, volume, weight)
        - domain-relevant categories emerging from patterns
        Again, this is open-world: you may generate new conceptual classes.

    IMPORTANT:
        These layers are not mutually exclusive — you produce the *most informative*
        semantic type that best describes the values.

    ──────────────────────────────────────────────
    GENERALIZED DETECTION PATTERNS
    ──────────────────────────────────────────────
    Use the following reasoning strategies:

    • STRUCTURAL SIGNALS:
        - formats (dates, codes, emails, numbers)
        - token patterns
        - length consistency
        - character composition

    • LINGUISTIC SIGNALS:
        - multi-word phrases
        - professional titles
        - credential-like structures (“Certified…”, “Diploma in…”)
        - named entities (locations, organizations)
        - labels with standardized wording (hazard class, tier labels)

    • CATEGORY SIGNALS:
        - group names (“teenagers”, “seniors”)
        - categorical adjectives (high, low, premium, hazardous)
        - domain-like clusters (“heavy truck”, “backup technologies certifications”)
        - ordinal or tiered values

    • NUMERIC SEMANTICS:
        If values *represent* categories (not numbers), infer:
            – ranges (age group, tier)
            – thresholds (heavy truck → payload > X)
            – levels (risk, salary, quality)

    ──────────────────────────────────────────────
    WHEN TO USE FREE_TEXT
    ──────────────────────────────────────────────
    ONLY use FREE_TEXT if:

    1. Values show no consistent structure,
    2. No stable linguistic or categorical signals exist,
    3. Values are arbitrary human-written sentences with no common semantic class.

    This should be rare.

    ──────────────────────────────────────────────
    OUTPUT FORMAT (STRICT JSON ONLY)
    ──────────────────────────────────────────────

    {{
    "semantic_type": "string",
    "subcategory": "string or null",
    "confidence": 0.0,
    "range": {{
        "min": number or null,
        "max": number or null
    }},
    "criteria": "string or null",
    "rationale": "short explanation of the inferred semantics",
    "examples": []
    }}

    INTERPRETATION GUIDELINES:
    - semantic_type is the most informative object-level category.
    - subcategory is a refinement (e.g., HEAVY_TRUCK, TEENAGER, PREMIUM).
    - range is used only when applicable (ages, thresholds, numeric groups).
    - criteria is used for inferred thresholds or rules.
    - examples must be from the provided values.

    ──────────────────────────────────────────────
    ATTRIBUTE CONTEXT
    ──────────────────────────────────────────────
    Entity: {entity_name}
    Attribute: {attr_name}
    Declared Type: {attr_type}

    Sample Values:
    {json.dumps(sample_values, indent=2)}

    ───────────────────────────────
    CROSS-ATTRIBUTE CONTEXT (OPTIONAL EVIDENCE)
    ───────────────────────────────
    {json.dumps(context or {}, indent=2)}

    Guidelines:
    - Use this context ONLY to discover latent groupings, thresholds,
    or fuzzy categories.
    - Do NOT assign roles or enforce consistency across columns.

    Now infer the most accurate semantics and return ONLY valid JSON.
    """
    return prompt.strip()

build_vsi_prompt(entity_name, attr_name, attr_properties, context=None)

VSI (Value Semantics Inference) — deterministic, bounded. Uses stats + sample values + bounded semantic types.

Source code in blue/metadata.py
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
    def build_vsi_prompt(self, entity_name, attr_name, attr_properties, context=None):
        """
        VSI (Value Semantics Inference) — deterministic, bounded.
        Uses stats + sample values + bounded semantic types.
        """

        stats = attr_properties.get("stats", {}) or {}
        sample_values = stats.get("sample_values", [])[:10]
        if not sample_values:
            sample_values = ["<NO SAMPLE VALUES AVAILABLE>"]

        attr_type = attr_properties.get("info", {}).get("type", "unknown")

        stats_json = json.dumps(stats, indent=2)
        samples_json = json.dumps(sample_values, indent=2)
        allowed_json = json.dumps(self.VALUE_SEMANTIC_TYPES, indent=2)
        context_json = json.dumps(context or {}, indent=2)


        schema_json = """{
  "value_semantics": {
    "primary_type": "<ONE_OF_ALLOWED_TYPES>",
    "secondary_types": [],
    "is_categorical": false,
    "is_identifier": false,
    "is_free_text": false,
    "numeric": {
      "is_numeric": false,
      "is_continuous": false,
      "is_discrete": false,
      "min": null,
      "max": null
    },
    "temporal": {
      "is_date": false,
      "is_datetime": false,
      "is_duration": false,
      "granularity": null
    },
    "normalization": {
      "can_normalize": false,
      "normalized_examples": [],
      "normalization_strategy": null
    },
    "confidence": 0.0,
    "examples_used": [],
    "notes": []
  }
}"""

        prompt = f"""
You are performing Value Semantics Inference (VSI), a deterministic and bounded semantic classification
module used by autonomous agents. You MUST infer WHAT THE VALUES *ARE*, not what they represent in the domain.

Rules:
- Use ONLY value patterns + statistics.
- You MAY use other attributes ONLY for structural AND distributional disambiguation
  (e.g., distinguishing durations vs counts, identifiers vs categories,
   event times vs boundaries).
- You MUST NOT infer business, policy, or domain concepts from context.
- Cross-attribute context is evidence, not ground truth.
- Use ONLY allowed semantic types.
- You MUST be deterministic, safe, and predictable.
- You MUST produce machine-usable semantics.
- DO NOT invent new types. Stay within allowed types.
- DO NOT infer business/domain meanings.

───────────────────────────────
ATTRIBUTE CONTEXT
───────────────────────────────
Entity: {entity_name}
Attribute: {attr_name}
Declared Type: {attr_type}

ATTRIBUTE_STATS:
{stats_json}

SAMPLE_VALUES:
{samples_json}

OTHER_ATTRIBUTES_IN_ENTITY (contextual structural hints only):
{context_json}


ALLOWED_SEMANTIC_TYPES:
{allowed_json}

───────────────────────────────
OUTPUT FORMAT (STRICT JSON ONLY)
───────────────────────────────

{schema_json}

Return ONLY this JSON structure, filled in appropriately.
"""
        return prompt.strip()

collect_source_database_collection_metadata(data_registry, source, database, collection, recursive=False, rebuild=False)

Collect and enrich metadata for a specific collection and its entities within a database.

For each entity in the collection, generates enriched table and attribute descriptions using the LLM-based enrichment process, and stores them in the data registry if missing. Optionally, also generates a collection-level description.

Parameters:

Name Type Description Default
data_registry DataRegistry

Registry instance for accessing and storing metadata.

required
source str

Identifier of the data source.

required
database str

Name of the database containing the collection.

required
collection str

Name of the collection to process.

required
recursive bool

Whether to process nested collections or entities. Defaults to False.

False
rebuild bool

Whether to regenerate existing descriptions. Defaults to False.

False

Returns:

Type Description

None

Source code in blue/metadata.py
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
def collect_source_database_collection_metadata(self, data_registry, source, database, collection, recursive=False, rebuild=False):
    """
    Collect and enrich metadata for a specific collection and its entities within a database.

    For each entity in the collection, generates enriched table and attribute descriptions
    using the LLM-based enrichment process, and stores them in the data registry if missing.
    Optionally, also generates a collection-level description.

    Parameters:
        data_registry (DataRegistry): Registry instance for accessing and storing metadata.
        source (str): Identifier of the data source.
        database (str): Name of the database containing the collection.
        collection (str): Name of the collection to process.
        recursive (bool, optional): Whether to process nested collections or entities. Defaults to False.
        rebuild (bool, optional): Whether to regenerate existing descriptions. Defaults to False.

    Returns:
        None
    """

    entities = data_registry.get_source_database_collection_entities(source, database, collection)

    entity_descriptions = {}
    for entity in entities:
        entity_name = entity.get("name")

        attributes = data_registry.get_source_database_collection_entity_attributes(source, database, collection, entity_name)

        entity_stats = entity.get("properties", {}).get("stats", {})
        row_samples = entity_stats.get("row_samples", [])

        self.current_entity_row_samples = row_samples

        entity_attribute_description = self.enrich_entity(entity, attributes)

        try:
            parsed = json_utils.safe_json_parse(entity_attribute_description)
            if not parsed:
                logging.warning(f"Entity {entity} returned invalid or empty JSON.")
                continue
        except json.JSONDecodeError:
            logging.warning("LLM did not return valid JSON. Skipping entity enrichment.")
            parsed = {}

        table_desc = parsed.get("table_description", "")
        attribute_descs = parsed.get("attributes", {})
        entity_descriptions[entity_name] = table_desc

        if self.properties.get('enable_entity_description_generation', True):
            current_description = data_registry.get_source_database_collection_entity_description(source, database, collection, entity_name)

            if not current_description or current_description.strip() == "":
                data_registry.set_source_database_collection_entity_description(source, database, collection, entity_name, table_desc, rebuild=rebuild)

        if self.properties.get('enable_attribute_description_generation', True):
            for attr, desc in attribute_descs.items():
                current_description = data_registry.get_source_database_collection_entity_attribute_description(source, database, collection, entity_name, attr)
                if not current_description or current_description.strip() == "":
                    data_registry.set_source_database_collection_entity_attribute_description(source, database, collection, entity_name, attr, desc, rebuild=rebuild)

        self.current_entity_attributes = attributes

        if self.properties.get("enable_value_semantics_inference", True):
            for attr_obj in attributes:
                attr_name = attr_obj.get("name")

                # Skip attributes without stats or samples
                attr_stats = attr_obj.get("properties", {}).get("stats", {})
                if not attr_stats:
                    continue

                # Check existing semantics
                existing = data_registry.get_source_database_collection_entity_attribute_property(
                    source, database, collection, entity_name, attr_name, "value_semantics"
                )

                # Infer only if missing or rebuild=True
                if existing and not rebuild:
                    continue

                inferred = self.infer_attribute_vsi(entity_name, attr_obj)

                if inferred:
                    data_registry.set_source_database_collection_entity_attribute_property(
                        source,
                        database,
                        collection,
                        entity_name,
                        attr_name,
                        "value_semantics",
                        inferred,
                        rebuild=rebuild
                    )

                sdi = self.infer_attribute_sdi(entity_name, attr_obj)
                if sdi:
                    data_registry.set_source_database_collection_entity_attribute_property(
                        source, database, collection, entity_name, attr_name,
                        "semantic_discovery", sdi, rebuild=rebuild
                    )


            ### refresh attributes after value semantics inference
            attributes = data_registry.get_source_database_collection_entity_attributes(
                source, database, collection, entity_name
            )

        if self.properties.get("enable_domain_concept_mapping", True):
            for attr_obj in attributes:
                attr_name = attr_obj.get("name")

                # Skip attributes without value semantics
                attr_properties = attr_obj.get("properties", {})
                if "value_semantics" not in attr_properties:
                    continue

                existing = data_registry.get_source_database_collection_entity_attribute_property(
                    source, database, collection, entity_name, attr_name, "domain_concept"
                )

                if existing and not rebuild:
                    continue

                inferred = self.infer_domain_concept(entity_name, attr_obj)
                if inferred:
                    data_registry.set_source_database_collection_entity_attribute_property(
                        source,
                        database,
                        collection,
                        entity_name,
                        attr_name,
                        "domain_concept",
                        inferred,
                        rebuild=rebuild
                    )

        if self.properties.get("enable_interpretive_semantics", True):
            for attr in attributes:
                ivs = self.infer_interpretive_semantics(entity_name, attr)
                if ivs:
                    data_registry.set_source_database_collection_entity_attribute_property(
                        source,
                        database,
                        collection,
                        entity_name,
                        attr["name"],
                        "interpretive_semantics",
                        ivs,
                        rebuild=rebuild
                    )

                    logging.info(
                        f"[IVS] Inferred interpretive semantics for "
                        f"{entity_name}.{attr['name']}: {ivs}"
                    )

        # Refresh attributes after IVS
        attributes = data_registry.get_source_database_collection_entity_attributes(
            source, database, collection, entity_name
        )

        if self.properties.get("enable_value_axis_inference", True):
            axes = self.infer_value_axis(
                attributes,
                self.current_entity_row_samples
            )

            for attr_name, axis in axes.items():
                data_registry.set_source_database_collection_entity_attribute_property(
                    source, database, collection, entity_name,
                    attr_name, "value_axis", axis, rebuild=rebuild
                )

        if self.properties.get("enable_semantic_links_inference", True):
            # -------------------------------------------------
            # SEMANTIC LINK DISCOVERY (STRUCTURAL + SOFT LLM)
            # -------------------------------------------------
            semantic_links = self.infer_semantic_links(attributes)

            row_links = self.infer_row_based_links(
                attributes,
                self.current_entity_row_samples
            )

            # TAG row-grounded links
            for l in row_links:
                l["row_support"] = True

            semantic_links = semantic_links + row_links

            validated_links = []

            for link in semantic_links:
                try:
                    src = next(a for a in attributes if a["name"] == link["source"])
                    tgt = next(a for a in attributes if a["name"] == link["target"])
                except StopIteration:
                    continue

                result = self.validate_semantic_link(src, tgt, link["signal"])

                relationship = result.get("relationship") if result else None

                signal_type = (
                    link["signal"].get("type")
                    if isinstance(link["signal"], dict)
                    else link["signal"]
                )

                if relationship == "UNRELATED" and signal_type in {
                    "grouped_distribution",
                    "functional_dependency",
                    "temporal_alignment",
                }:
                    relationship = {
                        "grouped_distribution": "SEGMENTS",
                        "functional_dependency": "DERIVES",
                        "temporal_alignment": "SUPPORTS",
                    }[signal_type]


                # -------------------------------------------------
                # FINALIZE LINK
                # -------------------------------------------------
                if relationship:
                    if link.get("row_support"):
                        # Row-grounded links dominate LLM disagreement
                        confidence = max(
                            0.7,
                            result.get("confidence", 0.7) if result else 0.7
                        )
                    else:
                        confidence = (
                            result.get("confidence", 0.55)
                            if result and relationship == result.get("relationship")
                            else 0.55
                        )

                    validated_links.append({
                        "source": link["source"],
                        "target": link["target"],
                        "relationship": relationship,
                        "confidence": confidence,
                        "rationale": (
                            result.get("rationale")
                            if result else "Structure-backed semantic signal"
                        ),
                    })

            # Persist links
            if validated_links:
                data_registry.set_source_database_collection_entity_property(
                    source,
                    database,
                    collection,
                    entity_name,
                    "semantic_links",
                    validated_links,
                    rebuild=rebuild
                )

            # -------------------------------------------------
            # CONDITIONAL VALUE DISTRIBUTIONS 
            # -------------------------------------------------
            # Only infer conditional distributions for validated SEGMENTS links
            if validated_links and self.current_entity_row_samples:

                for link in validated_links:
                    if link.get("relationship") != "SEGMENTS":
                        continue

                    try:
                        group_attr = next(
                            a for a in attributes if a["name"] == link["source"]
                        )
                        value_attr = next(
                            a for a in attributes if a["name"] == link["target"]
                        )

                    except StopIteration:
                        continue

                    group_role = (
                            group_attr
                            .get("properties", {})
                            .get("semantic_role", {})
                        )

                    if group_role.get("primary_role") != "SEGMENTATION_DRIVER":
                        continue


                    group_vsi = group_attr.get("properties", {}).get("value_semantics", {})
                    value_vsi = value_attr.get("properties", {}).get("value_semantics", {})

                    # Hard safety gate
                    if value_vsi.get("semantic_type") not in {
                        "CURRENCY_AMOUNT",
                        "INTEGER",
                        "FLOAT",
                        "NUMERIC_GENERAL",
                        "PERCENTAGE",
                        "RATIO",
                        "DURATION"
                    }:
                        continue

                    cds = self.infer_conditional_distributions(
                        group_attr,
                        value_attr,
                        self.current_entity_row_samples
                    )

                    if not cds:
                        continue

                    logging.info(
                        f"[MetaData] Inferred conditional distributions: "
                        f"{entity_name}.{value_attr['name']} | grouped by {group_attr['name']}"
                    )

                    existing = data_registry.get_source_database_collection_entity_attribute_property(
                        source, database, collection, entity_name,
                        value_attr["name"], "conditional_value_distribution"
                        ) or {}

                    existing[group_attr["name"]] = cds

                    data_registry.set_source_database_collection_entity_attribute_property(
                        source,
                        database,
                        collection,
                        entity_name,
                        value_attr["name"],
                        "conditional_value_distribution",
                        existing,
                        rebuild=rebuild
                    )


            # -------------------------------------------------
            # SEMANTIC ROLE INFERENCE (LINK-DRIVEN)
            # -------------------------------------------------
            for attr in attributes:
                attr_name = attr["name"]

                role = self.infer_semantic_role(
                    attr,
                    attributes,
                    semantic_links=(validated_links or semantic_links)
                )

                if role:
                    data_registry.set_source_database_collection_entity_attribute_property(
                        source, database, collection, entity_name,
                        attr_name, "semantic_role", role, rebuild=rebuild
                    )

    if self.properties.get('enable_collection_description_generation', True):
        current_description = data_registry.get_source_database_collection_description(source, database, collection)
        if not current_description or current_description.strip() == "":

            collection_metadata = data_registry.get_source_database_collection_property(source, database, collection, "metadata")

            if not collection_metadata:
                collection_metadata = {"name": collection, "type": "collection"}

            collection_desc = self.enrich_collection_description(collection, entity_descriptions, collection_metadata)

            data_registry.set_source_database_collection_description(source, database, collection, collection_desc, rebuild=rebuild)

collect_source_database_metadata(data_registry, source, database, recursive=False, rebuild=False)

Collect and enrich metadata for a database within a data source.

This method checks whether the database already has a description. If not, it uses available metadata and collection descriptions to generate an enriched description (via enrich_database_description) and stores it back into the data registry. Optionally, it can also recurse into collections to collect their metadata.

Parameters:

Name Type Description Default
data_registry DataRegistry

The registry object that manages sources, databases, collections, and metadata.

required
source str

Identifier for the data source.

required
database str

Name of the database to collect metadata for.

required
recursive bool

If True, also collect metadata for all collections within the database. Defaults to False.

False
rebuild bool

If True, forces metadata to be rebuilt or refreshed even if it already exists. Defaults to False.

False

Returns:

Type Description

None

Source code in blue/metadata.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
def collect_source_database_metadata(self, data_registry, source, database, recursive=False, rebuild=False):
    """
    Collect and enrich metadata for a database within a data source.

    This method checks whether the database already has a description. If not,
    it uses available metadata and collection descriptions to generate an
    enriched description (via `enrich_database_description`) and stores it
    back into the data registry. Optionally, it can also recurse into
    collections to collect their metadata.

    Parameters:
        data_registry (DataRegistry): The registry object that manages sources,
            databases, collections, and metadata.
        source (str): Identifier for the data source.
        database (str): Name of the database to collect metadata for.
        recursive (bool, optional): If True, also collect metadata for all
            collections within the database. Defaults to False.
        rebuild (bool, optional): If True, forces metadata to be rebuilt or
            refreshed even if it already exists. Defaults to False.

    Returns:
        None

    """
    collections = data_registry.get_source_database_collections(source, database)
    collection_descriptions = {}

    if recursive:
        for collection in collections:
            collection_name = collection.get("name")
            self.collect_source_database_collection_metadata(
                data_registry,
                source,
                database,
                collection_name,
                recursive=True,
                rebuild=rebuild
            )


    if self.properties.get('enable_database_description_generation', True):
        current_description = data_registry.get_source_database_description(source, database)
        if rebuild or not current_description or current_description.strip() == "":
            database_metadata = data_registry.get_source_database_property(source, database, "metadata")

            if not database_metadata:
                database_metadata = {"name": database, "type": "database"}

            for collection in collections:
                collection_name = collection.get("name")
                collection_desc = collection.get("description")
                collection_descriptions[collection_name] = collection_desc

            database_desc = self.enrich_database_description(database, collection_descriptions, database_metadata)

            data_registry.set_source_database_description(source, database, database_desc, rebuild=rebuild)

    return

collect_source_metadata(data_registry, source, recursive=False, rebuild=False)

Collect and optionally recursively enrich metadata for a data source.

If recursive is True, iterates through all databases under the source and collects/enriches their metadata.

Parameters:

Name Type Description Default
data_registry DataRegistry

Registry instance for metadata access/storage.

required
source str

Identifier of the data source.

required
recursive bool

Whether to include child databases. Defaults to False.

False
rebuild bool

Whether to regenerate existing descriptions. Defaults to False.

False

Returns:

Type Description

None

Source code in blue/metadata.py
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def collect_source_metadata(self, data_registry, source, recursive=False, rebuild=False):
    """
    Collect and optionally recursively enrich metadata for a data source.

    If recursive is True, iterates through all databases under the source
    and collects/enriches their metadata.

    Parameters:
        data_registry (DataRegistry): Registry instance for metadata access/storage.
        source (str): Identifier of the data source.
        recursive (bool, optional): Whether to include child databases. Defaults to False.
        rebuild (bool, optional): Whether to regenerate existing descriptions. Defaults to False.

    Returns:
        None
    """
    if recursive:
        databases = data_registry.get_source_databases(source)
        for database in databases:
            self.collect_source_database_metadata(data_registry, source, database, recursive=recursive, rebuild=rebuild)
    return

enrich_collection_description(collection_name, entity_descriptions, collection_metadata)

Enrich a collection description using LLM.

Builds a prompt from the provided entity descriptions and metadata, then executes an LLM call to generate or refine the collection-level description.

Parameters:

Name Type Description Default
collection_name str

The name of the collection.

required
entity_descriptions dict

Mapping of entity names to their descriptions.

required
collection_metadata dict or str

Additional metadata for the collection.

required

Returns:

Name Type Description
Any

The enriched collection description, as returned by the LLM.

Source code in blue/metadata.py
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
def enrich_collection_description(self, collection_name, entity_descriptions, collection_metadata):
    """
    Enrich a collection description using LLM.

    Builds a prompt from the provided entity descriptions and metadata, then
    executes an LLM call to generate or refine the collection-level description.

    Parameters:
        collection_name (str): The name of the collection.
        entity_descriptions (dict): Mapping of entity names to their descriptions.
        collection_metadata (dict or str): Additional metadata for the collection.

    Returns:
        Any: The enriched collection description, as returned by the LLM.
    """
    prompt = self.build_collection_description_prompt(collection_name, entity_descriptions, collection_metadata)
    return self.execute_api_call(prompt, properties=self.properties, additional_data={})

enrich_database_description(database_name, collection_descriptions, database_metadata)

Enrich a database description using LLM.

Builds a prompt from the provided collection descriptions and metadata, then executes an API call to generate or refine the database-level description.

Parameters:

Name Type Description Default
database_name str

The name of the database.

required
collection_descriptions dict

Mapping of collection names to their descriptions.

required
database_metadata dict or str

Additional metadata for the database.

required

Returns:

Name Type Description
Any

The enriched database description.

Source code in blue/metadata.py
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
def enrich_database_description(self, database_name, collection_descriptions, database_metadata):
    """
    Enrich a database description using LLM.

    Builds a prompt from the provided collection descriptions and metadata, then
    executes an API call to generate or refine the database-level description.

    Parameters:
        database_name (str): The name of the database.
        collection_descriptions (dict): Mapping of collection names to their descriptions.
        database_metadata (dict or str): Additional metadata for the database.

    Returns:
        Any: The enriched database description.
    """

    prompt = self.build_database_description_prompt(database_name, collection_descriptions, database_metadata)
    return self.execute_api_call(prompt, properties=self.properties, additional_data={})

enrich_entity(entity, attributes)

Generate an enriched description for an entity using its attributes.

Builds a prompt from the entity and its attributes, then calls the external LLM API to produce the enriched description.

Parameters:

Name Type Description Default
entity dict

The entity metadata to enrich.

required
attributes dict

Attribute data associated with the entity.

required

Returns:

Name Type Description
str

Enriched description text generated by the API.

Source code in blue/metadata.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
def enrich_entity(self, entity, attributes):
    """
    Generate an enriched description for an entity using its attributes.

    Builds a prompt from the entity and its attributes, then calls the
    external LLM API to produce the enriched description.

    Parameters:
        entity (dict): The entity metadata to enrich.
        attributes (dict): Attribute data associated with the entity.

    Returns:
        str: Enriched description text generated by the API.
    """
    entity_prompt = self.build_entity_description_prompt(entity, attributes)
    return self.execute_api_call(entity_prompt, properties=self.properties, additional_data={})

functional_dependency_signal(a, b)

Detect potential functional / monotonic dependency between two attributes.

Source code in blue/metadata.py
183
184
185
186
187
188
189
190
191
192
193
194
def functional_dependency_signal(self, a, b):
    """
    Detect potential functional / monotonic dependency between two attributes.
    """
    a_vals = self.get_numeric_samples(a)
    b_vals = self.get_numeric_samples(b)

    if len(a_vals) < 5 or len(b_vals) < 5:
        return False

    corr = abs(self.safe_pearson(a_vals, b_vals))
    return corr > 0.7

grouped_distribution_signal(group_attr, value_attr)

Detect whether grouping by group_attr produces meaningfully different distributions of value_attr.

This version is calibrated for small sample_values and enables SEGMENTATION_DRIVER to emerge without hallucination.

Source code in blue/metadata.py
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
def grouped_distribution_signal(self, group_attr, value_attr):
    """
    Detect whether grouping by group_attr produces
    meaningfully different distributions of value_attr.

    This version is calibrated for small sample_values and
    enables SEGMENTATION_DRIVER to emerge without hallucination.
    """

    g_vals = group_attr.get("properties", {}).get("stats", {}).get("sample_values", [])
    v_vals = value_attr.get("properties", {}).get("stats", {}).get("sample_values", [])

    if not g_vals or not v_vals:
        return False

    try:
        groups = {}

        for g, v in zip(g_vals, v_vals):
            try:
                v = float(v)
            except Exception:
                continue
            groups.setdefault(g, []).append(v)

        # Require enough groups and per-group support
        min_groups = 2   # instead of 3
        if len(groups) < min_groups:
            return False

        supported_groups = [vals for vals in groups.values() if len(vals) >= 2]
        if len(supported_groups) < 2:
            return False

        means = [sum(vals) / len(vals) for vals in groups.values()]

        max_mean = max(abs(m) for m in means) if means else 0.0
        if max_mean == 0:
            return False

        spread = max(means) - min(means)
        avg_mean = sum(abs(m) for m in means) / len(means)
        relative_spread = spread / avg_mean if avg_mean > 0 else 0.0

        # Two-tier acceptance:
        # 1. Absolute spread (classic signal)
        # 2. Relative spread (robust for small/noisy samples)
        return (
            spread > 0.12 * max_mean
            or relative_spread > 0.18
        )

    except Exception:
        return False

grouped_distribution_signal_row_aligned(group_attr, value_attr, row_samples)

Row-grounded version of grouped_distribution_signal. Uses real co-occurrence, not column samples.

Source code in blue/metadata.py
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
def grouped_distribution_signal_row_aligned(self, group_attr, value_attr,row_samples):
    """
    Row-grounded version of grouped_distribution_signal.
    Uses real co-occurrence, not column samples.
    """

    g_name = group_attr["name"]
    v_name = value_attr["name"]

    g_vals = []
    v_vals = []

    for row in row_samples:
        if g_name not in row or v_name not in row:
            continue

        g = row.get(g_name)
        v = row.get(v_name)

        if g is None or v is None:
            continue

        try:
            v = float(v)
        except Exception:
            continue

        g_vals.append(g)
        v_vals.append(v)

    if len(g_vals) < 5:
        return False

    return self.grouped_distribution_signal(
        {"properties": {"stats": {"sample_values": g_vals}}},
        {"properties": {"stats": {"sample_values": v_vals}}},
    )

has_row_cooccurrence(a, b, row_samples, min_rows=5)

Check if two attributes co-occur in at least min_rows sample rows.

Source code in blue/metadata.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def has_row_cooccurrence(self, a, b, row_samples, min_rows=5):
    """
    Check if two attributes co-occur in at least min_rows sample rows.
    """

    a_name = a["name"]
    b_name = b["name"]

    count = 0
    for row in row_samples:
        if row.get(a_name) is not None and row.get(b_name) is not None:
            count += 1
            if count >= min_rows:
                return True
    return False

infer_attribute_sdi(entity_name, attr)

Infer semantic discovery information for an attribute.

Source code in blue/metadata.py
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
def infer_attribute_sdi(self, entity_name, attr):
    """
    Infer semantic discovery information for an attribute.
    """
    attr_name = attr.get("name")
    props = attr.get("properties", {})

    context = self.build_attribute_context(
        self.current_entity_attributes, attr_name
    )

    prompt = self.build_sdi_prompt(entity_name, attr_name, props, context=context)
    llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

    try:
        parsed = json_utils.safe_json_parse(llm_output)
        return parsed
    except Exception:
        logging.warning(f"Invalid SDI JSON for {entity_name}.{attr_name}")
        return None

infer_attribute_value_semantics(entity_name, attr)

Infer value semantics for an attribute.

Source code in blue/metadata.py
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
def infer_attribute_value_semantics(self, entity_name, attr):
    """
    Infer value semantics for an attribute.
    """
    attr_name = attr.get("name")
    attr_properties = attr.get("properties", {})

    # Build prompt
    prompt = self.build_value_semantics_prompt(entity_name, attr_name, attr_properties)

    # Call LLM
    llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

    # Parse LLM output
    try:
        semantics = json_utils.safe_json_parse(llm_output)
        if not semantics:
            logging.warning(f"Value semantics inference returned empty for {entity_name}.{attr_name}")
            return None
        return semantics
    except Exception:
        logging.warning(f"Invalid JSON from value semantics inference for {entity_name}.{attr_name}")
        return None

infer_attribute_vsi(entity_name, attr)

Infer value semantics for an attribute.

Source code in blue/metadata.py
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
def infer_attribute_vsi(self, entity_name, attr):
    """
    Infer value semantics for an attribute."""

    attr_name = attr.get("name")
    props = attr.get("properties", {})

    context = self.build_attribute_context(
        self.current_entity_attributes, attr_name
    )

    prompt = self.build_vsi_prompt(entity_name, attr_name, props, context=context)
    llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

    try:
        parsed = json_utils.safe_json_parse(llm_output)
        if not parsed:
            return None

        # unwrap
        if "value_semantics" in parsed:
            vs = parsed["value_semantics"]
        else:
            vs = parsed

        # ensure compatibility with old DCM (“semantic_type”)
        if "primary_type" in vs:
            vs["semantic_type"] = vs.get("primary_type")

        return vs

    except Exception:
        logging.warning(f"Invalid VSI JSON for {entity_name}.{attr_name}")
        return None

infer_conditional_distributions(group_attr, value_attr, row_samples)

Discover conditional distribution semantics: P(value | group)

Source code in blue/metadata.py
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
def infer_conditional_distributions(self, group_attr, value_attr, row_samples):
    """
    Discover conditional distribution semantics:
    P(value | group)
    """

    g_name = group_attr["name"]
    v_name = value_attr["name"]

    groups = {}

    for row in row_samples:
        g = row.get(g_name)
        v = row.get(v_name)

        if g is None or v is None:
            continue

        try:
            v = float(v)
        except Exception:
            continue

        groups.setdefault(g, []).append(v)

    profiles = {}
    for g, vals in groups.items():
        if len(vals) < 10:
            continue

        p50 = np.percentile(vals, 50)
        p90 = np.percentile(vals, 90)
        p99 = np.percentile(vals, 99)

        profiles[str(g)] = {   
            "count": len(vals),
            "p50": round(p50, 2),
            "p90": round(p90, 2),
            "p99": round(p99, 2),
            "typical_range": [round(np.min(vals), 2), round(p90, 2)],
            "extreme_range": [round(p90, 2), round(np.max(vals), 2)],
        }


    if len(profiles) < 2:
        return None

    MAX_GROUPS = 10
    if len(profiles) > MAX_GROUPS:
        return None

    return {
        "group_attribute": g_name,
        "value_attribute": v_name,
        "profiles": profiles,
        "recommended_stat": "p99",
        "confidence": 0.7,
        "rationale": "Conditional distributions inferred from row-aligned numeric behavior"
    }

infer_domain_concept(entity_name, attr)

Infer a domain concept for an attribute.

Source code in blue/metadata.py
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
def infer_domain_concept(self, entity_name, attr):
    """
    Infer a domain concept for an attribute.
    """
    attr_name = attr.get("name")
    attr_properties = attr.get("properties", {})

    # Skip if no value semantics yet
    if "value_semantics" not in attr_properties:
        return None

    prompt = self.build_domain_concept_prompt(entity_name, attr_name, attr_properties)

    llm_output = self.execute_api_call(prompt, properties=self.properties, additional_data={})

    try:
        concept = json_utils.safe_json_parse(llm_output)
        if not concept:
            logging.warning(f"Domain concept inference empty for {entity_name}.{attr_name}")
            return None
        return concept
    except Exception:
        logging.warning(f"Invalid JSON from domain concept inference for {entity_name}.{attr_name}")
        return None

infer_interpretive_semantics(entity_name, attr)

Infer interpretive semantics for an attribute. Returns interpretive semantics dict or None.

Source code in blue/metadata.py
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
def infer_interpretive_semantics(self, entity_name, attr):
    """
    Infer interpretive semantics for an attribute.
    Returns interpretive semantics dict or None.
    """
    attr_name = attr["name"]
    props = attr.get("properties", {})

    # Hard gate: numeric & identifiers NEVER qualify
    vsi = props.get("value_semantics", {})
    if vsi.get("is_identifier"):
        return None

    semantic_type = vsi.get("semantic_type")
    if semantic_type not in ("ENUM_CATEGORY", "TEXT_CATEGORY"):
        return None

    prompt = self.build_interpretive_semantics_prompt(
        entity_name, attr_name, props
    )

    out = self.execute_api_call(
        prompt,
        properties=self.properties,
        additional_data={}
    )

    try:
        parsed = json_utils.safe_json_parse(out)

        if not parsed:
            return None

        # Strong safety filter
        if parsed.get("interpretation_type") == "NONE":
            return None

        if parsed.get("confidence", 0) < 0.6:
            return None

        return parsed

    except Exception:
        logging.warning(
            f"[IVS] Invalid interpretive semantics for {entity_name}.{attr_name}"
        )
        return None

infer_numeric_value_axis(attr, row_samples)

Infer magnitude-based axis for numeric attributes

Source code in blue/metadata.py
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
def infer_numeric_value_axis(self, attr, row_samples):
    """
    Infer magnitude-based axis for numeric attributes
    """

    name = attr["name"]
    values = []

    for row in row_samples:
        try:
            values.append(float(row.get(name)))
        except Exception:
            pass

    if len(values) < 20:
        return None

    # semantic gate
    if self.is_sparse_count_axis(attr, values):
        threshold = self.infer_tail_threshold(values)
        if threshold is None:
            threshold = 1

        return {
            "axis_type": "DISCRETE_COUNT",
            "polarity": {
                "direction": "increasing",
                "meaning": "severity"
            },
            "extreme_regions": {
                "large": f">= {threshold}"
            },
            "confidence": 0.85,
            "rationale": (
                "Sparse, zero-inflated discrete counts detected; "
                "extremes inferred via tail-regime semantics instead of percentiles"
            )
        }


    p10 = np.percentile(values, 10)
    p90 = np.percentile(values, 90)

    return {
        "axis_type": "CONTINUOUS",
        "polarity": {
            "direction": "increasing",
            "meaning": "magnitude"
        },
        "extreme_regions": {
            "small": f"< {round(p10, 2)}",
            "large": f"> {round(p90, 2)}"
        },
        "confidence": 0.7,
        "rationale": "Distributional extremes inferred via quantiles"
    }

Infer semantic links using row-grounded evidence. Returns a list of {source, target, signal}.

Source code in blue/metadata.py
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
def infer_row_based_links(self, attributes, row_samples):
    """
    Infer semantic links using row-grounded evidence.
     Returns a list of {source, target, signal}.
     """

    links = []

    if not row_samples:
        return links

    for a in attributes:
        for b in attributes:
            if a["name"] == b["name"]:
                continue

            a_type = a.get("properties", {}).get("value_semantics", {}).get("semantic_type")
            b_type = b.get("properties", {}).get("value_semantics", {}).get("semantic_type")

            # ----------------------------------
            # SEGMENTATION (row-grounded)
            # ----------------------------------
            if self.grouped_distribution_signal_row_aligned(a, b, row_samples):
                links.append({
                    "source": a["name"],
                    "target": b["name"],
                    "signal": "grouped_distribution",
                    "row_support": True
                })


            # TEMPORAL ALIGNMENT (row-grounded, generic)
            if (
            a_type in ("DATE", "DATETIME", "TIME", "DURATION")
            and b_type not in ("DATE", "DATETIME", "TIME", "DURATION")
            and self.has_row_cooccurrence(a, b, row_samples)
            ):
                links.append({
                    "source": a["name"],
                    "target": b["name"],
                    "signal": {
                        "type": "temporal_alignment",
                        "strength": 0.6,
                        "row_support": True
                    }
                })

    return links

Find candidate semantic relationships between attributes. Returns a list of {source, target, signal}.

Source code in blue/metadata.py
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
def infer_semantic_links(self, attributes):
    """
    Find candidate semantic relationships between attributes.
    Returns a list of {source, target, signal}.
    """
    links = []

    for a in attributes:
        for b in attributes:
            if a["name"] == b["name"]:
                continue

            # SEGMENTATION (distributional, data-driven)
            if self.grouped_distribution_signal(a, b):
                links.append({
                    "source": a["name"],
                    "target": b["name"],
                    "signal": "grouped_distribution",
                    "row_support": False
                })

            # ----------------------------------
            # FUNCTIONAL DEPENDENCY (numeric)
            # ----------------------------------
            if self.functional_dependency_signal(a, b):
                links.append({
                    "source": a["name"],
                    "target": b["name"],
                    "signal": {
                        "type": "functional_dependency",
                        "strength": 0.7
                    }
                })

    return links

infer_semantic_role(target_attr, all_attrs, semantic_links=None)

Infer the semantic role of a target attribute based on its relationships.

Source code in blue/metadata.py
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
def infer_semantic_role(self, target_attr, all_attrs, semantic_links=None):
    """
    Infer the semantic role of a target attribute based on its relationships.
    """

    semantic_links = semantic_links or []

    attr_name = target_attr["name"]
    vsi = target_attr.get("properties", {}).get("value_semantics", {})
    primary_type = vsi.get("semantic_type", "UNKNOWN")

    # -------------------------------------------------
    # 1. IDENTIFIER (hard rule)
    # -------------------------------------------------
    if vsi.get("is_identifier"):
        return {
            "primary_role": "IDENTIFIER",
            "confidence": 0.95,
            "role_arguments": {},
            "justification": {
                "rule": "identifier_flag"
            }
        }

    outgoing = [l for l in semantic_links if l["source"] == attr_name]
    incoming = [l for l in semantic_links if l["target"] == attr_name]

    # -------------------------------------------------
    # 2. SEGMENTATION DRIVER
    # -------------------------------------------------
    segment_links = []
    for l in outgoing:
        if l["relationship"] != "SEGMENTS":
            continue

        target = next(
            (a for a in all_attrs if a["name"] == l["target"]),
            None
        )
        if not target:
            continue

        target_vsi = target.get("properties", {}).get("value_semantics", {})
        if target_vsi.get("is_identifier"):
            continue  

        segment_links.append(l)

    if segment_links:
        return {
            "primary_role": "SEGMENTATION_DRIVER",
            "confidence": min(
                0.95,
                sum(l.get("confidence", 0.7) for l in segment_links) / len(segment_links)
            ),
            "role_arguments": {
                "segments": [l["target"] for l in segment_links]
            },
            "justification": {
                "signals": ["SEGMENTS"],
                "link_count": len(segment_links)
            }
        }

    # -------------------------------------------------
    # RELATIONSHIP-DRIVEN FALLBACK (GENERIC)
    # -------------------------------------------------
    supports = [l for l in outgoing if l["relationship"] == "SUPPORTS"]

    if supports:
        return {
            "primary_role": "EVIDENCE",
            "confidence": min(
                0.9,
                sum(l.get("confidence", 0.7) for l in supports) / len(supports)
            ),
            "role_arguments": {
                "supports": [l["target"] for l in supports]
            },
            "justification": {
                "signals": ["SUPPORTS"]
            }
        }

    # -------------------------------------------------
    # DERIVED MEASURE
    # -------------------------------------------------
    #derive_links = [l for l in incoming if l["relationship"] == "DERIVES"]
    #if derive_links:
    #    return {
    #        "primary_role": "DERIVED_MEASURE",
    #        "confidence": min(
    #            0.95,
    #            sum(l.get("confidence", 0.7) for l in derive_links) / len(derive_links)
    #        ),
    #        "role_arguments": {
    #            "derived_from": [l["source"] for l in derive_links]
    #        },
    #        "justification": {
    #            "signals": ["DERIVES"],
    #            "link_count": len(derive_links)
    #        }
    #    }


    # -------------------------------------------------
    # WEAK TYPE PRIOR
    # -------------------------------------------------
    if primary_type in ("DATE", "DATETIME"):
        return {
            "primary_role": "EVENT_TIME",
            "confidence": 0.55,
            "role_arguments": {},
            "justification": {
                "type_prior": primary_type
            }
        }

    # -------------------------------------------------
    # FALLBACK
    # -------------------------------------------------
    return {
        "primary_role": "DESCRIPTIVE",
        "confidence": 0.4,
        "role_arguments": {},
        "justification": {
            "reason": "no causal or statistical signals detected"
        }
    }

infer_tail_threshold(values)

Given a numeric column that has already been classified as a sparse discrete count, infer a threshold that marks the beginning of the “extreme” tail. Automatically find a cutoff that separates: normal / common values from rare, extreme, or regime-changing values. This is classic long-tail / power-law count data behavior: many zeros, many small counts, very few large counts, but those large counts are semantically important (e.g., mass casualty events)

Source code in blue/metadata.py
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
def infer_tail_threshold(self, values):
    """
    Given a numeric column that has already been classified as a sparse discrete count, 
    infer a threshold that marks the beginning of the “extreme” tail.
    Automatically find a cutoff that separates:
    normal / common values from rare, extreme, or regime-changing values.
    This is classic long-tail / power-law count data behavior:
    many zeros, many small counts, very few large counts, but those large counts are semantically important (e.g., mass casualty events)
    """

    values = sorted(values)
    n = len(values)

    non_zero = [v for v in values if v > 0]
    if not non_zero:
        return None

    # ---- Case 1: binary-tail  ---------------
    ## “Presence vs absence is the meaningful distinction.”
    if max(non_zero) <= 3:
        return 1

    # ---- Case 2: scalable long-tail discrete counts ---------------------
    # Extreme tail detection
    # We search for the smallest interpretable count threshold t
    # such that only a very small fraction of rows (e.g., <1%)
    # reach or exceed t. This identifies a regime change where
    # values transition from common occurrences to rare, extreme events.
    # Unlike percentiles, this tail-frequency criterion remains stable
    # under heavy zero-inflation and preserves integer semantics.
    for t in [5, 10, 15, 20]:
        frac = sum(v >= t for v in values) / n
        if frac < 0.01:
            return t

    # Fallback: just take the extreme
    return max(values)

infer_value_axis(attributes, row_samples)

Infer value axes for attributes when supported. Returns dict: {attr_name: axis_metadata}

Source code in blue/metadata.py
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
def infer_value_axis(self, attributes, row_samples):
    """
    Infer value axes for attributes when supported.
    Returns dict: {attr_name: axis_metadata}
    """

    axes = {}

    # -------------------------------------------------
    # 1. NUMERIC MAGNITUDE AXES 
    # -------------------------------------------------
    for attr in attributes:
        if not self.is_value_axis_eligible(attr):
            continue

        semantic_type = (
            attr.get("properties", {})
                .get("value_semantics", {})
                .get("semantic_type")
        )

        if semantic_type in (
            "CURRENCY_AMOUNT",
            "INTEGER",
            "FLOAT",
            "NUMERIC_GENERAL",
            "PERCENTAGE",
            "RATIO",
            "DURATION"
        ):
            axis = self.infer_numeric_value_axis(attr, row_samples)
            if axis:
                axes[attr["name"]] = axis

    # -------------------------------------------------
    # 2. ORDINAL AXES FROM IVS 
    # -------------------------------------------------
    for attr in attributes:
        name = attr["name"]

        if name in axes:
            continue  

        if not self.is_ivs_promotable_to_value_axis(attr):
            continue

        #ivs = attr["properties"]["interpretive_semantics"]
        ivs = attr.get("properties", {}).get("interpretive_semantics")
        if not ivs:
            continue

        axis = {
            "axis_type": "ORDINAL",
            "ordering": ivs.get("ordering", []),
            "polarity": ivs.get("polarity", {}),
            "extreme_regions": {
                "low": ivs.get("ordering", [])[:1],
                "high": ivs.get("ordering", [])[-1:]
            },
            "confidence": ivs.get("confidence", 0.7),
            "rationale": (
                "Promoted from interpretive semantics "
                "after confidence and semantic eligibility checks"
            ),
            "source": "interpretive_semantics"
        }

        axes[name] = axis

        logging.info(
            f"[ValueAxis] Promoted IVS → ORDINAL axis for {name}: {axis}"
        )

    return axes

is_ivs_promotable_to_value_axis(attr)

Decide whether interpretive semantics may be promoted to a VALUE AXIS (ORDINAL).

This is a STRICT gate.

Source code in blue/metadata.py
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
def is_ivs_promotable_to_value_axis(self, attr):
    """
    Decide whether interpretive semantics may be promoted
    to a VALUE AXIS (ORDINAL).

    This is a STRICT gate.
    """

    props = attr.get("properties", {})
    ivs = props.get("interpretive_semantics", {})
    vsi = props.get("value_semantics", {})

    if not ivs:
        return False

    if ivs.get("interpretation_type") not in ("ORDINAL", "TIER", "SEVERITY"):
        return False

    if ivs.get("confidence", 0.0) < 0.7:
        return False

    # Identifiers never qualify
    if vsi.get("is_identifier"):
        return False

    # Only categorical value types may enter this path
    if vsi.get("semantic_type") not in (
        "ENUM_CATEGORY",
        "TEXT_CATEGORY",
        "LABEL"
    ):
        return False

    return True

is_sparse_count_axis(attr, values)

Detect sparse, zero-inflated, discrete count variables where percentile-based extremes are semantically invalid.

Source code in blue/metadata.py
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
def is_sparse_count_axis(self, attr, values):
    """
    Detect sparse, zero-inflated, discrete count variables
    where percentile-based extremes are semantically invalid.
    """

    if not values:
        return False

    vsi = attr.get("properties", {}).get("value_semantics", {})
    semantic_type = vsi.get("semantic_type")

    # ---- semantic gate ---------------------------------
    if semantic_type not in {"INTEGER", "NUMERIC_GENERAL"}:
        return False

    # ---- hard exclude spatial / continuous numerics ----
    name = attr.get("name", "").lower()
    if name in {"latitude", "longitude", "lat", "lon", "lng"}:
        return False

    numeric = vsi.get("numeric", {}) or {}
    if not numeric.get("is_discrete", True):
        return False

    # ---- distributional signals ------------------------
    n = len(values)
    zero_frac = sum(v == 0 for v in values) / n
    non_zero = [v for v in values if v > 0]

    #if not non_zero:
    #    return False

    max_val = max(values)
    distinct = len(set(values))

    # High zero mass + discrete support => sparse count
    if zero_frac >= 0.7 and distinct <= 10:
        return True

    if zero_frac >= 0.4 and max_val >= 5:
        return True

    return False

is_value_axis_eligible(attr)

Decide whether an attribute is eligible for value-axis inference. This is a SEMANTIC gate, not a statistical one.

Source code in blue/metadata.py
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
def is_value_axis_eligible(self, attr):
    """
    Decide whether an attribute is eligible for value-axis inference.
    This is a SEMANTIC gate, not a statistical one.
    """

    props = attr.get("properties", {})
    vsi = props.get("value_semantics", {}) or {}

    semantic_type = vsi.get("semantic_type", "UNKNOWN")

    # ---- HARD EXCLUSIONS ----------------------------------
    # Identifiers, opaque codes, labels, join keys
    if vsi.get("is_identifier"):
        return False

    if semantic_type in {
        "ID_STRING",
        "ID_NUMERIC",
        "UUID",
        "HASH",
        "CODE",
        "TAG",
        "LABEL",
        "ENUM_CATEGORY",   
        "TEXT_CATEGORY"
    }:
        return False

    # ---- ALLOWED NUMERIC AXES -----------------------------
    if semantic_type in {
        "CURRENCY_AMOUNT",
        "INTEGER",
        "FLOAT",
        "NUMERIC_GENERAL",
        "PERCENTAGE",
        "RATIO",
        "DURATION"
    }:
        return True

    return False

temporal_alignment_signal(a, b)

Detect weak temporal alignment without semantic commitment.

Source code in blue/metadata.py
196
197
198
199
200
201
202
203
204
def temporal_alignment_signal(self, a, b):
    """
    Detect weak temporal alignment without semantic commitment.
    """
    a_type = a.get("properties", {}).get("value_semantics", {}).get("semantic_type")
    b_type = b.get("properties", {}).get("value_semantics", {}).get("semantic_type")

    temporal_types = {"DATE", "DATETIME", "TIME", "DURATION"}
    return a_type in temporal_types and b_type not in temporal_types

Validate a semantic link between two attributes.

Source code in blue/metadata.py
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
def validate_semantic_link(self, source, target, signal):
    """
    Validate a semantic link between two attributes.
    """
    src_props = source.get("properties", {})
    tgt_props = target.get("properties", {})

    src_vs = src_props.get("value_semantics", {})
    tgt_vs = tgt_props.get("value_semantics", {})

    src_semantic_type = src_vs.get("semantic_type", "UNKNOWN")
    tgt_semantic_type = tgt_vs.get("semantic_type", "UNKNOWN")

    # ---- Prompt ------------------------------------------------------
    prompt = f"""
You are validating a semantic relationship between two attributes.

Source attribute:
- Name: {source.get('name', 'UNKNOWN')}
- Semantic type: {src_semantic_type}

Target attribute:
- Name: {target.get('name', 'UNKNOWN')}
- Semantic type: {tgt_semantic_type}

Observed signal: {signal}

Choose ONE relationship:
- SEGMENTS
- DERIVES
- SUPPORTS
- ASSOCIATED
- UNRELATED

If semantic types are UNKNOWN, rely primarily on the observed signal.

Return JSON ONLY:
{{
"relationship": "...",
"confidence": 0.0,
"rationale": "short explanation"
}}
"""

    out = self.execute_api_call(
        prompt,
        properties=self.properties,
        additional_data={}
    )

    return json_utils.safe_json_parse(out)
Last update: 2025-10-09