Spaces:
Running
Running
| """ | |
| MODULE: ONTOLOGY BLOCKS BUILDER (WITH LINEAGE) | |
| ============================================== | |
| Responsabilité : Construire les dossiers contextuels avec traçabilité (Source). | |
| """ | |
| import pandas as pd | |
| class OntologyBlocksBuilder: | |
| def __init__(self, rdf_store): | |
| self.rdf = rdf_store | |
| def build_all_blocks(self): | |
| """Génère des dossiers contextuels avec métadonnées de provenance""" | |
| g = self.rdf.g | |
| blocks = [] | |
| q_entities = """ | |
| SELECT DISTINCT ?s ?type ?label | |
| WHERE { ?s a ?type . ?s rdfs:label ?label . FILTER(?type != vortex:Transaction) } | |
| """ | |
| for row in g.query(q_entities): | |
| uri = row[0] | |
| entity_type = str(row[1]).split('#')[-1] | |
| label = str(row[2]) | |
| # --- HEADER AVEC LINEAGE (La Nouveauté) --- | |
| dossier_text = [f"📂 DOSSIER {entity_type.upper()}: {label}"] | |
| dossier_text.append(f"🆔 URI: {str(uri).split('#')[-1]}") | |
| dossier_text.append(f"📡 SOURCE: Ontology Gold Record (Validé par Pipeline)") | |
| # --- PROPRIÉTÉS (A-Box) --- | |
| q_props = f"SELECT ?p ?o WHERE {{ <{str(uri)}> ?p ?o . FILTER(!isURI(?o)) }}" | |
| for p, o in g.query(q_props): | |
| pred = str(p).split('#')[-1] | |
| dossier_text.append(f" 🔹 {pred}: {str(o)}") | |
| # --- RELATIONS SORTANTES (Smart Links) --- | |
| q_out = f"SELECT ?p ?target_lbl WHERE {{ <{str(uri)}> ?p ?t . ?t rdfs:label ?target_lbl . FILTER(isURI(?t)) }}" | |
| for p, t_lbl in g.query(q_out): | |
| dossier_text.append(f" 👉 {str(p).split('#')[-1]} -> {t_lbl}") | |
| # --- RELATIONS ENTRANTES (R-Box Context) --- | |
| q_in = f"SELECT ?src_lbl ?p WHERE {{ ?s ?p <{str(uri)}> . ?s rdfs:label ?src_lbl . FILTER(?p != rdf:type) }}" | |
| incoming = [f"{src} ({p.split('#')[-1]})" for src, p in g.query(q_in)] | |
| if incoming: | |
| dossier_text.append(f" 🔗 CONTEXTE LIÉ : {', '.join(incoming)}") | |
| blocks.append({ | |
| "uri": str(uri), | |
| "text": "\n".join(dossier_text), | |
| "metadata": { | |
| "type": entity_type, | |
| "label": label, | |
| "source": "Vortex_Ontology" # Méta-donnée pour le Lineage | |
| } | |
| }) | |
| print(f"✅ [LINEAGE] {len(blocks)} blocs générés avec traçabilité.") | |
| return blocks |