klydekushy commited on
Commit
7a0bb61
·
verified ·
1 Parent(s): f42a19f

Update src/core/inference_engine.py

Browse files
Files changed (1) hide show
  1. src/core/inference_engine.py +48 -21
src/core/inference_engine.py CHANGED
@@ -1,8 +1,9 @@
1
  """
2
- MODULE: INFERENCE ENGINE
3
- ========================
 
4
  """
5
- from rdflib import Graph, Namespace
6
 
7
  VORTEX = Namespace("http://vortex.ai/ontology#")
8
 
@@ -11,32 +12,58 @@ class InferenceEngine:
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}")
 
1
  """
2
+ MODULE: INFERENCE ENGINE (FORCE BRUTE)
3
+ ======================================
4
+ Correction : Force le typage numérique pour trouver les HighValue.
5
  """
6
+ from rdflib import Graph, Namespace, Literal, XSD
7
 
8
  VORTEX = Namespace("http://vortex.ai/ontology#")
9
 
 
12
  self.rdf_store = rdf_store
13
 
14
  def run_inference(self):
15
+ print("🧠 [R-BOX] Inférence (Mode Force Brute)...")
16
  initial_count = len(self.rdf_store.g)
17
 
18
+ # 1. HighValue : On force la conversion en Decimal pour la comparaison
19
+ # On cherche tout ce qui ressemble à un montant et qui dépasse 1 million
20
+ rule_risk = """
21
+ CONSTRUCT {
22
+ ?pret vortex:statut_risque "HighValue" .
23
+ }
24
+ WHERE {
25
+ ?pret a ?type .
26
+ ?pret ?p ?val_raw .
27
+
28
+ # Détection large des colonnes montant/capital
29
+ FILTER(CONTAINS(LCASE(STR(?p)), "montant") || CONTAINS(LCASE(STR(?p)), "capital"))
30
+
31
+ # Conversion sécurisée et test
32
+ BIND(xsd:decimal(?val_raw) AS ?val_num)
33
+ FILTER(?val_num > 1000000)
34
+ }
35
+ """
36
+ self._apply_rule(rule_risk, "Détection Risque (>1M)")
37
 
38
+ # 2. Relations Inverses (Pour que Koulnan soit relié au prêt)
39
+ # Si A 'possede_pret' B, alors B 'a_emprunteur' A
40
+ rule_inverse = """
41
+ CONSTRUCT {
42
+ ?pret vortex:a_emprunteur ?client_label .
43
+ }
44
  WHERE {
45
+ ?client vortex:possede_pret ?pret .
46
+ ?client rdfs:label ?client_label .
 
47
  }
48
+ """
49
+ self._apply_rule(rule_inverse, "Lien Inverse (Client <-> Pret)")
50
 
51
+ final = len(self.rdf_store.g)
52
+ print(f"✅ [R-BOX] Terminé. {final - initial_count} nouveaux faits générés.")
53
 
54
  def _apply_rule(self, sparql, name):
55
  try:
56
+ full = f"""
57
+ PREFIX vortex: <http://vortex.ai/ontology#>
58
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
59
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
60
+ {sparql}
61
+ """
62
  res = self.rdf_store.g.query(full)
63
+ c = 0
64
+ for t in res:
65
+ self.rdf_store.g.add(t)
66
+ c += 1
67
+ if c > 0: print(f" 🔹 {name} : +{c} faits.")
68
  except Exception as e:
69
+ print(f" ❌ Erreur {name} : {e}")