Implement WP-26 v1.3 (Phase 3): Enhance graph schema validation and edge handling
- Introduced a new function `load_graph_schema_full` to parse and cache both typical and prohibited edge types from the graph schema. - Updated `load_graph_schema` to utilize the full schema for improved edge type extraction. - Added `get_topology_info` to retrieve typical and prohibited edges for source/target pairs. - Implemented `validate_intra_note_edge` and `validate_edge_against_schema` for schema validation of intra-note edges. - Enhanced logging for schema validation outcomes and edge handling. - Updated documentation to reflect new validation features and testing procedures.
This commit is contained in:
1 parent
c5215e22e7
commit
509efc9393
5 files changed
+674
-23
No files matched your search
@@ -281,6 +281,8 @@ def get_edge_defaults_for(note_type: Optional[str], reg: dict) -> List[str]:
|
||||
|
||||
# Cache für geladenes Schema (vermeidet mehrfaches Parsen)
|
||||
_GRAPH_SCHEMA_CACHE: Optional[Dict[str, Dict[str, List[str]]]] = None
|
||||
# WP-26 v1.3: Erweitertes Schema mit prohibited edges
|
||||
_GRAPH_SCHEMA_FULL_CACHE: Optional[Dict[str, Dict[str, Dict[str, List[str]]]]] = None
|
||||
|
||||
def load_graph_schema() -> Dict[str, Dict[str, List[str]]]:
|
||||
"""
|
||||
@@ -300,6 +302,31 @@ def load_graph_schema() -> Dict[str, Dict[str, List[str]]]:
|
||||
if _GRAPH_SCHEMA_CACHE is not None:
|
||||
return _GRAPH_SCHEMA_CACHE
|
||||
|
||||
# Nutze das erweiterte Schema und extrahiere nur typical
|
||||
full_schema = load_graph_schema_full()
|
||||
|
||||
schema: Dict[str, Dict[str, List[str]]] = {}
|
||||
for source_type, targets in full_schema.items():
|
||||
schema[source_type] = {}
|
||||
for target_type, edge_info in targets.items():
|
||||
schema[source_type][target_type] = edge_info.get("typical", [])
|
||||
|
||||
_GRAPH_SCHEMA_CACHE = schema
|
||||
return schema
|
||||
|
||||
|
||||
def load_graph_schema_full() -> Dict[str, Dict[str, Dict[str, List[str]]]]:
|
||||
"""
|
||||
WP-26 v1.3: Parst das graph_schema.md und extrahiert sowohl Typical als auch Prohibited Edge-Types.
|
||||
|
||||
Returns:
|
||||
Dict[source_type, Dict[target_type, {"typical": [...], "prohibited": [...]}]]
|
||||
Beispiel: {"experience": {"event": {"typical": ["caused_by"], "prohibited": ["consists_of"]}}}
|
||||
"""
|
||||
global _GRAPH_SCHEMA_FULL_CACHE
|
||||
if _GRAPH_SCHEMA_FULL_CACHE is not None:
|
||||
return _GRAPH_SCHEMA_FULL_CACHE
|
||||
|
||||
import re
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -331,10 +358,10 @@ def load_graph_schema() -> Dict[str, Dict[str, List[str]]]:
|
||||
|
||||
if not content:
|
||||
logger.warning("Graph-Schema nicht gefunden. Fallback auf leeres Schema.")
|
||||
_GRAPH_SCHEMA_CACHE = {}
|
||||
return _GRAPH_SCHEMA_CACHE
|
||||
_GRAPH_SCHEMA_FULL_CACHE = {}
|
||||
return _GRAPH_SCHEMA_FULL_CACHE
|
||||
|
||||
schema: Dict[str, Dict[str, List[str]]] = {}
|
||||
schema: Dict[str, Dict[str, Dict[str, List[str]]]] = {}
|
||||
current_source = None
|
||||
|
||||
# Regex für Source-Header: ## Source: `experience`
|
||||
@@ -346,6 +373,8 @@ def load_graph_schema() -> Dict[str, Dict[str, List[str]]]:
|
||||
r'^\|\s*`(\w+)`\s*\|\s*([^|]+)\s*\|\s*([^|]*)\s*\|'
|
||||
)
|
||||
|
||||
edge_pattern = re.compile(r'`(\w+)`')
|
||||
|
||||
for line in content.split('\n'):
|
||||
stripped = line.strip()
|
||||
|
||||
@@ -363,19 +392,59 @@ def load_graph_schema() -> Dict[str, Dict[str, List[str]]]:
|
||||
if row_match:
|
||||
target_type = row_match.group(1).lower()
|
||||
typical_edges_raw = row_match.group(2).strip()
|
||||
prohibited_edges_raw = row_match.group(3).strip()
|
||||
|
||||
# Parse die Edge-Types (können mit Backticks und Kommas getrennt sein)
|
||||
# Format: `caused_by`, `resulted_in` oder `caused_by`
|
||||
edge_pattern = re.compile(r'`(\w+)`')
|
||||
# Parse die Edge-Types
|
||||
typical_edges = edge_pattern.findall(typical_edges_raw)
|
||||
prohibited_edges = edge_pattern.findall(prohibited_edges_raw)
|
||||
|
||||
if typical_edges:
|
||||
schema[current_source][target_type] = typical_edges
|
||||
schema[current_source][target_type] = {
|
||||
"typical": typical_edges,
|
||||
"prohibited": prohibited_edges
|
||||
}
|
||||
|
||||
logger.info(f"Graph-Schema geladen: {len(schema)} Source-Types")
|
||||
_GRAPH_SCHEMA_CACHE = schema
|
||||
logger.info(f"Graph-Schema (full) geladen: {len(schema)} Source-Types")
|
||||
_GRAPH_SCHEMA_FULL_CACHE = schema
|
||||
return schema
|
||||
|
||||
|
||||
def get_topology_info(source_type: str, target_type: str) -> Dict[str, List[str]]:
|
||||
"""
|
||||
WP-26 v1.3: Ermittelt Typical und Prohibited Edge-Types für ein Typ-Paar.
|
||||
|
||||
Args:
|
||||
source_type: Typ der Quell-Sektion (z.B. "experience")
|
||||
target_type: Typ der Ziel-Sektion (z.B. "insight")
|
||||
|
||||
Returns:
|
||||
Dict mit "typical" und "prohibited" Listen
|
||||
Beispiel: {"typical": ["resulted_in"], "prohibited": ["solves"]}
|
||||
"""
|
||||
schema = load_graph_schema_full()
|
||||
|
||||
source_lower = source_type.lower() if source_type else "default"
|
||||
target_lower = target_type.lower() if target_type else "any"
|
||||
|
||||
result = {"typical": [], "prohibited": []}
|
||||
|
||||
# 1. Exakter Match
|
||||
if source_lower in schema and target_lower in schema[source_lower]:
|
||||
return schema[source_lower][target_lower]
|
||||
|
||||
# 2. Fallback auf "any" Target
|
||||
if source_lower in schema and "any" in schema[source_lower]:
|
||||
return schema[source_lower]["any"]
|
||||
|
||||
# 3. Fallback auf "default" Source
|
||||
if "default" in schema:
|
||||
if target_lower in schema["default"]:
|
||||
return schema["default"][target_lower]
|
||||
if "any" in schema["default"]:
|
||||
return schema["default"]["any"]
|
||||
|
||||
# 4. Absoluter Fallback: alles erlaubt
|
||||
return {"typical": ["related_to", "references"], "prohibited": []}
|
||||
|
||||
def get_typical_edge_for(source_type: str, target_type: str) -> Optional[str]:
|
||||
"""
|
||||
WP-26 v1.1: Ermittelt den ersten "Typical Edge-Type" für ein Typ-Paar.
|
||||
@@ -424,6 +493,8 @@ def clear_graph_schema_cache():
|
||||
"""
|
||||
WP-26 v1.1: Löscht den Cache für das Graph-Schema.
|
||||
Nützlich für Tests oder wenn das Schema neu geladen werden soll.
|
||||
WP-26 v1.3: Löscht auch den erweiterten Schema-Cache.
|
||||
"""
|
||||
global _GRAPH_SCHEMA_CACHE
|
||||
_GRAPH_SCHEMA_CACHE = None
|
||||
global _GRAPH_SCHEMA_CACHE, _GRAPH_SCHEMA_FULL_CACHE
|
||||
_GRAPH_SCHEMA_CACHE = None
|
||||
_GRAPH_SCHEMA_FULL_CACHE = None
|
||||
Reference in new issue
Block a user