Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Fix conflict detector input handling and add unified Reasoner

KaifAhmad1a8828741e1b7
diff --git a/semantica/conflicts/conflict_detector.py b/semantica/conflicts/conflict_detector.py
index 9b65fbae..b1295f60 100644
--- a/semantica/conflicts/conflict_detector.py
+++ b/semantica/conflicts/conflict_detector.py
@@ -57,7 +57,7 @@ License: MIT
from dataclasses import dataclass, field
from enum import Enum
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, List, Optional, Union
from ..utils.logging import get_logger
from ..utils.progress_tracker import get_progress_tracker
@@ -131,7 +131,7 @@ class ConflictDetector:
def detect_value_conflicts(
self,
- entities: List[Dict[str, Any]],
+ entities: Union[List[Dict[str, Any]], Dict[str, Any]],
property_name: str,
entity_type: Optional[str] = None,
) -> List[Conflict]:
@@ -139,13 +139,20 @@ class ConflictDetector:
Detect property value conflicts.
Args:
- entities: List of entity dictionaries
+ entities: List of entity dictionaries or Graph dictionary
property_name: Property name to check
entity_type: Optional entity type filter
Returns:
List of detected conflicts
"""
+ # Handle graph dictionary input
+ if isinstance(entities, dict):
+ if "entities" in entities:
+ entities = entities["entities"]
+ else:
+ entities = [entities]
+
# Track conflict detection
tracking_id = self.progress_tracker.start_tracking(
file=None,
@@ -815,7 +822,9 @@ class ConflictDetector:
raise
def detect_conflicts(
- self, entities: List[Dict[str, Any]], entity_type: Optional[str] = None
+ self,
+ entities: Union[List[Dict[str, Any]], Dict[str, Any]],
+ entity_type: Optional[str] = None,
) -> List[Conflict]:
"""
Detect all conflicts for entities (general method).
@@ -824,12 +833,20 @@ class ConflictDetector:
temporal, and logical conflicts.
Args:
- entities: List of entity dictionaries
+ entities: List of entity dictionaries or Graph dictionary (containing "entities" key)
entity_type: Optional entity type filter
Returns:
List of all detected conflicts
"""
+ # Handle graph dictionary input
+ if isinstance(entities, dict):
+ if "entities" in entities:
+ entities = entities["entities"]
+ else:
+ # If it's a single entity dict, wrap in list
+ entities = [entities]
+
tracking_id = self.progress_tracker.start_tracking(
file=None,
module="conflicts",
diff --git a/semantica/reasoning/__init__.py b/semantica/reasoning/__init__.py
index cefef696..dd722bd4 100644
--- a/semantica/reasoning/__init__.py
+++ b/semantica/reasoning/__init__.py
@@ -100,6 +100,7 @@ from .abductive_reasoner import AbductiveReasoner
from .abductive_reasoner import Explanation as AbductiveExplanation
from .abductive_reasoner import Hypothesis, HypothesisRanking, Observation
from .deductive_reasoner import Argument, Conclusion, DeductiveReasoner, Premise, Proof
+from .reasoner import Reasoner
from .explanation_generator import (
Explanation,
ExplanationGenerator,
@@ -148,6 +149,8 @@ __all__ = [
"Conclusion",
"Proof",
"Argument",
+ # Reasoner facade
+ "Reasoner",
# Rule management
"RuleManager",
"Rule",
diff --git a/semantica/reasoning/reasoner.py b/semantica/reasoning/reasoner.py
new file mode 100644
index 00000000..374d1952
--- /dev/null
+++ b/semantica/reasoning/reasoner.py
@@ -0,0 +1,149 @@
+"""
+Reasoner Module
+
+This module provides a high-level Reasoner class that unifies various reasoning strategies
+supported by the Semantica framework. It serves as a facade for different reasoning engines
+like InferenceEngine, SPARQLReasoner, etc.
+"""
+
+from typing import Any, Dict, List, Optional, Union
+from ..utils.logging import get_logger
+from ..utils.progress_tracker import get_progress_tracker
+from .inference_engine import InferenceEngine, InferenceStrategy
+from .rule_manager import Rule
+
+class Reasoner:
+ """
+ High-level Reasoner class for knowledge graph inference.
+
+ This class provides a unified interface for applying reasoning rules to facts
+ or knowledge graphs.
+ """
+
+ def __init__(self, strategy: str = "forward", **kwargs):
+ """
+ Initialize the Reasoner.
+
+ Args:
+ strategy: Inference strategy ("forward", "backward", etc.)
+ **kwargs: Additional configuration options
+ """
+ self.logger = get_logger("reasoner")
+ self.progress_tracker = get_progress_tracker()
+ self.strategy = strategy
+ self.config = kwargs
+
+ # Initialize the underlying engine based on strategy
+ # Currently defaults to InferenceEngine for forward/backward chaining
+ if strategy in ["forward", "backward"]:
+ self.engine = InferenceEngine(strategy=strategy, **kwargs)
+ else:
+ # Default to forward chaining if unknown strategy
+ self.engine = InferenceEngine(strategy="forward", **kwargs)
+
+ def infer_facts(
+ self,
+ facts: Union[List[Any], Dict[str, Any]],
+ rules: Optional[List[Union[str, Rule]]] = None
+ ) -> List[Any]:
+ """
+ Infer new facts from existing facts or a knowledge graph.
+
+ Args:
+ facts: List of initial facts or a knowledge graph dictionary.
+ If a list is provided, it can contain strings, dicts, or MergeOperation objects.
+ rules: List of rules to apply (strings or Rule objects)
+
+ Returns:
+ List of inferred facts (conclusions)
+ """
+ # Pre-process facts if they come from a knowledge graph or are MergeOperation objects
+ processed_facts = []
+
+ if isinstance(facts, dict):
+ # If it's a knowledge graph, extract facts from entities and relationships
+ if "entities" in facts:
+ for entity in facts["entities"]:
+ # Create simple fact string for reasoning
+ # e.g., Person(John)
+ name = entity.get("name", entity.get("id"))
+ etype = entity.get("type", "Entity")
+ processed_facts.append(f"{etype}({name})")
+
+ if "relationships" in facts:
+ for rel in facts["relationships"]:
+ # Create simple relationship fact string
+ # e.g., WorksFor(John, Acme)
+ source = rel.get("source_name", rel.get("source_id"))
+ target = rel.get("target_name", rel.get("target_id"))
+ rtype = rel.get("type", "Relationship")
+ processed_facts.append(f"{rtype}({source}, {target})")
+
+ elif isinstance(facts, list):
+ for item in facts:
+ # Check if it's a MergeOperation (which has a merged_entity)
+ if hasattr(item, "merged_entity"):
+ entity = item.merged_entity
+ name = entity.get("name", entity.get("id"))
+ etype = entity.get("type", "Entity")
+ processed_facts.append(f"{etype}({name})")
+ elif isinstance(item, dict):
+ # It's an entity dictionary
+ name = item.get("name", item.get("id"))
+ etype = item.get("type", "Entity")
+ processed_facts.append(f"{etype}({name})")
+ else:
+ # Assume it's already a fact string or compatible object
+ processed_facts.append(item)
+
+ if not processed_facts:
+ processed_facts = facts if isinstance(facts, list) else []
+
+ tracking_id = self.progress_tracker.start_tracking(
+ module="reasoning",
+ submodule="Reasoner",
+ message=f"Inferring facts from {len(processed_facts)} processed facts"
+ )
+
+ try:
+ # Add rules if provided
+ if rules:
+ for rule in rules:
+ self.engine.add_rule(rule)
+
+ # Perform inference
+ # The engine.infer method (or forward_chain) returns InferenceResult objects
+ # We want to return the actual facts (conclusions)
+ if self.strategy == "forward":
+ results = self.engine.forward_chain(processed_facts)
+ else:
+ # Fallback to general infer method
+ results = self.engine.infer(processed_facts, rules)
+
+ # Extract conclusions from results
+ inferred_facts = [result.conclusion for result in results]
+
+ self.progress_tracker.stop_tracking(
+ tracking_id,
+ status="completed",
+ message=f"Inferred {len(inferred_facts)} new facts"
+ )
+
+ return inferred_facts
+
+ except Exception as e:
+ self.progress_tracker.stop_tracking(
+ tracking_id,
+ status="failed",
+ message=str(e)
+ )
+ self.logger.error(f"Inference failed: {e}")
+ raise
+
+ def add_rule(self, rule: Union[str, Rule]) -> None:
+ """Add a rule to the reasoner."""
+ self.engine.add_rule(rule)
+
+ def clear(self) -> None:
+ """Clear facts and rules."""
+ self.engine.reset()