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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Incremental Reasoning Engine
//!
//! Provides efficient incremental reasoning by computing only the delta (new facts)
//! when the knowledge base is updated, rather than recomputing everything from scratch.
//!
//! # Features
//!
//! - **Delta Computation**: Track changes and compute only affected inferences
//! - **Dependency Tracking**: Maintain dependencies between facts and rules
//! - **Efficient Updates**: Add/remove facts with minimal recomputation
//! - **Undo Support**: Rollback to previous states
//!
//! # Example
//!
//! ```rust
//! use oxirs_rule::incremental::IncrementalReasoner;
//! use oxirs_rule::{Rule, RuleAtom, Term};
//!
//! let mut reasoner = IncrementalReasoner::new();
//!
//! // Add rule
//! reasoner.add_rule(Rule {
//! name: "mortal".to_string(),
//! body: vec![RuleAtom::Triple {
//! subject: Term::Variable("X".to_string()),
//! predicate: Term::Constant("type".to_string()),
//! object: Term::Constant("human".to_string()),
//! }],
//! head: vec![RuleAtom::Triple {
//! subject: Term::Variable("X".to_string()),
//! predicate: Term::Constant("type".to_string()),
//! object: Term::Constant("mortal".to_string()),
//! }],
//! });
//!
//! // Add fact incrementally
//! let delta = reasoner.add_fact(RuleAtom::Triple {
//! subject: Term::Constant("socrates".to_string()),
//! predicate: Term::Constant("type".to_string()),
//! object: Term::Constant("human".to_string()),
//! }).expect("should succeed");
//!
//! // Only newly derived facts are returned
//! assert!(!delta.is_empty());
//! ```
use crate::{Rule, RuleAtom, Term};
use anyhow::Result;
use scirs2_core::metrics::Counter;
use std::collections::{HashMap, HashSet, VecDeque};
use tracing::{debug, info, trace};
// Global metrics for memory tracking
lazy_static::lazy_static! {
static ref INCREMENTAL_SUBSTITUTION_CLONES: Counter = Counter::new("incremental_substitution_clones".to_string());
static ref INCREMENTAL_RULE_CLONES: Counter = Counter::new("incremental_rule_clones".to_string());
}
/// Fact identifier for dependency tracking
type FactId = usize;
/// Rule identifier for dependency tracking
type RuleId = usize;
/// Dependency information for a derived fact
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct FactDependency {
/// Facts that this fact depends on
depends_on: HashSet<FactId>,
/// Rule that derived this fact
derived_by: Option<RuleId>,
/// Generation when this fact was added
generation: usize,
}
/// Incremental reasoning engine
#[derive(Debug)]
pub struct IncrementalReasoner {
/// All known facts (indexed by ID)
facts: HashMap<FactId, RuleAtom>,
/// Reverse index: fact content -> fact ID
fact_index: HashMap<RuleAtom, FactId>,
/// Next fact ID
next_fact_id: FactId,
/// All rules (indexed by ID)
rules: HashMap<RuleId, Rule>,
/// Next rule ID
next_rule_id: RuleId,
/// Dependencies for each fact
dependencies: HashMap<FactId, FactDependency>,
/// Reverse dependencies: which facts depend on this fact
reverse_dependencies: HashMap<FactId, HashSet<FactId>>,
/// Facts that are explicitly asserted (not derived)
asserted_facts: HashSet<FactId>,
/// Facts that were derived (not asserted)
derived_facts: HashSet<FactId>,
/// Current generation counter
generation: usize,
/// Snapshot history for undo support
snapshots: Vec<Snapshot>,
/// Maximum derivation depth to prevent infinite loops
max_depth: usize,
}
/// Snapshot of reasoner state for undo support
#[derive(Debug, Clone)]
struct Snapshot {
generation: usize,
asserted_facts: HashSet<FactId>,
derived_facts: HashSet<FactId>,
}
impl Default for IncrementalReasoner {
fn default() -> Self {
Self::new()
}
}
impl IncrementalReasoner {
/// Create a new incremental reasoner
pub fn new() -> Self {
Self {
facts: HashMap::new(),
fact_index: HashMap::new(),
next_fact_id: 0,
rules: HashMap::new(),
next_rule_id: 0,
dependencies: HashMap::new(),
reverse_dependencies: HashMap::new(),
asserted_facts: HashSet::new(),
derived_facts: HashSet::new(),
generation: 0,
snapshots: Vec::new(),
max_depth: 100,
}
}
/// Create a new incremental reasoner with custom configuration
pub fn with_config(max_depth: usize) -> Self {
Self {
max_depth,
..Self::new()
}
}
/// Add a rule to the reasoner
pub fn add_rule(&mut self, rule: Rule) -> RuleId {
let rule_id = self.next_rule_id;
self.next_rule_id += 1;
self.rules.insert(rule_id, rule);
debug!("Added rule with ID {}", rule_id);
rule_id
}
/// Add a fact incrementally and return newly derived facts (delta)
pub fn add_fact(&mut self, fact: RuleAtom) -> Result<Vec<RuleAtom>> {
self.generation += 1;
// Check if fact already exists
if let Some(&fact_id) = self.fact_index.get(&fact) {
trace!("Fact already exists with ID {}", fact_id);
return Ok(vec![]);
}
// Add the fact
let fact_id = self.add_fact_internal(fact.clone(), None, HashSet::new())?;
self.asserted_facts.insert(fact_id);
// Compute delta (newly derived facts)
let delta = self.compute_delta_from_fact(fact_id)?;
info!("Added fact {} derived {} new facts", fact_id, delta.len());
Ok(delta)
}
/// Remove a fact and all facts that depend on it
pub fn remove_fact(&mut self, fact: &RuleAtom) -> Result<Vec<RuleAtom>> {
self.generation += 1;
// Find fact ID
let fact_id = match self.fact_index.get(fact) {
Some(&id) => id,
None => {
trace!("Fact not found for removal");
return Ok(vec![]);
}
};
// Can only remove asserted facts
if !self.asserted_facts.contains(&fact_id) {
return Err(anyhow::anyhow!(
"Cannot remove derived fact {}; remove its dependencies instead",
fact_id
));
}
// Find all facts that depend on this fact (transitively)
let affected_facts = self.find_affected_facts(fact_id);
// Remove all affected facts
let mut removed = Vec::new();
for affected_id in affected_facts {
if let Some(fact) = self.facts.remove(&affected_id) {
self.fact_index.remove(&fact);
self.dependencies.remove(&affected_id);
self.reverse_dependencies.remove(&affected_id);
self.asserted_facts.remove(&affected_id);
self.derived_facts.remove(&affected_id);
removed.push(fact);
}
}
info!("Removed {} facts", removed.len());
Ok(removed)
}
/// Create a snapshot for undo support
pub fn create_snapshot(&mut self) {
let snapshot = Snapshot {
generation: self.generation,
asserted_facts: self.asserted_facts.clone(),
derived_facts: self.derived_facts.clone(),
};
self.snapshots.push(snapshot);
debug!("Created snapshot at generation {}", self.generation);
}
/// Restore to the last snapshot
pub fn restore_snapshot(&mut self) -> Result<()> {
let snapshot = self
.snapshots
.pop()
.ok_or_else(|| anyhow::anyhow!("No snapshot available to restore"))?;
// Remove facts that were added after the snapshot
let facts_to_remove: Vec<FactId> = self
.facts
.keys()
.filter(|&id| {
!snapshot.asserted_facts.contains(id) && !snapshot.derived_facts.contains(id)
})
.copied()
.collect();
for fact_id in facts_to_remove {
if let Some(fact) = self.facts.remove(&fact_id) {
self.fact_index.remove(&fact);
self.dependencies.remove(&fact_id);
self.reverse_dependencies.remove(&fact_id);
}
}
self.generation = snapshot.generation;
self.asserted_facts = snapshot.asserted_facts;
self.derived_facts = snapshot.derived_facts;
info!("Restored snapshot to generation {}", self.generation);
Ok(())
}
/// Get all current facts
pub fn get_facts(&self) -> Vec<RuleAtom> {
self.facts.values().cloned().collect()
}
/// Get statistics about the reasoner
pub fn get_stats(&self) -> IncrementalStats {
IncrementalStats {
total_facts: self.facts.len(),
asserted_facts: self.asserted_facts.len(),
derived_facts: self.derived_facts.len(),
total_rules: self.rules.len(),
generation: self.generation,
snapshots: self.snapshots.len(),
}
}
// Internal methods
/// Add a fact internally with dependency tracking
fn add_fact_internal(
&mut self,
fact: RuleAtom,
derived_by: Option<RuleId>,
depends_on: HashSet<FactId>,
) -> Result<FactId> {
let fact_id = self.next_fact_id;
self.next_fact_id += 1;
// Store fact
self.facts.insert(fact_id, fact.clone());
self.fact_index.insert(fact, fact_id);
// Store dependency
self.dependencies.insert(
fact_id,
FactDependency {
depends_on: depends_on.clone(),
derived_by,
generation: self.generation,
},
);
// Update reverse dependencies
for dep_id in depends_on {
self.reverse_dependencies
.entry(dep_id)
.or_default()
.insert(fact_id);
}
Ok(fact_id)
}
/// Compute delta (newly derived facts) from adding a fact
fn compute_delta_from_fact(&mut self, new_fact_id: FactId) -> Result<Vec<RuleAtom>> {
let mut delta = Vec::new();
let mut queue = VecDeque::new();
queue.push_back((new_fact_id, 0));
let mut processed = HashSet::new();
while let Some((fact_id, depth)) = queue.pop_front() {
if depth >= self.max_depth {
continue;
}
if !processed.insert(fact_id) {
continue;
}
// OPTIMIZATION: Collect only rule IDs and body/head references instead of cloning entire rules
let rules_to_process: Vec<(RuleId, Vec<RuleAtom>, Vec<RuleAtom>)> = self
.rules
.iter()
.map(|(&id, rule)| {
INCREMENTAL_RULE_CLONES.inc();
(id, rule.body.clone(), rule.head.clone())
})
.collect();
// Try to apply each rule
for (rule_id, body, head) in rules_to_process {
let derived = self.try_apply_rule_parts(rule_id, &body, &head, fact_id)?;
for new_fact in derived {
// Check if this fact already exists
if self.fact_index.contains_key(&new_fact) {
continue;
}
// Add the derived fact
let new_id = self.add_fact_internal(
new_fact.clone(),
Some(rule_id),
[fact_id].iter().copied().collect(),
)?;
self.derived_facts.insert(new_id);
delta.push(new_fact);
queue.push_back((new_id, depth + 1));
}
}
}
Ok(delta)
}
/// Try to apply a rule given a new fact (takes body and head separately)
/// OPTIMIZED: Takes body/head instead of entire Rule to avoid cloning rule names
fn try_apply_rule_parts(
&self,
_rule_id: RuleId,
body: &[RuleAtom],
head: &[RuleAtom],
_new_fact_id: FactId,
) -> Result<Vec<RuleAtom>> {
// This is a simplified implementation
// In a full implementation, we would:
// 1. Check if the new fact matches any atom in the rule body
// 2. Find all possible variable bindings
// 3. Check if all other atoms in the body are satisfied
// 4. Generate head atoms with the bindings
// For now, we'll use a simpler forward-chaining approach
let mut derived = Vec::new();
// Try to find substitutions that satisfy the rule body
let substitutions = self.find_substitutions(body)?;
for substitution in substitutions {
// Apply substitution to head
for head_atom in head {
let instantiated = self.apply_substitution(head_atom, &substitution)?;
derived.push(instantiated);
}
}
Ok(derived)
}
/// Find all substitutions that satisfy the rule body
fn find_substitutions(&self, body: &[RuleAtom]) -> Result<Vec<HashMap<String, Term>>> {
if body.is_empty() {
return Ok(vec![HashMap::new()]);
}
// Start with the first atom
let mut substitutions = self.match_atom(&body[0], &HashMap::new())?;
// Extend with remaining atoms
for atom in &body[1..] {
let mut new_substitutions = Vec::new();
for sub in substitutions {
let extended = self.match_atom(atom, &sub)?;
new_substitutions.extend(extended);
}
substitutions = new_substitutions;
}
Ok(substitutions)
}
/// Match an atom against known facts
/// OPTIMIZED: Pass reference to unify_triple instead of cloning
fn match_atom(
&self,
atom: &RuleAtom,
partial_sub: &HashMap<String, Term>,
) -> Result<Vec<HashMap<String, Term>>> {
let mut substitutions = Vec::new();
match atom {
RuleAtom::Triple {
subject,
predicate,
object,
} => {
for fact in self.facts.values() {
if let RuleAtom::Triple {
subject: fs,
predicate: fp,
object: fo,
} = fact
{
// OPTIMIZATION: Pass reference - only clone on unification success
if let Some(sub) = self.unify_triple(
(subject, predicate, object),
(fs, fp, fo),
partial_sub, // Reference instead of clone!
)? {
substitutions.push(sub);
}
}
}
}
_ => {
// Handle other atom types
}
}
Ok(substitutions)
}
/// Unify two triples
/// OPTIMIZED: Takes reference, only clones on success
fn unify_triple(
&self,
pattern: (&Term, &Term, &Term),
fact: (&Term, &Term, &Term),
substitution: &HashMap<String, Term>,
) -> Result<Option<HashMap<String, Term>>> {
// Clone once at start - only this clone survives if unification succeeds
let mut new_substitution = substitution.clone();
if !self.unify_terms(pattern.0, fact.0, &mut new_substitution)? {
return Ok(None);
}
if !self.unify_terms(pattern.1, fact.1, &mut new_substitution)? {
return Ok(None);
}
if !self.unify_terms(pattern.2, fact.2, &mut new_substitution)? {
return Ok(None);
}
// Track successful unification (only clones that survive)
INCREMENTAL_SUBSTITUTION_CLONES.inc();
Ok(Some(new_substitution))
}
/// Unify two terms
fn unify_terms(
&self,
pattern: &Term,
fact: &Term,
substitution: &mut HashMap<String, Term>,
) -> Result<bool> {
match (pattern, fact) {
(Term::Variable(var), fact_term) => {
if let Some(existing) = substitution.get(var) {
Ok(existing == fact_term)
} else {
substitution.insert(var.clone(), fact_term.clone());
Ok(true)
}
}
(Term::Constant(c1), Term::Constant(c2)) => Ok(c1 == c2),
(Term::Literal(l1), Term::Literal(l2)) => Ok(l1 == l2),
_ => Ok(false),
}
}
/// Apply substitution to an atom
fn apply_substitution(
&self,
atom: &RuleAtom,
substitution: &HashMap<String, Term>,
) -> Result<RuleAtom> {
match atom {
RuleAtom::Triple {
subject,
predicate,
object,
} => Ok(RuleAtom::Triple {
subject: self.substitute_term(subject, substitution),
predicate: self.substitute_term(predicate, substitution),
object: self.substitute_term(object, substitution),
}),
_ => Ok(atom.clone()),
}
}
/// Substitute variables in a term
fn substitute_term(&self, term: &Term, substitution: &HashMap<String, Term>) -> Term {
match term {
Term::Variable(var) => substitution
.get(var)
.cloned()
.unwrap_or_else(|| term.clone()),
_ => term.clone(),
}
}
/// Find all facts that depend on the given fact (transitively)
fn find_affected_facts(&self, fact_id: FactId) -> HashSet<FactId> {
let mut affected = HashSet::new();
let mut queue = VecDeque::new();
queue.push_back(fact_id);
while let Some(id) = queue.pop_front() {
if !affected.insert(id) {
continue;
}
if let Some(dependents) = self.reverse_dependencies.get(&id) {
for &dependent_id in dependents {
queue.push_back(dependent_id);
}
}
}
affected
}
}
/// Statistics about incremental reasoning
#[derive(Debug, Clone)]
pub struct IncrementalStats {
pub total_facts: usize,
pub asserted_facts: usize,
pub derived_facts: usize,
pub total_rules: usize,
pub generation: usize,
pub snapshots: usize,
}
impl std::fmt::Display for IncrementalStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Facts: {} (asserted: {}, derived: {}), Rules: {}, Generation: {}, Snapshots: {}",
self.total_facts,
self.asserted_facts,
self.derived_facts,
self.total_rules,
self.generation,
self.snapshots
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_incremental_add_fact() -> Result<(), Box<dyn std::error::Error>> {
let mut reasoner = IncrementalReasoner::new();
// Add a simple rule
reasoner.add_rule(Rule {
name: "mortal".to_string(),
body: vec![RuleAtom::Triple {
subject: Term::Variable("X".to_string()),
predicate: Term::Constant("type".to_string()),
object: Term::Constant("human".to_string()),
}],
head: vec![RuleAtom::Triple {
subject: Term::Variable("X".to_string()),
predicate: Term::Constant("type".to_string()),
object: Term::Constant("mortal".to_string()),
}],
});
// Add a fact
let delta = reasoner.add_fact(RuleAtom::Triple {
subject: Term::Constant("socrates".to_string()),
predicate: Term::Constant("type".to_string()),
object: Term::Constant("human".to_string()),
})?;
// Should derive that socrates is mortal
assert!(!delta.is_empty());
let stats = reasoner.get_stats();
assert_eq!(stats.asserted_facts, 1);
assert!(stats.derived_facts > 0);
Ok(())
}
#[test]
fn test_incremental_remove_fact() -> Result<(), Box<dyn std::error::Error>> {
let mut reasoner = IncrementalReasoner::new();
let fact = RuleAtom::Triple {
subject: Term::Constant("socrates".to_string()),
predicate: Term::Constant("type".to_string()),
object: Term::Constant("human".to_string()),
};
reasoner.add_fact(fact.clone())?;
let removed = reasoner.remove_fact(&fact)?;
assert!(!removed.is_empty());
assert_eq!(reasoner.get_stats().total_facts, 0);
Ok(())
}
#[test]
fn test_snapshot_restore() -> Result<(), Box<dyn std::error::Error>> {
let mut reasoner = IncrementalReasoner::new();
reasoner.create_snapshot();
reasoner.add_fact(RuleAtom::Triple {
subject: Term::Constant("test".to_string()),
predicate: Term::Constant("p".to_string()),
object: Term::Constant("o".to_string()),
})?;
assert_eq!(reasoner.get_stats().total_facts, 1);
reasoner.restore_snapshot()?;
assert_eq!(reasoner.get_stats().total_facts, 0);
Ok(())
}
}