use anyhow::Result;
pub mod active_learning;
pub mod adaptive_strategies;
pub mod advanced_integration_example;
pub mod asp;
pub mod backward;
pub mod benchmark_suite;
pub mod cache;
pub mod chr;
pub mod composition;
pub mod comprehensive_tutorial;
pub mod conflict;
pub mod coverage;
pub mod datalog_engine;
pub mod debug;
pub mod dempster_shafer;
pub mod description_logic;
pub mod distributed;
pub mod entailment;
pub mod explainable_generation;
pub mod explanation;
pub mod forward;
pub mod fuzzy;
pub mod getting_started;
pub mod gpu_matching;
pub mod hermit_reasoner;
pub mod incremental;
pub mod integration;
pub mod integration_benchmarks;
pub mod language;
pub mod lazy_materialization;
pub mod lockfree;
pub mod materialization;
pub mod migration;
pub mod negation;
pub mod optimization;
pub mod owl;
pub mod owl_dl;
pub mod owl_el;
pub mod owl_profiles;
pub mod owl_ql;
pub mod owl_rl;
pub use owl_ql::{Owl2QLTBox, QueryAtom, QueryRewriter, RewrittenQuery};
pub mod n3logic;
pub mod parallel;
pub mod pellet_classifier;
pub mod performance;
pub mod possibilistic;
pub mod probabilistic;
pub mod probabilistic_rdf;
pub mod problog;
pub mod production_utils;
pub mod profiler;
pub mod quantum_optimizer;
pub mod rdf_integration;
pub mod rdf_processing_simple;
pub mod rdfs;
pub mod rete;
pub mod rete_enhanced;
pub mod rif;
pub mod rule_compression;
pub mod rule_index;
pub mod rule_learning;
pub mod rule_refinement;
pub mod shacl_integration;
pub mod simd_ops;
pub mod skos;
pub mod sparql_integration;
pub mod statistical_relational;
pub mod swrl;
pub mod tabling;
pub mod temporal;
pub mod test_generator;
pub mod transaction;
pub mod transfer_learning;
pub mod uncertainty_propagation;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Rule {
pub name: String,
pub body: Vec<RuleAtom>,
pub head: Vec<RuleAtom>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum RuleAtom {
Triple {
subject: Term,
predicate: Term,
object: Term,
},
Builtin {
name: String,
args: Vec<Term>,
},
NotEqual {
left: Term,
right: Term,
},
GreaterThan {
left: Term,
right: Term,
},
LessThan {
left: Term,
right: Term,
},
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Term {
Variable(String),
Constant(String),
Literal(String),
Function { name: String, args: Vec<Term> },
}
#[derive(Debug)]
pub struct RuleEngine {
rules: Vec<Rule>,
forward_chainer: forward::ForwardChainer,
backward_chainer: backward::BackwardChainer,
rete_network: rete::ReteNetwork,
cache: Option<crate::cache::RuleCache>,
}
impl RuleEngine {
pub fn new() -> Self {
Self {
rules: Vec::new(),
forward_chainer: forward::ForwardChainer::new(),
backward_chainer: backward::BackwardChainer::new(),
rete_network: rete::ReteNetwork::new(),
cache: Some(crate::cache::RuleCache::new()),
}
}
pub fn add_rule(&mut self, rule: Rule) {
self.rules.push(rule.clone());
self.forward_chainer.add_rule(rule.clone());
self.backward_chainer.add_rule(rule.clone());
let _ = self.rete_network.add_rule(&rule);
}
pub fn add_rules(&mut self, rules: Vec<Rule>) {
for rule in rules {
self.add_rule(rule);
}
}
pub fn add_facts(&mut self, facts: Vec<RuleAtom>) {
self.forward_chainer.add_facts(facts.clone());
self.backward_chainer.add_facts(facts);
}
pub fn forward_chain(&mut self, facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
self.forward_chainer.add_facts(facts.to_vec());
self.forward_chainer.infer()
}
pub fn backward_chain(&mut self, goal: &RuleAtom) -> Result<bool> {
self.backward_chainer.prove(goal)
}
pub fn rete_forward_chain(&mut self, facts: Vec<RuleAtom>) -> Result<Vec<RuleAtom>> {
self.rete_network.forward_chain(facts)
}
pub fn get_facts(&self) -> Vec<RuleAtom> {
self.forward_chainer.get_facts()
}
pub fn clear(&mut self) {
self.forward_chainer.clear_facts();
self.backward_chainer.clear_facts();
self.rete_network.clear();
self.clear_cache();
}
pub fn set_backward_chain_max_depth(&mut self, max_depth: usize) {
self.backward_chainer = backward::BackwardChainer::with_config(max_depth, false);
for rule in &self.rules {
self.backward_chainer.add_rule(rule.clone());
}
}
pub fn add_fact(&mut self, fact: RuleAtom) {
self.add_facts(vec![fact]);
}
pub fn set_cache(&mut self, cache: Option<crate::cache::RuleCache>) {
self.cache = cache;
tracing::debug!(
"Rule engine cache {}",
if self.cache.is_some() {
"enabled"
} else {
"disabled"
}
);
}
pub fn get_cache(&self) -> Option<&crate::cache::RuleCache> {
self.cache.as_ref()
}
pub fn get_cache_mut(&mut self) -> Option<&mut crate::cache::RuleCache> {
self.cache.as_mut()
}
pub fn enable_cache(&mut self) {
self.cache = Some(crate::cache::RuleCache::new());
tracing::info!("Rule engine cache enabled with default settings");
}
pub fn disable_cache(&mut self) {
self.cache = None;
tracing::info!("Rule engine cache disabled");
}
pub fn is_cache_enabled(&self) -> bool {
self.cache.is_some()
}
pub fn clear_cache(&mut self) {
if let Some(cache) = &self.cache {
cache.clear_all();
tracing::debug!("Rule engine cache cleared");
}
}
pub fn get_cache_statistics(&self) -> Option<crate::cache::CachingStatistics> {
self.cache.as_ref().map(|cache| cache.get_statistics())
}
pub fn warm_cache(&self, common_facts: &[RuleAtom]) {
if let Some(cache) = &self.cache {
cache.warm_cache(&self.rules, common_facts);
tracing::info!(
"Cache warmed up with {} rules and {} facts",
self.rules.len(),
common_facts.len()
);
}
}
}
impl Default for RuleEngine {
fn default() -> Self {
Self::new()
}
}
pub mod rdfs_entailment;
pub mod forward_chainer;
pub mod rule_parser;
pub mod rete_network;
pub mod conflict_resolver;
pub mod backward_chainer;
pub mod rule_graph;
pub mod rule_compiler;
pub mod truth_maintenance;
pub mod rule_tracer;
pub mod rule_serializer;
pub mod rule_validator;
pub mod rule_executor;
pub mod rule_statistics;
#[cfg(test)]
mod comprehensive_tests;
#[cfg(test)]
mod comprehensive_tests_extended;