Complete Guide to Reasoning Engines in Semantica: 9 Built-In Inference Strategies
Semantica provides nine distinct reasoning engines—ReteEngine, SPARQLReasoner, TemporalReasoningEngine, OntologyEngine, GraphReasoner, DatalogReasoner, AbductiveReasoner, DeductiveReasoner, and ReasoningEngineWithProvenance—each implementing a specific inference strategy accessible via a unified Python interface or the CLI.
The semantica-agi/semantica repository ships with a modular collection of purpose-built reasoning engines that plug into the framework via a unified engine contract. Every engine accepts collections of Facts and exposes standard methods such as match_patterns, execute_matches, and reset, allowing you to swap inference strategies without changing your data model.
Core Forward-Chaining and Rule Engines
ReteEngine
ReteEngine (semantica/reasoning/rete_engine.py) implements the Rete algorithm for efficient forward-chaining rule matching. This engine builds a network of alpha, beta, and terminal nodes to minimize rule re-evaluation when facts change, making it ideal for high-throughput scenarios with large rule sets.
from semantica.reasoning.rete_engine import ReteEngine, Fact, Rule
engine = ReteEngine()
engine.build_network([Rule(...), ...])
engine.add_fact(Fact("f1", "Person", ["John"]))
matches = engine.match_patterns()
print(matches)
DeductiveReasoner
DeductiveReasoner (semantica/reasoning/deductive_reasoner.py) performs classic forward-chain deduction using rule bodies to derive new facts. Unlike the optimized Rete implementation, this engine provides a straightforward production-rule system for simpler use cases where full network optimization is unnecessary.
from semantica.reasoning.deductive_reasoner import DeductiveReasoner
engine = DeductiveReasoner()
engine.add_rule(Rule(conditions=["Person(?x)"], conclusion="Mortal(?x)"))
engine.add_fact(Fact("f1", "Person", ["Socrates"]))
engine.run()
print(engine.facts)
Query and Graph-Based Reasoners
SPARQLReasoner
SPARQLReasoner (semantica/reasoning/sparql_reasoner.py) executes SPARQL queries over external graph stores, translating query results into logical conclusions. Use this when integrating with existing SPARQL-compatible triple stores such as Blazegraph or GraphDB.
from semantica.reasoning.sparql_reasoner import SPARQLReasoner
engine = SPARQLReasoner(endpoint="http://my-graph:7200/repositories/myrepo")
results = engine.query("SELECT ?s WHERE { ?s a <http://example.org/Person> }")
print(results)
GraphReasoner
GraphReasoner (semantica/reasoning/graph_reasoner.py) offers generic graph-traversal capabilities including path finding, reachability analysis, and sub-graph extraction. This engine suits scenarios requiring custom graph analyses without deploying a full SPARQL stack.
from semantica.reasoning.graph_reasoner import GraphReasoner
g = GraphReasoner()
g.add_edge("A", "B")
g.add_edge("B", "C")
paths = g.reachable("A", "C")
print(paths)
Logic Programming and Hypothesis Generation
DatalogReasoner
DatalogReasoner (semantica/reasoning/datalog_reasoner.py) implements a bottom-up Datalog inference engine supporting recursive rules and stratified negation. This engine fits logic programming tasks requiring declarative, rule-based paradigms with recursive query capabilities.
from semantica.reasoning.datalog_reasoner import DatalogReasoner
engine = DatalogReasoner()
engine.add_rule("ancestor(X,Y) :- parent(X,Y).")
engine.add_rule("ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).")
engine.add_fact(Fact("f1", "parent", ["Bob", "Carol"]))
engine.add_fact(Fact("f2", "parent", ["Carol", "Dave"]))
print(engine.query("ancestor(Bob, Dave)"))
AbductiveReasoner
AbductiveReasoner (semantica/reasoning/abductive_reasoner.py) generates plausible hypotheses through abductive inference, explaining observed facts by proposing likely causes. This engine supports diagnostic and explanation-oriented workflows requiring "best-fit" causal reasoning.
from semantica.reasoning.abductive_reasoner import AbductiveReasoner
engine = AbductiveReasoner()
engine.add_fact(Fact("symptom1", "HasSymptom", ["Fever"]))
hypotheses = engine.abduct(target="DiseaseX")
print(hypotheses)
Specialized Domain Engines
TemporalReasoningEngine
TemporalReasoningEngine (semantica/kg/temporal_reasoning.py) provides temporal operators such as at_time, since, and before, plus consistency validation for time-annotated graphs. Use this engine for modeling histories, versioned data, or any domain where temporal validity matters.
from semantica.kg.temporal_reasoning import TemporalReasoningEngine
engine = TemporalReasoningEngine()
engine.add_fact(Fact("f1", "Employment", ["Alice", "Acme", "2020-01-01"]))
past_facts = engine.at_time("2020-06-01")
print(past_facts)
OntologyEngine
OntologyEngine (semantica/ontology/engine.py) supplies SKOS-style ontology utilities for managing concept hierarchies, broader/narrower relationships, and synonyms. This engine facilitates building controlled vocabularies and taxonomies.
from semantica.ontology.engine import OntologyEngine
onto = OntologyEngine()
onto.load_vocab("my_skos.ttl")
broader = onto.broader("http://example.org/ConceptA")
print(broader)
Provenance and Auditing
ReasoningEngineWithProvenance
ReasoningEngineWithProvenance (semantica/reasoning/reasoning_provenance.py) wraps any base engine to capture detailed provenance information including activation keys and rule lineage. This wrapper is essential for auditing, reproducibility, and scenarios requiring full traceability of inferred results.
from semantica.reasoning.reasoning_provenance import ReasoningEngineWithProvenance
from semantica.reasoning.rete_engine import ReteEngine
base_engine = ReteEngine()
engine = ReasoningEngineWithProvenance(base_engine)
engine.build_network([...])
engine.add_fact(...)
matches = engine.match_patterns()
engine.execute_matches(matches)
print(engine.provenance_report())
Using Engines via the CLI and Python API
All reasoning engines in Semantica share a common contract: they accept collections of Facts and expose methods such as match_patterns, execute_matches, and reset. You can instantiate engines directly in Python as shown above, or invoke them through the CLI using the --engine option. The CLI command semantica reason run --engine <name> validates the supplied engine name against the registry defined in semantica/cli/commands/reason.py, raising a clear error for unknown engines.
Summary
- ReteEngine (
semantica/reasoning/rete_engine.py) provides optimized forward-chaining via the Rete algorithm for high-throughput rule matching. - DeductiveReasoner offers straightforward forward-chain deduction without network optimization.
- SPARQLReasoner integrates external triple stores via SPARQL endpoints.
- GraphReasoner handles generic graph traversal and reachability.
- DatalogReasoner supports recursive, bottom-up logic programming.
- AbductiveReasoner generates explanatory hypotheses for diagnostic workflows.
- TemporalReasoningEngine manages time-aware inference with temporal operators.
- OntologyEngine processes SKOS vocabularies and concept hierarchies.
- ReasoningEngineWithProvenance wraps any engine to audit activation keys and rule lineage.
Frequently Asked Questions
How do I select the right reasoning engine for my use case?
Choose ReteEngine for large-scale production rule systems requiring performance, DatalogReasoner for recursive logic programming, SPARQLReasoner when querying external triple stores, and TemporalReasoningEngine when facts carry timestamps. For diagnostic scenarios requiring explanation generation, use AbductiveReasoner.
Can I combine multiple reasoning engines in a single Semantica workflow?
Yes. The unified engine interface allows you to instantiate multiple engines in the same Python process. You can also wrap any base engine with ReasoningEngineWithProvenance to add auditing capabilities without modifying the underlying inference logic.
What is the difference between ReteEngine and DeductiveReasoner?
ReteEngine builds an optimized discrimination network (alpha, beta, and terminal nodes) to cache partial matches and avoid redundant rule evaluations, making it suitable for high-frequency fact updates. DeductiveReasoner implements a simpler forward-chaining mechanism without the Rete network overhead, ideal for smaller rule sets or educational purposes.
How does the CLI validate engine names?
The CLI command semantica reason run --engine <name> validates the provided engine identifier against an internal registry defined in semantica/cli/commands/reason.py. If you specify an unregistered engine, the CLI raises a clear error message listing available options.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →