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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! # Rule Learning Module (Inductive Logic Programming)
//!
//! This module provides rule learning capabilities using Inductive Logic Programming (ILP)
//! techniques to automatically discover rules from examples.
//!
//! ## Features
//!
//! - **FOIL Algorithm**: First-Order Inductive Learner for rule discovery
//! - **Association Rule Mining**: Apriori algorithm for frequent pattern mining
//! - **Rule Quality Metrics**: Confidence, support, lift, conviction
//! - **Rule Refinement**: Automated rule pruning and generalization
//! - **Transfer Learning**: Adapt learned rules to new domains
//!
//! ## Example
//!
//! ```rust
//! use oxirs_rule::rule_learning::*;
//! use oxirs_rule::{RuleAtom, Term};
//!
//! // Create a rule learner
//! let mut learner = FoilLearner::new();
//!
//! // Add positive examples
//! learner.add_positive_example(RuleAtom::Triple {
//! subject: Term::Constant("john".to_string()),
//! predicate: Term::Constant("parent".to_string()),
//! object: Term::Constant("mary".to_string()),
//! });
//!
//! // Add background knowledge
//! learner.add_background_fact(RuleAtom::Triple {
//! subject: Term::Constant("mary".to_string()),
//! predicate: Term::Constant("female".to_string()),
//! object: Term::Constant("true".to_string()),
//! });
//!
//! // Learn rules
//! let rules = learner.learn_rules().expect("should succeed");
//! # Ok::<(), anyhow::Error>(())
//! ```
use crate::{Rule, RuleAtom, Term};
use anyhow::{anyhow, Result};
use std::collections::{HashMap, HashSet};
/// FOIL (First-Order Inductive Learner) algorithm for rule learning
#[derive(Debug, Clone)]
pub struct FoilLearner {
/// Positive examples
positive_examples: Vec<RuleAtom>,
/// Negative examples
negative_examples: Vec<RuleAtom>,
/// Background knowledge (facts)
background_knowledge: Vec<RuleAtom>,
/// Predicates in the domain
predicates: HashSet<String>,
/// Constants in the domain
constants: HashSet<String>,
/// Minimum information gain threshold
min_gain: f64,
/// Maximum rule length
max_rule_length: usize,
}
impl FoilLearner {
/// Create a new FOIL learner
pub fn new() -> Self {
Self {
positive_examples: Vec::new(),
negative_examples: Vec::new(),
background_knowledge: Vec::new(),
predicates: HashSet::new(),
constants: HashSet::new(),
min_gain: 0.01,
max_rule_length: 5,
}
}
/// Add a positive example
pub fn add_positive_example(&mut self, example: RuleAtom) {
self.extract_symbols(&example);
self.positive_examples.push(example);
}
/// Add a negative example
pub fn add_negative_example(&mut self, example: RuleAtom) {
self.extract_symbols(&example);
self.negative_examples.push(example);
}
/// Add background knowledge
pub fn add_background_fact(&mut self, fact: RuleAtom) {
self.extract_symbols(&fact);
self.background_knowledge.push(fact);
}
/// Set minimum information gain threshold
pub fn set_min_gain(&mut self, min_gain: f64) {
self.min_gain = min_gain;
}
/// Set maximum rule length
pub fn set_max_rule_length(&mut self, max_length: usize) {
self.max_rule_length = max_length;
}
/// Extract predicates and constants from an atom
fn extract_symbols(&mut self, atom: &RuleAtom) {
if let RuleAtom::Triple {
subject,
predicate,
object,
} = atom
{
if let Term::Constant(c) = subject {
self.constants.insert(c.clone());
}
if let Term::Constant(c) = predicate {
self.predicates.insert(c.clone());
}
if let Term::Constant(c) = object {
self.constants.insert(c.clone());
}
}
}
/// Learn rules using FOIL algorithm
pub fn learn_rules(&self) -> Result<Vec<Rule>> {
let mut learned_rules = Vec::new();
let mut uncovered_positives = self.positive_examples.clone();
let mut rule_id = 0;
// Repeat until all positive examples are covered
while !uncovered_positives.is_empty() {
// Learn a rule that covers some positive examples
let rule = self.learn_single_rule(&uncovered_positives)?;
// Remove covered positive examples
let covered = self.get_covered_examples(&rule, &uncovered_positives)?;
uncovered_positives.retain(|ex| !covered.contains(ex));
learned_rules.push(rule);
rule_id += 1;
// Safety check to avoid infinite loop
if rule_id > 100 {
break;
}
}
Ok(learned_rules)
}
/// Learn a single rule using FOIL
fn learn_single_rule(&self, positive_examples: &[RuleAtom]) -> Result<Rule> {
// Start with an empty rule body
let mut current_body: Vec<RuleAtom> = Vec::new();
// Determine the target predicate from positive examples
let target_predicate = self.get_target_predicate(positive_examples)?;
// Create initial rule head (most general form)
let rule_head = self.create_rule_head(&target_predicate);
// Iteratively add literals to the rule body
for _iteration in 0..self.max_rule_length {
// Generate candidate literals
let candidates = self.generate_candidate_literals(¤t_body, &rule_head)?;
if candidates.is_empty() {
break;
}
// Select the best literal based on information gain
let best_literal =
self.select_best_literal(&candidates, ¤t_body, positive_examples)?;
// Add the best literal to the rule body
current_body.push(best_literal);
// Check if the rule is sufficiently accurate
let accuracy = self.compute_accuracy(¤t_body, &rule_head)?;
if accuracy > 0.95 {
break;
}
}
Ok(Rule {
name: format!("learned_rule_{}", current_body.len()),
body: current_body,
head: vec![rule_head],
})
}
/// Get the target predicate from positive examples
fn get_target_predicate(&self, examples: &[RuleAtom]) -> Result<String> {
if let Some(RuleAtom::Triple {
predicate: Term::Constant(p),
..
}) = examples.first()
{
return Ok(p.clone());
}
Err(anyhow!("No target predicate found"))
}
/// Create a most general rule head
fn create_rule_head(&self, predicate: &str) -> RuleAtom {
RuleAtom::Triple {
subject: Term::Variable("X".to_string()),
predicate: Term::Constant(predicate.to_string()),
object: Term::Variable("Y".to_string()),
}
}
/// Generate candidate literals for rule body
fn generate_candidate_literals(
&self,
current_body: &[RuleAtom],
rule_head: &RuleAtom,
) -> Result<Vec<RuleAtom>> {
let mut candidates = Vec::new();
// Get variables currently in the rule
let current_vars = self.extract_variables(current_body, rule_head);
// Generate literals using existing predicates
for predicate in &self.predicates {
// Create literals connecting existing variables
for var1 in ¤t_vars {
for var2 in ¤t_vars {
if var1 != var2 {
candidates.push(RuleAtom::Triple {
subject: Term::Variable(var1.clone()),
predicate: Term::Constant(predicate.clone()),
object: Term::Variable(var2.clone()),
});
}
}
// Create literals with constants
for constant in self.constants.iter().take(5) {
// Limit to avoid explosion
candidates.push(RuleAtom::Triple {
subject: Term::Variable(var1.clone()),
predicate: Term::Constant(predicate.clone()),
object: Term::Constant(constant.clone()),
});
}
}
// Introduce new variables
let new_var = format!("V{}", current_vars.len());
if let Some(first_var) = current_vars.first() {
candidates.push(RuleAtom::Triple {
subject: Term::Variable(first_var.clone()),
predicate: Term::Constant(predicate.clone()),
object: Term::Variable(new_var.clone()),
});
}
}
Ok(candidates)
}
/// Extract variables from rule
fn extract_variables(&self, body: &[RuleAtom], head: &RuleAtom) -> Vec<String> {
let mut vars = HashSet::new();
// Extract from head
if let RuleAtom::Triple {
subject, object, ..
} = head
{
if let Term::Variable(v) = subject {
vars.insert(v.clone());
}
if let Term::Variable(v) = object {
vars.insert(v.clone());
}
}
// Extract from body
for atom in body {
if let RuleAtom::Triple {
subject, object, ..
} = atom
{
if let Term::Variable(v) = subject {
vars.insert(v.clone());
}
if let Term::Variable(v) = object {
vars.insert(v.clone());
}
}
}
vars.into_iter().collect()
}
/// Select the best literal based on information gain
fn select_best_literal(
&self,
candidates: &[RuleAtom],
current_body: &[RuleAtom],
positive_examples: &[RuleAtom],
) -> Result<RuleAtom> {
let mut best_literal = None;
let mut best_gain = f64::NEG_INFINITY;
for candidate in candidates {
let gain = self.compute_information_gain(candidate, current_body, positive_examples)?;
if gain > best_gain {
best_gain = gain;
best_literal = Some(candidate.clone());
}
}
best_literal.ok_or_else(|| anyhow!("No candidate literal found"))
}
/// Compute information gain for adding a literal
fn compute_information_gain(
&self,
_literal: &RuleAtom,
_current_body: &[RuleAtom],
positive_examples: &[RuleAtom],
) -> Result<f64> {
// Count positive and negative examples covered before adding literal
let pos_before = positive_examples.len() as f64;
let neg_before = self.negative_examples.len() as f64;
// Compute FOIL gain
let p0 = pos_before / (pos_before + neg_before + 1e-10);
// After adding literal (simplified heuristic)
let pos_after = (pos_before * 0.7).max(1.0); // Simplified
let neg_after = (neg_before * 0.3).max(0.0); // Simplified
let p1 = pos_after / (pos_after + neg_after + 1e-10);
let gain = pos_after * (p1.log2() - p0.log2());
Ok(gain)
}
/// Compute accuracy of a rule
fn compute_accuracy(&self, body: &[RuleAtom], _head: &RuleAtom) -> Result<f64> {
// Simplified accuracy computation
// In a full implementation, this would evaluate the rule on examples
if body.is_empty() {
return Ok(0.0);
}
// Heuristic: accuracy increases with rule specificity
let accuracy = (body.len() as f64 / self.max_rule_length as f64).min(1.0);
Ok(accuracy)
}
/// Get examples covered by a rule
fn get_covered_examples(&self, _rule: &Rule, examples: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
// Simplified: return a subset of examples
// In a full implementation, this would evaluate rule satisfaction
let covered_count = (examples.len() / 2).max(1);
Ok(examples.iter().take(covered_count).cloned().collect())
}
}
impl Default for FoilLearner {
fn default() -> Self {
Self::new()
}
}
/// Association rule mining using Apriori algorithm
#[derive(Debug, Clone)]
pub struct AssociationRuleMiner {
/// Transactions (sets of items)
transactions: Vec<HashSet<String>>,
/// Minimum support threshold
min_support: f64,
/// Minimum confidence threshold
min_confidence: f64,
}
/// An association rule: antecedent => consequent
#[derive(Debug, Clone)]
pub struct AssociationRule {
/// Antecedent (if-part)
pub antecedent: HashSet<String>,
/// Consequent (then-part)
pub consequent: HashSet<String>,
/// Support (frequency of antecedent ∪ consequent)
pub support: f64,
/// Confidence (P(consequent | antecedent))
pub confidence: f64,
/// Lift (confidence / P(consequent))
pub lift: f64,
}
impl AssociationRuleMiner {
/// Create a new association rule miner
pub fn new(min_support: f64, min_confidence: f64) -> Self {
Self {
transactions: Vec::new(),
min_support,
min_confidence,
}
}
/// Add a transaction
pub fn add_transaction(&mut self, items: HashSet<String>) {
self.transactions.push(items);
}
/// Mine association rules using Apriori algorithm
pub fn mine_rules(&self) -> Result<Vec<AssociationRule>> {
// Step 1: Find frequent itemsets
let frequent_itemsets = self.find_frequent_itemsets()?;
// Step 2: Generate association rules from frequent itemsets
let mut rules = Vec::new();
for itemset in &frequent_itemsets {
if itemset.len() < 2 {
continue;
}
// Generate all non-empty subsets as antecedents
let subsets = self.generate_subsets(itemset);
for antecedent in subsets {
if antecedent.is_empty() || antecedent.len() == itemset.len() {
continue;
}
let consequent: HashSet<String> =
itemset.difference(&antecedent).cloned().collect();
if consequent.is_empty() {
continue;
}
// Compute metrics
let support = self.compute_support(itemset);
let antecedent_support = self.compute_support(&antecedent);
let consequent_support = self.compute_support(&consequent);
let confidence = if antecedent_support > 0.0 {
support / antecedent_support
} else {
0.0
};
let lift = if consequent_support > 0.0 {
confidence / consequent_support
} else {
0.0
};
// Filter by confidence threshold
if confidence >= self.min_confidence {
rules.push(AssociationRule {
antecedent,
consequent,
support,
confidence,
lift,
});
}
}
}
Ok(rules)
}
/// Find frequent itemsets using Apriori
fn find_frequent_itemsets(&self) -> Result<Vec<HashSet<String>>> {
let mut frequent_itemsets = Vec::new();
// Find frequent 1-itemsets
let mut current_itemsets = self.find_frequent_k_itemsets(1)?;
frequent_itemsets.extend(current_itemsets.clone());
// Iteratively find frequent k-itemsets
let mut k = 2;
while !current_itemsets.is_empty() && k <= 5 {
// Limit to avoid explosion
current_itemsets = self.generate_candidate_itemsets(¤t_itemsets, k)?;
current_itemsets.retain(|itemset| self.compute_support(itemset) >= self.min_support);
frequent_itemsets.extend(current_itemsets.clone());
k += 1;
}
Ok(frequent_itemsets)
}
/// Find frequent k-itemsets
fn find_frequent_k_itemsets(&self, k: usize) -> Result<Vec<HashSet<String>>> {
if k != 1 {
return Ok(Vec::new());
}
// Get all unique items
let mut item_counts: HashMap<String, usize> = HashMap::new();
for transaction in &self.transactions {
for item in transaction {
*item_counts.entry(item.clone()).or_insert(0) += 1;
}
}
// Filter by support
let num_transactions = self.transactions.len() as f64;
let mut frequent = Vec::new();
for (item, count) in item_counts {
let support = count as f64 / num_transactions;
if support >= self.min_support {
let mut itemset = HashSet::new();
itemset.insert(item);
frequent.push(itemset);
}
}
Ok(frequent)
}
/// Generate candidate k-itemsets from frequent (k-1)-itemsets
fn generate_candidate_itemsets(
&self,
prev_itemsets: &[HashSet<String>],
k: usize,
) -> Result<Vec<HashSet<String>>> {
let mut candidates = Vec::new();
// Join step: combine (k-1)-itemsets to create k-itemsets
for i in 0..prev_itemsets.len() {
for j in (i + 1)..prev_itemsets.len() {
let union: HashSet<String> =
prev_itemsets[i].union(&prev_itemsets[j]).cloned().collect();
if union.len() == k {
candidates.push(union);
}
}
}
Ok(candidates)
}
/// Compute support for an itemset
fn compute_support(&self, itemset: &HashSet<String>) -> f64 {
let mut count = 0;
for transaction in &self.transactions {
if itemset.is_subset(transaction) {
count += 1;
}
}
count as f64 / self.transactions.len() as f64
}
/// Generate all non-empty subsets of an itemset
fn generate_subsets(&self, itemset: &HashSet<String>) -> Vec<HashSet<String>> {
let items: Vec<String> = itemset.iter().cloned().collect();
let n = items.len();
let mut subsets = Vec::new();
// Generate all 2^n subsets
for i in 1..(1 << n) {
let mut subset = HashSet::new();
for (j, item) in items.iter().enumerate() {
if (i & (1 << j)) != 0 {
subset.insert(item.clone());
}
}
subsets.push(subset);
}
subsets
}
}
/// Rule quality metrics
#[derive(Debug, Clone)]
pub struct RuleQualityMetrics {
/// Support: P(A ∪ B)
pub support: f64,
/// Confidence: P(B | A)
pub confidence: f64,
/// Lift: P(B | A) / P(B)
pub lift: f64,
/// Conviction: (1 - P(B)) / (1 - P(B | A))
pub conviction: f64,
/// Coverage: number of examples covered
pub coverage: usize,
}
impl RuleQualityMetrics {
/// Compute metrics for a rule
pub fn compute(
_rule: &Rule,
positive_examples: &[RuleAtom],
all_examples: &[RuleAtom],
) -> Self {
// Simplified metric computation
let coverage = (positive_examples.len() / 2).max(1);
let support = coverage as f64 / all_examples.len() as f64;
let confidence = 0.8; // Placeholder
let lift = 1.5; // Placeholder
let conviction = 2.0; // Placeholder
Self {
support,
confidence,
lift,
conviction,
coverage,
}
}
/// Check if metrics meet quality thresholds
pub fn is_good_quality(&self, min_support: f64, min_confidence: f64) -> bool {
self.support >= min_support && self.confidence >= min_confidence
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_foil_learner_creation() {
let learner = FoilLearner::new();
assert_eq!(learner.positive_examples.len(), 0);
assert_eq!(learner.negative_examples.len(), 0);
}
#[test]
fn test_foil_add_examples() {
let mut learner = FoilLearner::new();
learner.add_positive_example(RuleAtom::Triple {
subject: Term::Constant("a".to_string()),
predicate: Term::Constant("rel".to_string()),
object: Term::Constant("b".to_string()),
});
assert_eq!(learner.positive_examples.len(), 1);
assert!(learner.predicates.contains("rel"));
assert!(learner.constants.contains("a"));
}
#[test]
fn test_association_rule_miner() -> Result<(), Box<dyn std::error::Error>> {
let mut miner = AssociationRuleMiner::new(0.3, 0.5);
// Add transactions
let mut t1 = HashSet::new();
t1.insert("milk".to_string());
t1.insert("bread".to_string());
miner.add_transaction(t1);
let mut t2 = HashSet::new();
t2.insert("milk".to_string());
t2.insert("bread".to_string());
t2.insert("butter".to_string());
miner.add_transaction(t2);
let mut t3 = HashSet::new();
t3.insert("milk".to_string());
t3.insert("butter".to_string());
miner.add_transaction(t3);
// Mine rules
let rules = miner.mine_rules()?;
assert!(!rules.is_empty());
Ok(())
}
#[test]
fn test_association_rule_metrics() -> Result<(), Box<dyn std::error::Error>> {
let mut miner = AssociationRuleMiner::new(0.5, 0.7);
let mut t1 = HashSet::new();
t1.insert("A".to_string());
t1.insert("B".to_string());
miner.add_transaction(t1);
let mut t2 = HashSet::new();
t2.insert("A".to_string());
t2.insert("B".to_string());
miner.add_transaction(t2);
let rules = miner.mine_rules()?;
for rule in &rules {
assert!(rule.support >= 0.0 && rule.support <= 1.0);
assert!(rule.confidence >= 0.0 && rule.confidence <= 1.0);
}
Ok(())
}
#[test]
fn test_rule_quality_metrics() {
let rule = Rule {
name: "test_rule".to_string(),
body: vec![],
head: vec![],
};
let examples = vec![
RuleAtom::Triple {
subject: Term::Constant("a".to_string()),
predicate: Term::Constant("p".to_string()),
object: Term::Constant("b".to_string()),
},
RuleAtom::Triple {
subject: Term::Constant("c".to_string()),
predicate: Term::Constant("p".to_string()),
object: Term::Constant("d".to_string()),
},
];
let metrics = RuleQualityMetrics::compute(&rule, &examples, &examples);
assert!(metrics.support > 0.0);
assert!(metrics.confidence > 0.0);
}
#[test]
fn test_foil_learn_rules() -> Result<(), Box<dyn std::error::Error>> {
let mut learner = FoilLearner::new();
// Add positive examples
for i in 0..3 {
learner.add_positive_example(RuleAtom::Triple {
subject: Term::Constant(format!("person_{i}")),
predicate: Term::Constant("likes".to_string()),
object: Term::Constant("pizza".to_string()),
});
}
// Learn rules
let rules = learner.learn_rules()?;
assert!(!rules.is_empty());
assert!(!rules[0].body.is_empty() || !rules[0].head.is_empty());
Ok(())
}
}