File size: 61,650 Bytes
66a942d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 | #!/usr/bin/env python3
"""
EvoRM Plugin: Evolvable Neuro-Symbolic Reasoning Framework
===========================================================
A plug-and-play module that implements the EvoRM framework from TKDE paper.
Integrates with AdaCoAgentEA's LLM1_label_selector.py.
Core components:
- RuleEncoding: distill LLM decisions into FOL rules
- HypergraphStorage: weighted hypergraph for rules + entity contexts
- TwoStageInferenceController: symbolic filtering + LLM judgment with feedback
- RuleMaintenance: dynamic confidence tracking + periodic optimization
"""
import os
import json
import re
import time
import math
import hashlib
import threading
from collections import defaultdict
from typing import Dict, List, Tuple, Set, Optional, Any
# ===== EvoRM MLP Gate (TKDE Section III-E) =====
from evorm_mlp_gate import MLPGate
# ===== EvoRM Mlight/Mheavy (TKDE Section III-C) =====
from evorm_mlight import MlightRationaleElicitor, MheavyDecisionMaker
from evorm_entity_embedding import EntityEmbedder
from evorm_config import EvoRMConfig
from dataclasses import dataclass, field
# ==============================================================================
# Data Structures
# ==============================================================================
@dataclass
class ConditionAtom:
"""A single condition atom in a FOL rule."""
atom_type: str # e.g., "SameValue", "DifferValue", "ShareNeighbor", "DifferNeighbor"
attr: str # attribute or relation name
value1: str = "" # value for entity 1
value2: str = "" # value for entity 2
def to_key(self) -> str:
return f"{self.atom_type}({self.attr})"
def __hash__(self):
return hash(self.to_key())
def __eq__(self, other):
return self.to_key() == other.to_key()
@dataclass
class FOLRule:
"""A first-order logic rule distilled from LLM inference."""
rule_id: str
atoms: List[ConditionAtom]
conclusion: int # 1 = match, 0 = non-match
conf: float = 0.0 # global confidence Conf(R)
conf0: float = 0.0 # initial confidence
fresh: float = 1.0 # freshness Fresh(R)
trigger_count: int = 0 # |Trig(R)|
used_count: int = 0 # times LLM used this rule
sR_sum: float = 0.0 # sum of contribution scores
last_used_time: float = 0.0 # timestamp of last positive contribution
created_time: float = 0.0
def update_confidence(self):
"""Update global confidence: Conf(R) = (Conf0 + sum(sR)) / (1 + trigger_count)"""
self.conf = (self.conf0 + self.sR_sum) / (1.0 + self.trigger_count)
def update_freshness(self, decay_lambda: float = 0.01):
"""Update freshness: Fresh(R) = exp(-lambda * (t_now - t_last))"""
if self.last_used_time > 0:
self.fresh = math.exp(-decay_lambda * (time.time() - self.last_used_time))
else:
self.fresh = 1.0
def atom_set(self) -> Set[str]:
return {a.to_key() for a in self.atoms}
def __hash__(self):
return hash(self.rule_id)
def __eq__(self, other):
return self.rule_id == other.rule_id
@dataclass
class Hyperedge:
"""A hyperedge in the weighted hypergraph, representing a matching cluster."""
hyperedge_id: str
entity_pairs: Set[Tuple[int, int]] = field(default_factory=set) # Mk
node_set: Set[str] = field(default_factory=set) # Vk
rules: List[FOLRule] = field(default_factory=list) # Rk
weight: float = 0.0 # wk
def recompute_weight(self, alpha: float = 0.6, beta: float = 0.4,
entity_embeddings: Dict = None):
"""
Recompute hyperedge weight: wk = alpha * max(Conf(R)) + beta * avg_cos_sim(Vk)
Paper Eq. 6: The hyperedge weight combines symbolic rule confidence
with neural embedding similarity of entity pairs in the cluster.
"""
symbolic = 0.0
if self.rules:
symbolic = max(r.conf for r in self.rules)
neural = 0.0
if entity_embeddings and self.entity_pairs:
# Collect embeddings for entities in this hyperedge
embs = []
for ep in self.entity_pairs:
eid1, eid2 = f"e_{ep[0]}", f"e_{ep[1]}"
if eid1 in entity_embeddings:
embs.append(entity_embeddings[eid1])
if eid2 in entity_embeddings:
embs.append(entity_embeddings[eid2])
if len(embs) >= 2:
# Compute avg pairwise cosine similarity
from evorm_entity_embedding import EntityEmbedder
embedder = EntityEmbedder()
neural = embedder.avg_cosine_similarity(embs)
elif entity_embeddings:
# Fallback: use rule confidence as proxy
neural = symbolic * 0.5
self.weight = alpha * symbolic + beta * neural
# ==============================================================================
# Rule Encoding Module
# ==============================================================================
class RuleEncoding:
"""Convert LLM matching decisions into explicit FOL rules."""
# Template-based parsing patterns
ATTR_SAME_PATTERN = re.compile(
r'(\w+)\s*=\s*same\s*(?:\(([^)]*)\))?', re.IGNORECASE)
ATTR_DIFFER_PATTERN = re.compile(
r'(\w+)\s*=\s*differ\s*(?:\(([^)]*)\))?', re.IGNORECASE)
NEIGHBOR_PATTERN = re.compile(
r'neighbor\s*:\s*(\w+)', re.IGNORECASE)
NEIGHBOR_DIFFER_PATTERN = re.compile(
r'neighbor\s*:\s*differ\s*(\w*)', re.IGNORECASE)
SHARE_NEIGHBOR_PATTERN = re.compile(
r'(?:share|has)\s+(?:common\s+)?(?:neighbor|relation)\s*(?::\s*)?(\w*)', re.IGNORECASE)
def __init__(self, client=None):
self.client = client
self.rule_counter = 0
def _generate_rule_id(self, atoms: List[ConditionAtom]) -> str:
"""Generate a deterministic rule ID from atoms."""
key = "|".join(sorted(a.to_key() for a in atoms))
return hashlib.md5(key.encode()).hexdigest()[:16]
def parse_rationale(self, rationale: str, conclusion: int) -> List[ConditionAtom]:
"""
Parse a natural language rationale into typed condition atoms.
Args:
rationale: natural language rationale from LLM
conclusion: 1 (match) or 0 (non-match)
Returns:
List of ConditionAtom objects
"""
atoms = []
# Extract [DECISIVE] and [SUPPORTING] sections
decisive_section = ""
supporting_section = ""
decisive_match = re.search(
r'\[DECISIVE\](.*?)(?:\[SUPPORTING\]|$)', rationale, re.DOTALL)
if decisive_match:
decisive_section = decisive_match.group(1)
supporting_match = re.search(
r'\[SUPPORTING\](.*?)$', rationale, re.DOTALL)
if supporting_match:
supporting_section = supporting_match.group(1)
all_text = decisive_section + " " + supporting_section
# Parse attr=same patterns
for m in self.ATTR_SAME_PATTERN.finditer(all_text):
attr = m.group(1).strip().lower()
atoms.append(ConditionAtom(
atom_type="SameValue",
attr=attr
))
# Parse attr=differ patterns
for m in self.ATTR_DIFFER_PATTERN.finditer(all_text):
attr = m.group(1).strip().lower()
atoms.append(ConditionAtom(
atom_type="DifferValue",
attr=attr
))
# Parse neighbor:rel patterns
for m in self.NEIGHBOR_PATTERN.finditer(all_text):
rel = m.group(1).strip().lower()
if rel and 'differ' not in rel.lower():
atoms.append(ConditionAtom(
atom_type="ShareNeighbor",
attr=rel
))
# Parse neighbor:differ patterns
for m in self.NEIGHBOR_DIFFER_PATTERN.finditer(all_text):
rel = m.group(1).strip().lower()
if rel:
atoms.append(ConditionAtom(
atom_type="DifferNeighbor",
attr=rel
))
# If no atoms parsed, create a generic atom based on the rationale
if not atoms:
# Try to extract any attribute mentions
words = re.findall(r'\b\w+\b', all_text)
# Create a simple atom based on the conclusion
if conclusion == 1:
atoms.append(ConditionAtom(
atom_type="SameValue",
attr="entity_name"
))
else:
atoms.append(ConditionAtom(
atom_type="DifferValue",
attr="entity_name"
))
return atoms
def create_rule(self, rationale: str, conclusion: int,
used_rules: List[Tuple['FOLRule', float]] = None) -> FOLRule:
"""
Create a new FOL rule from rationale and LLM feedback.
Args:
rationale: natural language rationale
conclusion: 1 (match) or 0 (non-match)
used_rules: list of (rule, contribution_score) from LLM feedback
Returns:
New FOLRule object
"""
atoms = self.parse_rationale(rationale, conclusion)
rule_id = self._generate_rule_id(atoms)
rule = FOLRule(
rule_id=rule_id,
atoms=atoms,
conclusion=conclusion,
created_time=time.time(),
last_used_time=time.time(),
)
# Warm-start confidence from overlapping used rules
if used_rules:
max_s = 0.0
for used_rule, sR in used_rules:
overlap = len(set(a.to_key() for a in atoms) &
set(a.to_key() for a in used_rule.atoms))
union = len(set(a.to_key() for a in atoms) |
set(a.to_key() for a in used_rule.atoms))
if union > 0 and overlap / union > 0:
max_s = max(max_s, sR)
rule.conf0 = max_s
else:
rule.conf0 = 0.0
rule.conf = rule.conf0
return rule
def elicit_rationale(self, entity_context: str, decision: int,
prompt_template: str = None) -> str:
"""
Use LLM to elicit rationale for a matching decision.
Args:
entity_context: serialized entity pair context
decision: 1 (match) or 0 (non-match)
prompt_template: optional custom template
Returns:
Natural language rationale
"""
if prompt_template is None:
decision_str = "MATCH" if decision == 1 else "NON-MATCH"
prompt_template = f"""Analyze the following entity pair and explain why they are a {decision_str}.
Entity Context:
{entity_context}
Please output your analysis in the following format:
[DECISIVE] List the core attributes or relations that are pivotal to the {decision_str} decision.
[SUPPORTING] List any auxiliary evidence that corroborates the decision.
Use the format: attr=same(value), attr=differ(value1 vs value2), or neighbor:rel_name."""
if self.client:
try:
response = self.client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{'role': 'user', 'content': prompt_template}],
temperature=0.1
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"RuleEncoding: rationale elicitation failed: {e}")
return f"[DECISIVE] entity_name=same\n[SUPPORTING] automatic fallback"
# Fallback without client
return f"[DECISIVE] entity_name={'same' if decision == 1 else 'differ'}\n[SUPPORTING] automatic fallback"
# ==============================================================================
# Hypergraph Storage Module
# ==============================================================================
class HypergraphStorage:
"""Weighted hypergraph for organizing rules and entity contexts."""
def __init__(self, alpha: float = 0.6, beta: float = 0.4,
merge_threshold: float = 0.5,
entity_embedder: 'EntityEmbedder' = None):
self.hyperedges: Dict[str, Hyperedge] = {}
self.rules: Dict[str, FOLRule] = {}
self.inverted_index: Dict[str, Set[str]] = defaultdict(set) # entity -> hyperedge IDs
self.alpha = alpha
self.beta = beta
self.merge_threshold = merge_threshold # eta_h
self.edge_counter = 0
self.entity_embedder = entity_embedder
self.entity_embeddings: Dict[str, 'np.ndarray'] = {} # entity_id -> embedding
self.flat_mode = False # w/o Hypergraph ablation (Table VI "w/o U")
def _generate_edge_id(self) -> str:
self.edge_counter += 1
return f"HE_{self.edge_counter:06d}"
def get_or_create_hyperedge(self, entity_pair: Tuple[int, int],
node_set: Set[str],
rule: FOLRule) -> Hyperedge:
"""
Find the most similar existing hyperedge or create a new one.
Args:
entity_pair: (es, et) tuple
node_set: VR - nodes extracted from entity context
rule: the new FOL rule
Returns:
The matched or newly created hyperedge
"""
# Flat mode (w/o Hypergraph ablation): don't create hyperedges
if self.flat_mode:
# Still register the rule globally
if rule.rule_id not in self.rules:
self.rules[rule.rule_id] = rule
# Return a dummy hyperedge (not stored)
dummy = Hyperedge(
hyperedge_id='flat',
entity_pairs={entity_pair},
node_set=node_set,
rules=[rule],
)
return dummy
# Find the most structurally similar hyperedge
best_id = None
best_overlap = 0.0
for he_id, he in self.hyperedges.items():
if not he.node_set:
continue
overlap = len(node_set & he.node_set)
union = len(node_set | he.node_set)
if union > 0:
jaccard = overlap / union
if jaccard > best_overlap:
best_overlap = jaccard
best_id = he_id
if best_id and best_overlap >= self.merge_threshold:
# Update existing hyperedge
he = self.hyperedges[best_id]
he.entity_pairs.add(entity_pair)
he.node_set.update(node_set)
if rule not in he.rules:
he.rules.append(rule)
he.recompute_weight(self.alpha, self.beta, self.entity_embeddings)
# Update inverted index
for node in node_set:
self.inverted_index[node].add(best_id)
return he
else:
# Create new hyperedge
new_id = self._generate_edge_id()
he = Hyperedge(
hyperedge_id=new_id,
entity_pairs={entity_pair},
node_set=node_set,
rules=[rule],
)
he.recompute_weight(self.alpha, self.beta, self.entity_embeddings)
self.hyperedges[new_id] = he
# Update inverted index
for node in node_set:
self.inverted_index[node].add(new_id)
# Also register the rule
if rule.rule_id not in self.rules:
self.rules[rule.rule_id] = rule
return he
def get_candidate_hyperedges(self, entity_ids: List[str]) -> List[Hyperedge]:
"""
Retrieve candidate hyperedges containing any of the given entity IDs.
O(1) complexity via inverted index.
Args:
entity_ids: list of entity ID strings
Returns:
List of candidate hyperedges
"""
candidate_ids: Set[str] = set()
for eid in entity_ids:
candidate_ids.update(self.inverted_index.get(eid, set()))
return [self.hyperedges[hid] for hid in candidate_ids
if hid in self.hyperedges]
def store_entity_embedding(self, entity_id: str, embedding: 'np.ndarray'):
"""Store entity embedding for neural similarity computation."""
self.entity_embeddings[entity_id] = embedding
def get_entity_embeddings(self) -> Dict:
"""Get all stored entity embeddings."""
return self.entity_embeddings
def get_candidate_rules(self, entity_ids: List[str]) -> List[FOLRule]:
"""
Get all candidate rules from hyperedges containing the entity IDs.
Args:
entity_ids: list of entity ID strings
Returns:
List of candidate FOL rules
"""
# Flat mode (w/o Hypergraph): return all rules
if self.flat_mode:
return list(self.rules.values())
candidates = self.get_candidate_hyperedges(entity_ids)
rules_set: Dict[str, FOLRule] = {}
for he in candidates:
for rule in he.rules:
if rule.rule_id not in rules_set:
rules_set[rule.rule_id] = rule
return list(rules_set.values())
def register_rule(self, rule: FOLRule):
"""Register a rule in the global rule set."""
if rule.rule_id not in self.rules:
self.rules[rule.rule_id] = rule
def get_rule(self, rule_id: str) -> Optional[FOLRule]:
return self.rules.get(rule_id)
def stats(self) -> Dict:
return {
'num_hyperedges': len(self.hyperedges),
'num_rules': len(self.rules),
'num_indexed_entities': len(self.inverted_index),
'avg_rules_per_edge': (
sum(len(he.rules) for he in self.hyperedges.values()) /
max(1, len(self.hyperedges))
),
}
# ==============================================================================
# Two-Stage Inference Controller
# ==============================================================================
class TwoStageInferenceController:
"""Two-stage controller: symbolic filtering + LLM judgment with feedback."""
def __init__(self, hypergraph: HypergraphStorage,
client=None,
theta_hi: float = 0.7,
theta_prune: float = 0.5,
theta_gate: float = 0.3,
K: int = 5,
use_mlight: bool = True):
self.hypergraph = hypergraph
self.client = client
self.theta_hi = theta_hi # high confidence threshold for direct match
self.theta_prune = theta_prune # threshold for direct non-match
self.theta_gate = theta_gate # MLP gate threshold
self.K = K # top-K hyperedges for evidence subgraph
# MLP Gate (gϕ) — paper Section III-E
self.mlp_gate = None # Set externally by EvoRMPlugin
self.use_mlp_gate = True
# Mlight / Mheavy — paper Section III-C (rationale elicitation separated from decision)
self.use_mlight = use_mlight
self.mlight = None # Set externally by EvoRMPlugin
self.mheavy = None # Set externally by EvoRMPlugin
# Statistics
self.stage1_hits = 0
self.stage1_total = 0
self.stage2_hits = 0
self.stage2_total = 0
def verify_rule(self, rule: FOLRule, es_context: Dict,
et_context: Dict) -> bool:
"""
Verify if a rule is triggered for the given entity pair.
Args:
rule: FOL rule to verify
es_context: source entity context (dict with attributes)
et_context: target entity context (dict with attributes)
Returns:
True if all atoms in the rule hold
"""
for atom in rule.atoms:
if not self._verify_atom(atom, es_context, et_context):
return False
return True
def _verify_atom(self, atom: ConditionAtom, es_ctx: Dict,
et_ctx: Dict) -> bool:
"""Verify a single condition atom."""
attr = atom.attr
if atom.atom_type == "SameValue":
# Check if both entities have the same value for this attribute
v1 = es_ctx.get(attr, "")
v2 = et_ctx.get(attr, "")
return v1 != "" and v2 != "" and v1.lower() == v2.lower()
elif atom.atom_type == "DifferValue":
v1 = es_ctx.get(attr, "")
v2 = et_ctx.get(attr, "")
return v1 != "" and v2 != "" and v1.lower() != v2.lower()
elif atom.atom_type == "ShareNeighbor":
# Check if entities share a neighbor via this relation
es_neighbors = es_ctx.get(f"neighbors_{attr}", set())
et_neighbors = et_ctx.get(f"neighbors_{attr}", set())
return bool(es_neighbors & et_neighbors)
elif atom.atom_type == "DifferNeighbor":
es_neighbors = es_ctx.get(f"neighbors_{attr}", set())
et_neighbors = et_ctx.get(f"neighbors_{attr}", set())
return not bool(es_neighbors & et_neighbors) and es_neighbors and et_neighbors
elif atom.atom_type == "SemanticEquiv":
# Simplified: check if values are similar enough
v1 = es_ctx.get(attr, "")
v2 = et_ctx.get(attr, "")
if v1 and v2:
# Simple token overlap
tokens1 = set(v1.lower().split())
tokens2 = set(v2.lower().split())
if tokens1 and tokens2:
overlap = len(tokens1 & tokens2) / len(tokens1 | tokens2)
return overlap > 0.5
return False
elif atom.atom_type == "SemanticConflict":
v1 = es_ctx.get(attr, "")
v2 = et_ctx.get(attr, "")
if v1 and v2:
tokens1 = set(v1.lower().split())
tokens2 = set(v2.lower().split())
if tokens1 and tokens2:
overlap = len(tokens1 & tokens2) / len(tokens1 | tokens2)
return overlap < 0.2
return False
return False
def stage1_symbolic_filtering(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict
) -> Tuple[Optional[int], List[FOLRule], List[FOLRule]]:
"""
Stage 1: Symbolic filtering and gated routing.
Returns:
(decision, triggered_rules, candidate_rules)
decision: None if survival (needs Stage 2), 0 or 1 if direct routing
"""
self.stage1_total += 1
# Candidate retrieval
candidate_rules = self.hypergraph.get_candidate_rules([es_id, et_id])
# Rule verification
triggered: List[FOLRule] = []
for rule in candidate_rules:
if self.verify_rule(rule, es_context, et_context):
triggered.append(rule)
if not triggered:
return None, [], candidate_rules
# Check verdicts
match_rules = [r for r in triggered if r.conclusion == 1]
nonmatch_rules = [r for r in triggered if r.conclusion == 0]
# Direct Matching
if match_rules and not nonmatch_rules:
max_conf = max(r.conf for r in match_rules)
if max_conf >= self.theta_hi:
self.stage1_hits += 1
return 1, triggered, candidate_rules
# Direct Non-Matching
if nonmatch_rules and not match_rules:
max_conf = max(r.conf for r in nonmatch_rules)
if max_conf >= self.theta_prune:
self.stage1_hits += 1
return 0, triggered, candidate_rules
# Survival - check MLP gate before deferring to Stage 2
if self.use_mlp_gate and self.mlp_gate is not None and self.mlp_gate.is_trained:
try:
features = self.mlp_gate.extract_features(es_context, et_context, triggered)
if not self.mlp_gate.should_invoke_llm(features):
self.stage1_hits += 1
return 0, triggered, candidate_rules
except Exception:
pass # Fall through to Stage 2 on any error
return None, triggered, candidate_rules
def construct_evidence_subgraph(self, es_id: str, et_id: str,
candidate_rules: List[FOLRule]) -> Dict:
"""
Construct a compact evidence subgraph for LLM inference.
Args:
es_id: source entity ID
et_id: target entity ID
candidate_rules: candidate rules
Returns:
Evidence subgraph data
"""
# Get hyperedges
candidate_edges = self.hypergraph.get_candidate_hyperedges([es_id, et_id])
# Prioritize edges containing both entities
joint_edges = []
other_edges = []
for he in candidate_edges:
if es_id in he.node_set and et_id in he.node_set:
joint_edges.append(he)
else:
other_edges.append(he)
# Sort by weight
joint_edges.sort(key=lambda x: x.weight, reverse=True)
other_edges.sort(key=lambda x: x.weight, reverse=True)
# Select top-K
selected = joint_edges[:self.K]
if len(selected) < self.K:
selected.extend(other_edges[:self.K - len(selected)])
return {
'hyperedges': selected,
'rules': candidate_rules,
}
def build_stage2_prompt(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict,
evidence: Dict,
triggered_rules: List[FOLRule],
base_prompt: str) -> str:
"""
Build the Stage 2 prompt with evidence subgraph and rule metadata.
Args:
es_id, et_id: entity IDs
es_context, et_context: entity contexts
evidence: evidence subgraph from construct_evidence_subgraph
triggered_rules: triggered rules
base_prompt: original prompt from AdaCoAgentEA
Returns:
Augmented prompt
"""
# Build rule metadata section
rule_section = ""
if triggered_rules:
rule_section = "\n\n**Triggered Rules (from historical matching experience):**\n"
for i, rule in enumerate(triggered_rules, 1):
atom_strs = []
for a in rule.atoms:
atom_strs.append(f"{a.atom_type}({a.attr})")
atoms_str = " ∧ ".join(atom_strs)
verdict = "MATCH" if rule.conclusion == 1 else "NON-MATCH"
rule_section += (
f" Rule {i}: {atoms_str} → {verdict}\n"
f" Confidence: {rule.conf:.3f} | "
f"Triggered: {rule.trigger_count} times\n"
)
# Build evidence summary
evidence_section = ""
if evidence.get('hyperedges'):
evidence_section = "\n\n**Evidence from Similar Historical Cases:**\n"
for he in evidence['hyperedges'][:3]:
matching_pairs = len(he.entity_pairs)
evidence_section += (
f" - Cluster (weight={he.weight:.3f}): "
f"{matching_pairs} similar entity pairs stored\n"
)
# Build the feedback instruction
feedback_instruction = """
**IMPORTANT: Rule Contribution Feedback**
After making your decision, you MUST also provide a JSON object with contribution scores (0.0 to 1.0) for each triggered rule that influenced your decision. Format:
```json
{
"decision": "MATCH" or "NON-MATCH",
"rule_feedback": {
"rule_1": 0.8,
"rule_2": 0.3
},
"reasoning": "[DECISIVE] ... [SUPPORTING] ..."
}
```
Score meaning: 1.0 = DECISIVE (rule was the key factor), 0.1-0.9 = SUPPORTING (rule partially influenced), 0.0 = IRRELEVANT (rule was considered but not used).
You MUST respond with valid JSON only."""
return base_prompt + rule_section + evidence_section + feedback_instruction
def parse_llm_feedback(self, response_text: str
) -> Tuple[Optional[int], Dict[str, float], str]:
"""
Parse the LLM's JSON response to extract decision, rule feedback, and rationale.
Returns:
(decision, rule_feedback dict, rationale)
"""
# Try to extract JSON from response
try:
# Find JSON block
json_match = re.search(r'```json\s*(.*?)\s*```', response_text, re.DOTALL)
if json_match:
json_str = json_match.group(1)
else:
# Try to find bare JSON
json_match = re.search(r'\{.*"decision".*\}', response_text, re.DOTALL)
if json_match:
json_str = json_match.group(0)
else:
json_str = response_text
data = json.loads(json_str)
decision_str = data.get('decision', '').upper()
decision = 1 if 'MATCH' in decision_str and 'NON' not in decision_str else 0
rule_feedback = data.get('rule_feedback', {})
reasoning = data.get('reasoning', '')
return decision, rule_feedback, reasoning
except (json.JSONDecodeError, KeyError):
pass
# Fallback: try to parse from plain text
decision = None
if 'match' in response_text.lower() and 'non-match' not in response_text.lower():
decision = 1
elif 'non-match' in response_text.lower() or 'no match' in response_text.lower():
decision = 0
return decision, {}, response_text
def stage2_elicit_rationale(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict,
triggered_rules: List[FOLRule]):
"""
Stage 2a: Elicit rationale using Mlight (independent from decision).
Paper Section III-C Step 1: Mlight generates a natural language
rationale explaining the matching evidence, WITHOUT making a decision.
Returns:
RationaleResult with structured analysis
"""
if self.mlight is not None:
return self.mlight.elicit(es_context, et_context, triggered_rules)
else:
from evorm_mlight import RationaleResult
return RationaleResult(
rationale="[DECISIVE] entity_name=same\\n[SUPPORTING] automatic fallback (no Mlight)",
token_usage=0,
)
def stage2_llm_judgment(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict,
triggered_rules: List[FOLRule],
candidate_rules: List[FOLRule],
base_prompt: str) -> Dict:
"""
Stage 2: LLM judgment with rule feedback.
Returns:
Dict with decision, rule_feedback, rationale, token_usage
"""
self.stage2_total += 1
# Increment trigger counts
for rule in triggered_rules:
rule.trigger_count += 1
# ================================================================
# Two-step Mlight-Mheavy pipeline (Paper Section III-C)
# ================================================================
if self.use_mlight and self.mlight is not None and self.mheavy is not None:
# Step 1: Mlight - elicit rationale (no decision)
rationale_result = self.stage2_elicit_rationale(
es_id, et_id, es_context, et_context, triggered_rules)
# Step 2: Mheavy - make decision with rationale as context
decision_result = self.mheavy.decide(
es_context, et_context,
rationale_result.rationale,
triggered_rules)
total_tokens = (rationale_result.token_usage +
decision_result.get('token_usage', 0))
if decision_result.get('decision') is not None:
self.stage2_hits += 1
return {
'decision': decision_result.get('decision'),
'rule_feedback': decision_result.get('rule_feedback', {}),
'rationale': rationale_result.rationale,
'confidence': decision_result.get('confidence', 0.5),
'token_usage': total_tokens,
'raw_response': decision_result.get('raw_response', ''),
'mlight_raw': rationale_result.raw_response,
'method': 'mlight+mheavy',
}
# ================================================================
# Legacy single-call approach (fallback)
# ================================================================
# Construct evidence subgraph
evidence = self.construct_evidence_subgraph(
es_id, et_id, candidate_rules)
# Build augmented prompt
prompt = self.build_stage2_prompt(
es_id, et_id, es_context, et_context,
evidence, triggered_rules, base_prompt)
if self.client:
try:
response = self.client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{'role': 'user', 'content': prompt}],
temperature=0.1,
response_format={"type": "json_object"},
)
response_text = response.choices[0].message.content.strip()
tokens = response.usage.total_tokens
decision, rule_feedback, rationale = self.parse_llm_feedback(
response_text)
if decision is not None:
self.stage2_hits += 1
return {
'decision': decision,
'rule_feedback': rule_feedback,
'rationale': rationale,
'token_usage': tokens,
'raw_response': response_text,
'method': 'single-call',
}
except Exception as e:
print(f"Stage 2 LLM call failed: {e}")
return {
'decision': None,
'rule_feedback': {},
'rationale': '',
'token_usage': 0,
'raw_response': '',
'error': str(e),
'method': 'error',
}
# Fallback without client
return {
'decision': None,
'rule_feedback': {},
'rationale': 'no client available',
'token_usage': 0,
'raw_response': '',
'method': 'no-client',
}
def get_stats(self) -> Dict:
stats = {
'stage1_total': self.stage1_total,
'stage1_hits': self.stage1_hits,
'stage1_rate': self.stage1_hits / max(1, self.stage1_total),
'stage2_total': self.stage2_total,
'stage2_hits': self.stage2_hits,
'use_mlight': self.use_mlight,
}
if self.mlight is not None:
stats['mlight'] = self.mlight.get_stats()
if self.mheavy is not None:
stats['mheavy'] = self.mheavy.get_stats()
return stats
# ==============================================================================
# Rule Maintenance Module
# ==============================================================================
class RuleMaintenance:
"""Dynamic rule maintenance: confidence tracking + periodic optimization."""
def __init__(self, hypergraph: HypergraphStorage,
decay_lambda: float = 0.01,
freshness_threshold: float = 0.1,
confidence_threshold: float = 0.2,
eval_triggers: int = 10,
merge_similarity: float = 0.7,
max_rules: int = 10000,
evict_percentile: float = 0.1):
self.hypergraph = hypergraph
self.decay_lambda = decay_lambda
self.theta_f = freshness_threshold
self.theta_c = confidence_threshold
self.N_eval = eval_triggers
self.eta_m = merge_similarity
self.max_rules = max_rules
self.evict_percentile = evict_percentile
self.last_optimization_time = time.time()
self.optimization_interval = 300 # 5 minutes
def update_rule_confidence(self, rule_id: str, sR: float):
"""
Update rule confidence with LLM contribution score.
Args:
rule_id: the rule to update
sR: contribution score from LLM (0.0 to 1.0)
"""
rule = self.hypergraph.get_rule(rule_id)
if rule is None:
return
rule.sR_sum += sR
if sR > 0:
rule.used_count += 1
rule.last_used_time = time.time()
rule.update_confidence()
rule.update_freshness(self.decay_lambda)
def should_optimize(self) -> bool:
"""Check if it's time for periodic optimization."""
return (time.time() - self.last_optimization_time) >= self.optimization_interval
def optimize(self):
"""Execute periodic set optimization: pruning, flipping, merging, forgetting."""
self.last_optimization_time = time.time()
rules = list(self.hypergraph.rules.values())
if not rules:
return
# 1. Pruning stale rules
stale_rules = []
for rule in rules:
rule.update_freshness(self.decay_lambda)
if rule.fresh < self.theta_f:
stale_rules.append(rule.rule_id)
for rid in stale_rules:
self._remove_rule(rid)
if stale_rules:
print(f"RuleMaintenance: pruned {len(stale_rules)} stale rules")
# 2. Conclusion flipping for low-confidence rules
remaining = [r for r in self.hypergraph.rules.values()
if r.rule_id not in stale_rules]
for rule in remaining:
if (rule.conf < self.theta_c and
rule.trigger_count >= self.N_eval and
rule.used_count < rule.trigger_count * 0.3):
# Flip conclusion
old_conclusion = rule.conclusion
rule.conclusion = 1 - rule.conclusion
rule.conf = 0.3 # warm-start reset
rule.conf0 = 0.3
rule.sR_sum = 0.0
rule.used_count = 0
print(f"RuleMaintenance: flipped conclusion of {rule.rule_id} "
f"from {old_conclusion} to {rule.conclusion}")
# 3. Rule merging
self._merge_similar_rules()
# 4. Capacity-based forgetting
if len(self.hypergraph.rules) > self.max_rules:
self._evict_low_utility_rules()
def _remove_rule(self, rule_id: str):
"""Remove a rule from the hypergraph."""
if rule_id in self.hypergraph.rules:
del self.hypergraph.rules[rule_id]
# Remove from hyperedges
for he in self.hypergraph.hyperedges.values():
he.rules = [r for r in he.rules if r.rule_id != rule_id]
def _merge_similar_rules(self):
"""Merge similar rules using Jaccard similarity on atom sets."""
rule_list = list(self.hypergraph.rules.values())
merged = set()
for i, r1 in enumerate(rule_list):
if r1.rule_id in merged:
continue
cluster = [r1]
for j, r2 in enumerate(rule_list):
if i >= j or r2.rule_id in merged:
continue
if r1.conclusion == r2.conclusion:
set1 = r1.atom_set()
set2 = r2.atom_set()
if set1 and set2:
union = len(set1 | set2)
if union > 0:
jaccard = len(set1 & set2) / union
if jaccard >= self.eta_m:
cluster.append(r2)
if len(cluster) > 1:
# Merge cluster into r1 (keep the one with higher confidence)
best = max(cluster, key=lambda r: r.conf * r.trigger_count)
for r in cluster:
if r.rule_id != best.rule_id:
merged.add(r.rule_id)
# Transfer stats
best.trigger_count += r.trigger_count
best.sR_sum += r.sR_sum
best.used_count += r.used_count
best.last_used_time = max(
best.last_used_time, r.last_used_time)
self._remove_rule(r.rule_id)
# Recompute best rule's confidence as weighted average
total_trig = sum(r.trigger_count for r in cluster)
if total_trig > 0:
best.conf = sum(r.conf * r.trigger_count
for r in cluster) / total_trig
best.conf0 = best.conf
best.update_freshness(self.decay_lambda)
print(f"RuleMaintenance: merged {len(cluster)} rules into {best.rule_id}")
# Clean up merged rules
for rid in list(merged):
if rid in self.hypergraph.rules:
del self.hypergraph.rules[rid]
def _evict_low_utility_rules(self):
"""Evict low-utility rules when capacity is exceeded."""
rules = list(self.hypergraph.rules.values())
# Sort by utility: Conf(R) * Fresh(R)
rules.sort(key=lambda r: r.conf * r.fresh)
num_to_evict = int(len(rules) * self.evict_percentile)
for rule in rules[:num_to_evict]:
self._remove_rule(rule.rule_id)
print(f"RuleMaintenance: evicted {num_to_evict} low-utility rules")
# ==============================================================================
# EvoRM Plugin - Main Interface
# ==============================================================================
class EvoRMPlugin:
"""
Main EvoRM plugin class for integration with AdaCoAgentEA.
Usage:
plugin = EvoRMPlugin(client=openai_client)
# Stage 1: Check if symbolic routing can decide
decision, triggered, candidates = plugin.stage1(es_id, et_id, es_ctx, et_ctx)
if decision is not None:
# Direct routing - no LLM needed
return decision
else:
# Stage 2: LLM with evidence and rule feedback
result = plugin.stage2(es_id, et_id, es_ctx, et_ctx,
triggered, candidates, base_prompt)
# Process result['decision'] and result['rule_feedback']
"""
def __init__(self, client=None,
theta_hi: float = 0.7,
theta_prune: float = 0.5,
alpha: float = 0.6,
beta: float = 0.4,
merge_threshold: float = 0.5,
persistence_dir: str = None,
ablation_mode: str = None,
config: 'EvoRMConfig' = None):
# Use config if provided, otherwise use individual params
if config is not None:
self.config = config
alpha = config.alpha
beta = config.beta
merge_threshold = config.merge_threshold
theta_hi = config.theta_hi
theta_prune = config.theta_prune
persistence_dir = config.persistence_dir or persistence_dir
ablation_mode = config.ablation_mode or ablation_mode
else:
self.config = EvoRMConfig(
alpha=alpha, beta=beta, merge_threshold=merge_threshold,
theta_hi=theta_hi, theta_prune=theta_prune,
persistence_dir=persistence_dir, ablation_mode=ablation_mode)
self.client = client
self.persistence_dir = persistence_dir
self.ablation_mode = ablation_mode # None, 'no_stage1', 'no_maintenance', etc.
# Initialize components
self.rule_encoding = RuleEncoding(client=client)
self.hypergraph = HypergraphStorage(
alpha=alpha, beta=beta, merge_threshold=merge_threshold)
self.controller = TwoStageInferenceController(
self.hypergraph, client=client,
theta_hi=theta_hi, theta_prune=theta_prune)
self.maintenance = RuleMaintenance(
self.hypergraph)
# Entity Embedder — paper Section III-D (demb=1024, cosine similarity)
self.entity_embedder = EntityEmbedder(
demb=self.config.demb, n_features=self.config.n_features)
self.hypergraph.entity_embedder = self.entity_embedder
# Mlight / Mheavy — paper Section III-C (rationale + decision separation)
self.mlight = MlightRationaleElicitor(
client=client, model=self.config.model,
temperature=self.config.mlight_temperature,
max_retries=self.config.api_max_retries,
timeout=self.config.api_timeout)
self.mheavy = MheavyDecisionMaker(
client=client, model=self.config.model,
temperature=self.config.mheavy_temperature,
max_retries=self.config.api_max_retries,
timeout=self.config.api_timeout)
self.controller.mlight = self.mlight
self.controller.mheavy = self.mheavy
# Ablation: disable Mlight (use legacy single-call)
if self.ablation_mode == 'no_mlight':
self.controller.use_mlight = False
# Ablation: w/o Hypergraph (flat layout) — Table VI "w/o U"
if self.ablation_mode == 'no_hypergraph':
self.hypergraph.flat_mode = True
# MLP Gate (gϕ) — paper Section III-E
try:
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
except ImportError:
device = 'cpu'
self.mlp_gate = MLPGate(
input_dim=self.config.mlp_input_dim,
hidden_dims=self.config.mlp_hidden_dims,
theta_gate=self.config.theta_gate,
n_warmup=self.config.mlp_n_warmup,
device=device,
)
self.controller.mlp_gate = self.mlp_gate
self._mlp_samples_collected = 0
# Ablation: disable MLP gate
if self.ablation_mode == 'no_mlp_gate':
self.controller.use_mlp_gate = False
# Statistics
self.total_queries = 0
self.llm_calls_saved = 0
self.total_tokens = 0
# Load persisted state if available
if persistence_dir:
self._load_state()
def stage1(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict
) -> Tuple[Optional[int], List[FOLRule], List[FOLRule]]:
"""
Stage 1: Symbolic filtering.
Returns:
(decision, triggered_rules, candidate_rules)
decision: None if needs Stage 2, 0/1 if direct routing
"""
# Ablation: skip Stage 1
if self.ablation_mode == 'no_stage1':
return None, [], []
self.total_queries += 1
return self.controller.stage1_symbolic_filtering(
es_id, et_id, es_context, et_context)
def stage2(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict,
triggered_rules: List[FOLRule],
candidate_rules: List[FOLRule],
base_prompt: str) -> Dict:
"""
Stage 2: LLM judgment with evidence and rule feedback.
Returns:
Dict with 'decision', 'rule_feedback', 'rationale', 'token_usage'
"""
result = self.controller.stage2_llm_judgment(
es_id, et_id, es_context, et_context,
triggered_rules, candidate_rules, base_prompt)
self.total_tokens += result.get('token_usage', 0)
# Process rule feedback
if result.get('rule_feedback'):
for rule_key, sR in result['rule_feedback'].items():
# Try to find matching rule
rule_id = self._find_rule_by_key(rule_key, triggered_rules)
if rule_id:
self.maintenance.update_rule_confidence(rule_id, float(sR))
return result
def _find_rule_by_key(self, key: str, rules: List[FOLRule]) -> Optional[str]:
"""Find a rule ID by its index key (e.g., 'rule_1')."""
match = re.match(r'rule_(\d+)', key)
if match:
idx = int(match.group(1)) - 1
if 0 <= idx < len(rules):
return rules[idx].rule_id
return None
def record_trajectory(self, es_id: str, et_id: str,
es_context: Dict, et_context: Dict,
decision: int, rationale: str,
triggered_rules: List[FOLRule],
rule_feedback: Dict[str, float]):
"""
Record a matching trajectory after LLM inference.
Creates/updates hyperedges and rules in the hypergraph.
Args:
es_id: source entity ID
et_id: target entity ID
es_context: source entity context
et_context: target entity context
decision: 1 (match) or 0 (non-match)
rationale: LLM rationale
triggered_rules: triggered rules
rule_feedback: dict of rule_key -> contribution score
"""
# Ablation: skip rule maintenance
if self.ablation_mode == 'no_maintenance':
return
# Create a new rule from the rationale
used_rules = []
for rule_key, sR in rule_feedback.items():
rule_id = self._find_rule_by_key(rule_key, triggered_rules)
if rule_id:
rule = self.hypergraph.get_rule(rule_id)
if rule:
used_rules.append((rule, float(sR)))
new_rule = self.rule_encoding.create_rule(
rationale, decision, used_rules)
# Build node set from entity contexts
node_set = set()
node_set.add(f"e_{es_id}")
node_set.add(f"e_{et_id}")
for key, val in es_context.items():
if not key.startswith('neighbors_'):
node_set.add(f"a_{key}")
if val:
node_set.add(f"v_{key}_{str(val)[:50]}")
for key, val in et_context.items():
if not key.startswith('neighbors_'):
node_set.add(f"a_{key}")
if val:
node_set.add(f"v_{key}_{str(val)[:50]}")
# Compute and store entity embeddings
if hasattr(self, 'entity_embedder') and self.entity_embedder is not None:
try:
emb_s = self.entity_embedder.embed_entity(es_context, f"e_{es_id}")
emb_t = self.entity_embedder.embed_entity(et_context, f"e_{et_id}")
self.hypergraph.store_entity_embedding(f"e_{es_id}", emb_s)
self.hypergraph.store_entity_embedding(f"e_{et_id}", emb_t)
except Exception:
pass
# Create or update hyperedge
self.hypergraph.get_or_create_hyperedge(
(int(es_id) if es_id.isdigit() else hash(es_id),
int(et_id) if et_id.isdigit() else hash(et_id)),
node_set, new_rule)
# Collect MLP gate training sample
if hasattr(self, 'mlp_gate') and self.mlp_gate is not None:
if not self.mlp_gate.is_trained and triggered_rules:
try:
features = self.mlp_gate.extract_features(
es_context, et_context, triggered_rules)
self.mlp_gate.collect_sample(features, float(decision))
self._mlp_samples_collected += 1
except Exception:
pass
# Periodic maintenance
if self.maintenance.should_optimize():
self.maintenance.optimize()
def build_context_dict(self, entity_name: str, relations: List[str] = None,
descriptions: str = "") -> Dict:
"""
Build a context dictionary from entity information.
Args:
entity_name: entity name
relations: list of relation strings (e.g., "Has relation 'X' with Y")
descriptions: entity description text
Returns:
Context dict suitable for rule verification
"""
ctx = {'entity_name': entity_name, 'description': descriptions}
if relations:
for rel_str in relations:
# Parse "Has relation 'R' with E" or "Is R of E"
m1 = re.match(r"Has relation '([^']+)' with (.+)", rel_str)
if m1:
rel = m1.group(1).lower()
neighbor = m1.group(2).strip()
key = f"neighbors_{rel}"
if key not in ctx:
ctx[key] = set()
ctx[key].add(neighbor)
m2 = re.match(r"Is ([^']+) of (.+)", rel_str)
if m2:
rel = m2.group(1).lower()
neighbor = m2.group(2).strip()
key = f"neighbors_{rel}"
if key not in ctx:
ctx[key] = set()
ctx[key].add(neighbor)
return ctx
def get_stats(self) -> Dict:
"""Get comprehensive statistics."""
controller_stats = self.controller.get_stats()
hypergraph_stats = self.hypergraph.stats()
stats = {
**controller_stats,
**hypergraph_stats,
'total_queries': self.total_queries,
'llm_calls_saved': self.llm_calls_saved,
'llm_save_rate': (self.llm_calls_saved /
max(1, self.llm_calls_saved + self.controller.stage2_total)),
'total_tokens': self.total_tokens,
}
if hasattr(self, 'mlp_gate') and self.mlp_gate is not None:
stats['mlp_gate'] = self.mlp_gate.get_stats()
if hasattr(self, 'entity_embedder') and self.entity_embedder is not None:
stats['entity_embedder'] = self.entity_embedder.get_stats()
return stats
def _save_state(self):
"""Persist hypergraph state to disk."""
if not self.persistence_dir:
return
os.makedirs(self.persistence_dir, exist_ok=True)
state = {
'rules': {},
'hyperedges': {},
'inverted_index': {k: list(v) for k, v in self.hypergraph.inverted_index.items()},
'stats': self.get_stats(),
}
for rid, rule in self.hypergraph.rules.items():
state['rules'][rid] = {
'rule_id': rule.rule_id,
'atoms': [{'atom_type': a.atom_type, 'attr': a.attr,
'value1': a.value1, 'value2': a.value2}
for a in rule.atoms],
'conclusion': rule.conclusion,
'conf': rule.conf,
'conf0': rule.conf0,
'trigger_count': rule.trigger_count,
'used_count': rule.used_count,
'sR_sum': rule.sR_sum,
'last_used_time': rule.last_used_time,
'created_time': rule.created_time,
}
for hid, he in self.hypergraph.hyperedges.items():
state['hyperedges'][hid] = {
'hyperedge_id': he.hyperedge_id,
'entity_pairs': list(he.entity_pairs),
'node_set': list(he.node_set),
'rule_ids': [r.rule_id for r in he.rules],
'weight': he.weight,
}
state_path = os.path.join(self.persistence_dir, 'evorm_state.json')
with open(state_path, 'w', encoding='utf-8') as f:
json.dump(state, f, ensure_ascii=False, indent=2)
# Save MLP gate
if hasattr(self, 'mlp_gate') and self.mlp_gate is not None:
try:
mlp_path = os.path.join(self.persistence_dir, 'mlp_gate.pt')
self.mlp_gate.save(mlp_path)
except Exception as e:
print(f"Failed to save MLP gate: {e}")
print(f"EvoRM state saved to {state_path}")
def _load_state(self):
"""Load persisted hypergraph state from disk."""
if not self.persistence_dir:
return
state_path = os.path.join(self.persistence_dir, 'evorm_state.json')
if not os.path.exists(state_path):
return
try:
with open(state_path, 'r', encoding='utf-8') as f:
state = json.load(f)
# Restore rules
for rid, rdata in state.get('rules', {}).items():
atoms = [ConditionAtom(**a) for a in rdata['atoms']]
rule = FOLRule(
rule_id=rdata['rule_id'],
atoms=atoms,
conclusion=rdata['conclusion'],
conf=rdata['conf'],
conf0=rdata['conf0'],
trigger_count=rdata['trigger_count'],
used_count=rdata['used_count'],
sR_sum=rdata['sR_sum'],
last_used_time=rdata['last_used_time'],
created_time=rdata['created_time'],
)
self.hypergraph.rules[rid] = rule
# Restore hyperedges
for hid, hdata in state.get('hyperedges', {}).items():
he = Hyperedge(
hyperedge_id=hdata['hyperedge_id'],
entity_pairs=set(
tuple(p) for p in hdata['entity_pairs']),
node_set=set(hdata['node_set']),
weight=hdata['weight'],
)
for rid in hdata['rule_ids']:
if rid in self.hypergraph.rules:
he.rules.append(self.hypergraph.rules[rid])
self.hypergraph.hyperedges[hid] = he
# Restore inverted index
for k, v in state.get('inverted_index', {}).items():
self.hypergraph.inverted_index[k] = set(v)
# Load MLP gate
if hasattr(self, 'mlp_gate') and self.mlp_gate is not None:
try:
mlp_path = os.path.join(self.persistence_dir, 'mlp_gate.pt')
self.mlp_gate.load(mlp_path)
except Exception as e:
print(f"Failed to load MLP gate: {e}")
print(f"EvoRM state loaded from {state_path}: "
f"{len(self.hypergraph.rules)} rules, "
f"{len(self.hypergraph.hyperedges)} hyperedges")
except Exception as e:
print(f"Failed to load EvoRM state: {e}")
# ==============================================================================
# Test / Demo
# ==============================================================================
if __name__ == "__main__":
print("EvoRM Plugin - Self Test")
print("=" * 60)
# Create plugin without OpenAI client (offline test)
plugin = EvoRMPlugin(client=None)
# Test rule encoding
print("\n1. Testing Rule Encoding...")
rationale = """
[DECISIVE] title=same ("locating data sources"), year=same (2003)
[SUPPORTING] authors=same
"""
atoms = plugin.rule_encoding.parse_rationale(rationale, conclusion=1)
print(f" Parsed atoms: {[a.to_key() for a in atoms]}")
# Test rule creation
rule = plugin.rule_encoding.create_rule(rationale, conclusion=1)
print(f" Created rule: {rule.rule_id}, conf={rule.conf:.3f}")
# Test hypergraph storage
print("\n2. Testing Hypergraph Storage...")
node_set = {"e_1", "e_2", "a_title", "a_year", "a_authors"}
he = plugin.hypergraph.get_or_create_hyperedge(
(1, 100), node_set, rule)
print(f" Created hyperedge: {he.hyperedge_id}, weight={he.weight:.3f}")
# Test candidate retrieval
candidates = plugin.hypergraph.get_candidate_rules(["e_1"])
print(f" Candidates for e_1: {len(candidates)} rules")
# Test stage 1
print("\n3. Testing Stage 1 (Symbolic Filtering)...")
es_ctx = {"entity_name": "Test E1", "title": "locating data sources",
"year": "2003", "authors": "Smith et al"}
et_ctx = {"entity_name": "Test E2", "title": "locating data sources",
"year": "2003", "authors": "Smith et al"}
decision, triggered, cands = plugin.stage1("e_1", "e_2", es_ctx, et_ctx)
print(f" Decision: {decision}, Triggered: {len(triggered)} rules")
# Test trajectory recording
print("\n4. Testing Trajectory Recording...")
plugin.record_trajectory(
"e_1", "e_2", es_ctx, et_ctx,
decision=1, rationale=rationale,
triggered_rules=triggered,
rule_feedback={"rule_1": 0.8})
# Test stats
print("\n5. Stats:", json.dumps(plugin.get_stats(), indent=2))
print("\n✅ All tests passed!") |