Spaces:
Running
Running
Update src/core/inference_engine.py
Browse files- src/core/inference_engine.py +19 -37
src/core/inference_engine.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
MODULE: INFERENCE ENGINE
|
| 3 |
-
========================
|
| 4 |
-
Mise à jour : Règle 'HighValue' rendue plus robuste aux variations de noms de colonnes.
|
| 5 |
"""
|
| 6 |
from rdflib import Graph, Namespace
|
| 7 |
|
|
@@ -12,49 +11,32 @@ class InferenceEngine:
|
|
| 12 |
self.rdf_store = rdf_store
|
| 13 |
|
| 14 |
def run_inference(self):
|
| 15 |
-
print("🧠 [R-BOX]
|
| 16 |
initial_count = len(self.rdf_store.g)
|
| 17 |
|
| 18 |
-
# 1. Réciprocité
|
| 19 |
-
|
| 20 |
CONSTRUCT { ?pret vortex:a_emprunteur ?client . }
|
| 21 |
WHERE { ?client vortex:possede_pret ?pret . }
|
| 22 |
-
"""
|
| 23 |
-
self._apply_rule(rule_inverse, "Inverse: possede_pret -> a_emprunteur")
|
| 24 |
|
| 25 |
-
# 2. Règle
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
CONSTRUCT {
|
| 29 |
-
?pret vortex:statut_risque "HighValue" .
|
| 30 |
-
}
|
| 31 |
WHERE {
|
| 32 |
-
?pret a
|
| 33 |
?pret ?p ?valeur .
|
| 34 |
-
FILTER(
|
| 35 |
-
(CONTAINS(LCASE(STR(?p)), "montant") || CONTAINS(LCASE(STR(?p)), "capital"))
|
| 36 |
-
&&
|
| 37 |
-
xsd:decimal(?valeur) > 1000000
|
| 38 |
-
)
|
| 39 |
}
|
| 40 |
-
"""
|
| 41 |
-
self._apply_rule(rule_risk, "Logique: Prêt Valeur Élevée (>1M)")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
print(f"✅ [R-BOX] Inférence terminée. {final_count - initial_count} nouveaux faits.")
|
| 45 |
|
| 46 |
-
def _apply_rule(self,
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
{sparql_construct}
|
| 52 |
-
"""
|
| 53 |
-
inferred_triples = self.rdf_store.g.query(full_query)
|
| 54 |
-
count = 0
|
| 55 |
-
for triple in inferred_triples:
|
| 56 |
-
self.rdf_store.g.add(triple)
|
| 57 |
-
count += 1
|
| 58 |
-
if count > 0: print(f" 🔹 Règle '{rule_name}' : +{count} triplets.")
|
| 59 |
except Exception as e:
|
| 60 |
-
print(f"
|
|
|
|
| 1 |
"""
|
| 2 |
+
MODULE: INFERENCE ENGINE
|
| 3 |
+
========================
|
|
|
|
| 4 |
"""
|
| 5 |
from rdflib import Graph, Namespace
|
| 6 |
|
|
|
|
| 11 |
self.rdf_store = rdf_store
|
| 12 |
|
| 13 |
def run_inference(self):
|
| 14 |
+
print("🧠 [R-BOX] Inférence...")
|
| 15 |
initial_count = len(self.rdf_store.g)
|
| 16 |
|
| 17 |
+
# 1. Réciprocité
|
| 18 |
+
self._apply_rule("""
|
| 19 |
CONSTRUCT { ?pret vortex:a_emprunteur ?client . }
|
| 20 |
WHERE { ?client vortex:possede_pret ?pret . }
|
| 21 |
+
""", "Inverse: possede_pret")
|
|
|
|
| 22 |
|
| 23 |
+
# 2. Règle HighValue (ROBUSTE)
|
| 24 |
+
# Cherche n'importe quelle propriété numérique > 1M
|
| 25 |
+
self._apply_rule("""
|
| 26 |
+
CONSTRUCT { ?pret vortex:statut_risque "HighValue" . }
|
|
|
|
|
|
|
| 27 |
WHERE {
|
| 28 |
+
?pret a vortex:Pret .
|
| 29 |
?pret ?p ?valeur .
|
| 30 |
+
FILTER(isNumeric(?valeur) && ?valeur > 1000000)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
+
""", "Risque: HighValue > 1M")
|
|
|
|
| 33 |
|
| 34 |
+
print(f"✅ [R-BOX] +{len(self.rdf_store.g) - initial_count} faits déduits.")
|
|
|
|
| 35 |
|
| 36 |
+
def _apply_rule(self, sparql, name):
|
| 37 |
try:
|
| 38 |
+
full = f"PREFIX vortex: <http://vortex.ai/ontology#>\nPREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n{sparql}"
|
| 39 |
+
res = self.rdf_store.g.query(full)
|
| 40 |
+
for t in res: self.rdf_store.g.add(t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
+
print(f"❌ Erreur Règle '{name}' : {e}")
|