Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

refactor: standardize reasoning module with comprehensive documentation

KaifAhmad1e11bf266b56c
diff --git a/semantica/reasoning/__init__.py b/semantica/reasoning/__init__.py
index c67910cf..6240d0ed 100644
--- a/semantica/reasoning/__init__.py
+++ b/semantica/reasoning/__init__.py
@@ -1,10 +1,21 @@
"""
-Reasoning module for Semantica framework.
+Reasoning Module
-This module provides reasoning and inference capabilities
-for knowledge graph analysis and query answering.
+This module provides reasoning and inference capabilities for knowledge graph
+analysis and query answering, supporting multiple reasoning strategies including
+rule-based inference, SPARQL reasoning, abductive and deductive reasoning.
-Exports:
+Key Features:
+ - Rule-based inference with forward/backward chaining
+ - SPARQL-based reasoning and query expansion
+ - Rete algorithm implementation for efficient rule matching
+ - Abductive reasoning for hypothesis generation
+ - Deductive reasoning for logical inference
+ - Rule management and execution tracking
+ - Explanation generation for reasoning results
+ - Proof generation and validation
+
+Main Classes:
- InferenceEngine: Rule-based inference with forward/backward chaining
- SPARQLReasoner: SPARQL-based reasoning and query expansion
- ReteEngine: Rete algorithm implementation for efficient rule matching
@@ -12,6 +23,16 @@ Exports:
- DeductiveReasoner: Deductive reasoning for logical inference
- RuleManager: Rule management and execution tracking
- ExplanationGenerator: Explanation generation for reasoning results
+
+Example Usage:
+ >>> from semantica.reasoning import InferenceEngine, SPARQLReasoner
+ >>> engine = InferenceEngine()
+ >>> result = engine.infer(facts, rules)
+ >>> reasoner = SPARQLReasoner()
+ >>> query_result = reasoner.query("SELECT ?s ?p ?o WHERE { ?s ?p ?o }")
+
+Author: Semantica Contributors
+License: MIT
"""
from .inference_engine import (
diff --git a/semantica/reasoning/abductive_reasoner.py b/semantica/reasoning/abductive_reasoner.py
index e235cdca..0c6d69e0 100644
--- a/semantica/reasoning/abductive_reasoner.py
+++ b/semantica/reasoning/abductive_reasoner.py
@@ -1,8 +1,34 @@
"""
-Abductive reasoner for Semantica framework.
+Abductive Reasoner Module
-This module provides abductive reasoning capabilities
-for hypothesis generation and explanation.
+This module provides abductive reasoning capabilities for hypothesis generation
+and explanation, finding the best explanations for observations using various
+ranking strategies.
+
+Key Features:
+ - Abductive reasoning for hypothesis generation
+ - Hypothesis ranking (simplicity, plausibility, consistency, coverage)
+ - Observation explanation
+ - Hypothesis evaluation and scoring
+ - Best explanation selection
+ - Confidence calculation
+
+Main Classes:
+ - AbductiveReasoner: Abductive reasoning engine
+ - Observation: Dataclass for observations to explain
+ - Hypothesis: Dataclass for abductive hypotheses
+ - Explanation: Dataclass for abductive explanations
+ - HypothesisRanking: Enum for hypothesis ranking strategies
+
+Example Usage:
+ >>> from semantica.reasoning import AbductiveReasoner, Observation
+ >>> reasoner = AbductiveReasoner()
+ >>> observation = Observation("obs1", "Person X is in location Y")
+ >>> hypotheses = reasoner.generate_hypotheses(observation, facts)
+ >>> best = reasoner.rank_hypotheses(hypotheses, HypothesisRanking.SIMPLICITY)
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional
diff --git a/semantica/reasoning/deductive_reasoner.py b/semantica/reasoning/deductive_reasoner.py
index 68d7681e..6424c733 100644
--- a/semantica/reasoning/deductive_reasoner.py
+++ b/semantica/reasoning/deductive_reasoner.py
@@ -1,8 +1,33 @@
"""
-Deductive reasoner for Semantica framework.
+Deductive Reasoner Module
-This module provides deductive reasoning capabilities
-for logical inference and proof generation.
+This module provides deductive reasoning capabilities for logical inference
+and proof generation, supporting formal logic and theorem proving.
+
+Key Features:
+ - Deductive reasoning for logical inference
+ - Proof generation and validation
+ - Premise and conclusion management
+ - Logical argument construction
+ - Theorem proving
+ - Confidence calculation
+
+Main Classes:
+ - DeductiveReasoner: Deductive reasoning engine
+ - Premise: Dataclass for logical premises
+ - Conclusion: Dataclass for logical conclusions
+ - Proof: Dataclass for logical proofs
+ - Argument: Dataclass for logical arguments
+
+Example Usage:
+ >>> from semantica.reasoning import DeductiveReasoner, Premise
+ >>> reasoner = DeductiveReasoner()
+ >>> premises = [Premise("p1", "All humans are mortal"), Premise("p2", "Socrates is human")]
+ >>> conclusion = reasoner.deduce(premises, rules)
+ >>> proof = reasoner.generate_proof(premises, conclusion)
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional, Set
diff --git a/semantica/reasoning/explanation_generator.py b/semantica/reasoning/explanation_generator.py
index f7cc14be..8d9fe024 100644
--- a/semantica/reasoning/explanation_generator.py
+++ b/semantica/reasoning/explanation_generator.py
@@ -1,8 +1,33 @@
"""
-Explanation generator for Semantica framework.
+Explanation Generator Module
-This module provides explanation generation for
-reasoning results and inference chains.
+This module provides explanation generation for reasoning results and inference
+chains, creating human-readable justifications and reasoning paths.
+
+Key Features:
+ - Explanation generation for reasoning results
+ - Reasoning path visualization
+ - Justification creation
+ - Step-by-step reasoning breakdown
+ - Human-readable explanations
+ - Evidence tracking
+
+Main Classes:
+ - ExplanationGenerator: Explanation generation engine
+ - Explanation: Dataclass for explanations
+ - ReasoningStep: Dataclass for single reasoning steps
+ - ReasoningPath: Dataclass for reasoning path visualization
+ - Justification: Dataclass for justifications
+
+Example Usage:
+ >>> from semantica.reasoning import ExplanationGenerator
+ >>> generator = ExplanationGenerator()
+ >>> explanation = generator.generate_explanation(inference_result)
+ >>> path = generator.generate_reasoning_path(inference_result)
+ >>> justification = generator.create_justification(conclusion, path)
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional
diff --git a/semantica/reasoning/inference_engine.py b/semantica/reasoning/inference_engine.py
index 7dcdf7ad..764ee182 100644
--- a/semantica/reasoning/inference_engine.py
+++ b/semantica/reasoning/inference_engine.py
@@ -1,8 +1,32 @@
"""
-Inference engine for Semantica framework.
+Inference Engine Module
-This module provides rule-based inference capabilities
-for knowledge graph reasoning and analysis.
+This module provides rule-based inference capabilities for knowledge graph
+reasoning and analysis, supporting forward chaining, backward chaining, and
+bidirectional inference strategies.
+
+Key Features:
+ - Rule-based inference and reasoning
+ - Forward and backward chaining
+ - Bidirectional inference
+ - Rule management and execution
+ - Performance optimization
+ - Error handling and recovery
+ - Custom rule support
+
+Main Classes:
+ - InferenceEngine: Rule-based inference engine
+ - InferenceResult: Dataclass for inference results
+ - InferenceStrategy: Enum for inference strategies (forward, backward, bidirectional)
+
+Example Usage:
+ >>> from semantica.reasoning import InferenceEngine, InferenceStrategy
+ >>> engine = InferenceEngine()
+ >>> result = engine.infer(facts, rules, strategy=InferenceStrategy.FORWARD)
+ >>> conclusions = engine.forward_chain(facts, rules)
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional, Callable, Set
diff --git a/semantica/reasoning/rete_engine.py b/semantica/reasoning/rete_engine.py
index af13aad4..8f69bf03 100644
--- a/semantica/reasoning/rete_engine.py
+++ b/semantica/reasoning/rete_engine.py
@@ -1,8 +1,36 @@
"""
-Rete algorithm implementation for Semantica framework.
+Rete Algorithm Engine Module
-This module provides Rete algorithm implementation
-for efficient rule-based reasoning.
+This module provides Rete algorithm implementation for efficient rule-based
+reasoning, using a network of alpha and beta nodes for pattern matching.
+
+Key Features:
+ - Rete algorithm implementation for efficient rule matching
+ - Alpha node pattern matching
+ - Beta node join operations
+ - Terminal node activation
+ - Incremental fact processing
+ - Performance optimization for large rule sets
+
+Main Classes:
+ - ReteEngine: Rete algorithm implementation
+ - ReteNode: Base Rete network node
+ - AlphaNode: Alpha node for single condition matching
+ - BetaNode: Beta node for join operations
+ - TerminalNode: Terminal node for rule activation
+ - Fact: Dataclass for fact representation
+ - Match: Dataclass for pattern matches
+
+Example Usage:
+ >>> from semantica.reasoning import ReteEngine, Fact
+ >>> engine = ReteEngine()
+ >>> engine.add_rule(rule)
+ >>> fact = Fact("f1", "Person", ["John"])
+ >>> engine.add_fact(fact)
+ >>> matches = engine.get_matches()
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional, Set, Tuple
diff --git a/semantica/reasoning/rule_manager.py b/semantica/reasoning/rule_manager.py
index 76a8ab0a..ec5eaa30 100644
--- a/semantica/reasoning/rule_manager.py
+++ b/semantica/reasoning/rule_manager.py
@@ -1,8 +1,33 @@
"""
-Rule manager for Semantica framework.
+Rule Manager Module
-This module provides rule management and execution
-for reasoning and inference operations.
+This module provides rule management and execution for reasoning and inference
+operations, supporting rule registration, validation, execution tracking, and
+rule type management.
+
+Key Features:
+ - Rule management and execution
+ - Rule registration and validation
+ - Rule execution tracking
+ - Multiple rule types (implication, equivalence, constraint, transformation)
+ - Rule priority management
+ - Execution history
+
+Main Classes:
+ - RuleManager: Rule management system
+ - Rule: Dataclass for rule definition
+ - RuleExecution: Dataclass for rule execution records
+ - RuleType: Enum for rule types
+
+Example Usage:
+ >>> from semantica.reasoning import RuleManager, Rule, RuleType
+ >>> manager = RuleManager()
+ >>> rule = Rule("r1", "If A then B", conditions=["A"], conclusion="B", rule_type=RuleType.IMPLICATION)
+ >>> manager.add_rule(rule)
+ >>> execution = manager.execute_rule(rule, facts)
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional, Callable
diff --git a/semantica/reasoning/sparql_reasoner.py b/semantica/reasoning/sparql_reasoner.py
index 283b0752..c8430c50 100644
--- a/semantica/reasoning/sparql_reasoner.py
+++ b/semantica/reasoning/sparql_reasoner.py
@@ -1,8 +1,32 @@
"""
-SPARQL reasoner for Semantica framework.
+SPARQL Reasoner Module
-This module provides SPARQL-based reasoning capabilities
-for knowledge graph query answering.
+This module provides SPARQL-based reasoning capabilities for knowledge graph
+query answering, including query expansion, inference rule integration, and
+query optimization.
+
+Key Features:
+ - SPARQL query reasoning and execution
+ - Inference rule integration
+ - Query optimization and caching
+ - Query expansion
+ - Performance optimization
+ - Error handling and recovery
+ - Triple store integration
+
+Main Classes:
+ - SPARQLReasoner: SPARQL-based reasoning engine
+ - SPARQLQueryResult: Dataclass for SPARQL query results
+
+Example Usage:
+ >>> from semantica.reasoning import SPARQLReasoner
+ >>> reasoner = SPARQLReasoner()
+ >>> query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o }"
+ >>> result = reasoner.query(query)
+ >>> expanded = reasoner.expand_query(query, rules)
+
+Author: Semantica Contributors
+License: MIT
"""
from typing import Any, Dict, List, Optional, Set