forked from KSD-CO/rust-rule-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproof_graph_integration_test.rs
More file actions
220 lines (178 loc) · 6.27 KB
/
Copy pathproof_graph_integration_test.rs
File metadata and controls
220 lines (178 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Integration test for ProofGraph caching with backward chaining
//!
//! This test demonstrates how ProofGraph caches proven facts across
//! multiple queries, avoiding re-exploration when facts are already known.
#[cfg(feature = "backward-chaining")]
use rust_rule_engine::backward::{BackwardEngine, FactKey, ProofGraph};
use rust_rule_engine::{Facts, KnowledgeBase, Value};
#[test]
#[cfg(feature = "backward-chaining")]
fn test_proof_graph_caching_basic() {
// Create knowledge base with rules
let kb = KnowledgeBase::new("test_kb");
// Create facts
let mut facts = Facts::new();
facts.set("User.Age", Value::Integer(25));
// Create backward engine
let mut engine = BackwardEngine::new(kb);
// First query - should explore and prove
let _result1 = engine.query("User.Age >= 18", &mut facts).unwrap();
// Note: ProofGraph is internal to search; this test verifies behavior
// In a real scenario with IncrementalEngine, cache would be populated
}
#[test]
#[cfg(feature = "backward-chaining")]
fn test_proof_graph_invalidation() {
// Test that demonstrates proof graph structure
let mut graph = ProofGraph::new();
use rust_rule_engine::rete::FactHandle;
// Create fact handles
let premise_handle = FactHandle::new(1);
let conclusion_handle = FactHandle::new(2);
// Create fact keys
let premise_key = FactKey::from_pattern("User.Age >= 18");
let conclusion_key = FactKey::from_pattern("User.CanVote == true");
// Insert premise proof
graph.insert_proof(
premise_handle,
premise_key.clone(),
"AgeFactRule".to_string(),
vec![],
vec![],
);
// Insert conclusion proof depending on premise
graph.insert_proof(
conclusion_handle,
conclusion_key.clone(),
"VotingRule".to_string(),
vec![premise_handle],
vec!["User.Age >= 18".to_string()],
);
// Both should be proven
assert!(graph.is_proven(&premise_key));
assert!(graph.is_proven(&conclusion_key));
// Invalidate premise (simulating retraction by TMS)
graph.invalidate_handle(&premise_handle);
// Conclusion should now be invalid
let conclusion_node = graph.get_node(&conclusion_handle).unwrap();
assert!(!conclusion_node.valid);
// Stats should show invalidations
assert_eq!(graph.stats.invalidations, 2); // premise + dependent
}
#[test]
#[cfg(feature = "backward-chaining")]
fn test_proof_graph_multiple_justifications() {
let mut graph = ProofGraph::new();
use rust_rule_engine::rete::FactHandle;
let handle = FactHandle::new(1);
let key = FactKey::from_pattern("User.IsVIP == true");
// Add first justification (high spender)
graph.insert_proof(
handle,
key.clone(),
"HighSpenderRule".to_string(),
vec![],
vec![],
);
// Add second justification (loyalty points)
graph.insert_proof(
handle,
key.clone(),
"LoyaltyRule".to_string(),
vec![],
vec![],
);
let node = graph.get_node(&handle).unwrap();
assert_eq!(node.justifications.len(), 2);
assert!(node.valid);
// Fact should remain valid even if one justification is removed
// (in a full TMS, this would be tracked)
}
#[test]
#[cfg(feature = "backward-chaining")]
fn test_proof_graph_cache_statistics() {
let mut graph = ProofGraph::new();
let key = FactKey::from_pattern("User.Active == true");
// First lookup - cache miss
assert!(!graph.is_proven(&key));
assert_eq!(graph.stats.cache_misses, 1);
assert_eq!(graph.stats.cache_hits, 0);
// Insert proof
use rust_rule_engine::rete::FactHandle;
let handle = FactHandle::new(1);
graph.insert_proof(
handle,
key.clone(),
"ActiveRule".to_string(),
vec![],
vec![],
);
// Second lookup - cache hit
assert!(graph.is_proven(&key));
assert_eq!(graph.stats.cache_hits, 1);
assert_eq!(graph.stats.cache_misses, 1);
// Third lookup - another hit
assert!(graph.is_proven(&key));
assert_eq!(graph.stats.cache_hits, 2);
// Calculate hit rate
let total = graph.stats.cache_hits + graph.stats.cache_misses;
let hit_rate = (graph.stats.cache_hits as f64) / (total as f64);
assert!(hit_rate > 0.66); // 2 hits out of 3 lookups
}
#[test]
#[cfg(feature = "backward-chaining")]
fn test_fact_key_parsing() {
// Test various pattern formats
let key1 = FactKey::from_pattern("User.Score >= 80");
assert_eq!(key1.fact_type, "User");
assert_eq!(key1.field, Some("Score".to_string()));
let key2 = FactKey::from_pattern("Order.Status == \"shipped\"");
assert_eq!(key2.fact_type, "Order");
assert_eq!(key2.field, Some("Status".to_string()));
let key3 = FactKey::from_pattern("SimplePattern");
assert_eq!(key3.fact_type, "SimplePattern");
assert_eq!(key3.field, None);
}
#[test]
#[cfg(feature = "backward-chaining")]
fn test_proof_graph_dependency_propagation() {
let mut graph = ProofGraph::new();
use rust_rule_engine::rete::FactHandle;
// Create a chain: A -> B -> C
let handle_a = FactHandle::new(1);
let handle_b = FactHandle::new(2);
let handle_c = FactHandle::new(3);
let key_a = FactKey::from_pattern("A == true");
let key_b = FactKey::from_pattern("B == true");
let key_c = FactKey::from_pattern("C == true");
// Insert A
graph.insert_proof(handle_a, key_a.clone(), "RuleA".to_string(), vec![], vec![]);
// Insert B (depends on A)
graph.insert_proof(
handle_b,
key_b.clone(),
"RuleB".to_string(),
vec![handle_a],
vec!["A == true".to_string()],
);
// Insert C (depends on B)
graph.insert_proof(
handle_c,
key_c.clone(),
"RuleC".to_string(),
vec![handle_b],
vec!["B == true".to_string()],
);
// All should be valid
assert!(graph.is_proven(&key_a));
assert!(graph.is_proven(&key_b));
assert!(graph.is_proven(&key_c));
// Invalidate A - should cascade to B and C
graph.invalidate_handle(&handle_a);
let node_b = graph.get_node(&handle_b).unwrap();
let node_c = graph.get_node(&handle_c).unwrap();
assert!(!node_b.valid);
assert!(!node_c.valid);
// Should have invalidated 3 nodes total
assert_eq!(graph.stats.invalidations, 3);
}