qualia_core_db/research/
inference_chain.rs1#[derive(Debug, Clone)]
5pub struct InferenceStep {
6 pub id: String,
7 pub premise: String,
8 pub conclusion: String,
9 pub confidence: f64,
10 pub validated: bool,
11}
12
13impl InferenceStep {
14 pub fn new(id: &str, premise: &str, conclusion: &str, confidence: f64) -> Self {
16 Self {
17 id: id.to_string(),
18 premise: premise.to_string(),
19 conclusion: conclusion.to_string(),
20 confidence: confidence.clamp(0.0, 1.0),
21 validated: false,
22 }
23 }
24
25 pub fn validate(&mut self) {
27 self.validated = true;
28 }
29}
30
31#[derive(Debug, Clone)]
33pub struct InferenceChain {
34 pub id: String,
35 pub steps: Vec<InferenceStep>,
36}
37
38impl InferenceChain {
39 pub fn new(id: &str) -> Self {
41 Self {
42 id: id.to_string(),
43 steps: Vec::new(),
44 }
45 }
46
47 pub fn make_inference(&mut self, step: InferenceStep) {
49 self.steps.push(step);
50 }
51
52 pub fn chain_inference(&mut self, id: &str, new_conclusion: &str, confidence: f64) -> bool {
54 let premise = self.steps.last().map(|s| s.conclusion.clone());
55 if let Some(premise) = premise {
56 self.steps
57 .push(InferenceStep::new(id, &premise, new_conclusion, confidence));
58 true
59 } else {
60 false
61 }
62 }
63
64 pub fn set_confidence(&mut self, step_id: &str, confidence: f64) -> bool {
66 if let Some(step) = self.steps.iter_mut().find(|s| s.id == step_id) {
67 step.confidence = confidence.clamp(0.0, 1.0);
68 true
69 } else {
70 false
71 }
72 }
73
74 pub fn validate(&mut self, step_id: &str) -> bool {
76 if let Some(step) = self.steps.iter_mut().find(|s| s.id == step_id) {
77 step.validate();
78 true
79 } else {
80 false
81 }
82 }
83
84 pub fn combined_confidence(&self) -> f64 {
86 if self.steps.is_empty() {
87 return 0.0;
88 }
89 self.steps.iter().map(|s| s.confidence).product()
90 }
91
92 pub fn len(&self) -> usize {
94 self.steps.len()
95 }
96
97 pub fn is_empty(&self) -> bool {
99 self.steps.is_empty()
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106
107 #[test]
108 fn chain_basic() {
109 let mut chain = InferenceChain::new("ch1");
110 chain.make_inference(InferenceStep::new("s1", "A is true", "B is likely", 0.8));
111 assert_eq!(chain.len(), 1);
112 }
113
114 #[test]
115 fn chain_inference_links() {
116 let mut chain = InferenceChain::new("ch1");
117 chain.make_inference(InferenceStep::new("s1", "A", "B", 0.8));
118 assert!(chain.chain_inference("s2", "C", 0.7));
119 assert_eq!(chain.steps[1].premise, "B");
120 }
121
122 #[test]
123 fn chain_combined_confidence() {
124 let mut chain = InferenceChain::new("ch1");
125 chain.make_inference(InferenceStep::new("s1", "A", "B", 0.8));
126 chain.chain_inference("s2", "C", 0.5);
127 let combined = chain.combined_confidence();
128 assert!((combined - 0.4).abs() < 0.01);
129 }
130
131 #[test]
132 fn chain_validate_step() {
133 let mut chain = InferenceChain::new("ch1");
134 chain.make_inference(InferenceStep::new("s1", "A", "B", 0.8));
135 assert!(chain.validate("s1"));
136 assert!(chain.steps[0].validated);
137 }
138
139 #[test]
140 fn chain_set_confidence() {
141 let mut chain = InferenceChain::new("ch1");
142 chain.make_inference(InferenceStep::new("s1", "A", "B", 0.8));
143 chain.set_confidence("s1", 0.5);
144 assert!((chain.steps[0].confidence - 0.5).abs() < 0.01);
145 }
146
147 #[test]
148 fn chain_empty_confidence() {
149 let chain = InferenceChain::new("ch1");
150 assert_eq!(chain.combined_confidence(), 0.0);
151 }
152}