semantica.reasoning derives new knowledge from existing facts using logical rules:
- Six reasoning engines: forward chaining, Rete, SPARQL, Datalog, temporal, and LLM-powered GraphReasoner
- Every engine produces explainable inference paths: traceable chains of rules and facts
DatalogReasonerguarantees termination via semi-naive fixpoint evaluationTemporalReasoningEngineimplements all 13 Allen interval algebra relationsExplanationGeneratorproduces step-by-step natural-language justifications
Exported Classes
Which Engine Should I Use?
- Reasoner — IF/THEN rules, forward and backward chaining. Start here: covers 90% of use cases. No query language required.
- GraphReasoner — Natural language queries over a knowledge graph via LLM. No SPARQL or rules: just ask a question.
- DatalogReasoner — Recursive Horn clause rules with guaranteed termination. Use for complex multi-hop transitive rules.
- ReteEngine — Rete pattern matching for high-frequency inference. Use when you need to match many facts against many rules simultaneously.
- SPARQLReasoner — SPARQL query expansion and rule-based inference. Use when you’re working with RDF/OWL data.
- TemporalReasoningEngine — All 13 Allen interval algebra relations. Use for time-aware reasoning: overlaps, before/after, during, contains.
Getting Started
The most common pattern is theReasoner for IF/THEN forward-chaining:
Rule dataclass:
Reasoner (Forward/Backward Chaining)
Reasoner is the unified entry point for rule-based inference: iterates facts and rules to a fixpoint, then optionally proves a specific goal via backward chaining:
Reasoner Methods
Rules with actions use at-most-once attempt semantics per concrete activation
(rule ID, bindings, and matched facts). Calling
forward_chain() again on the
same instance does not repeat side effects for an activation that was already
attempted, even when an action raised an exception. Call
reset_action_history() to deliberately retry without clearing facts or rules;
clear() and reset() also clear this history. Replacing a rule’s actions in
place does not invalidate an existing activation; reset the history explicitly
when the replacement should be replayed.
Rule and Fact dataclass fields
GraphReasoner
GraphReasoner uses an LLM to answer natural language queries over a knowledge graph dict: no SPARQL or rule authoring required:
reason() converts the graph to a text context and calls the LLM with a structured prompt. Returns a plain string answer.
ReteEngine
High-performance Rete pattern matching for large rule sets:ReteEngine Methods
When a Reasoner is bound,
execute_matches() deduplicates action side effects
by rule ID, bindings, and matched fact identity. Re-executing a match still
returns its conclusion for compatibility, but its actions are skipped after the
first attempt. reset_action_history(), reset(), and build_network() allow
those actions to run again.
SPARQLReasoner
SPARQLReasoner extends SPARQL with inference rule expansion: add IF-THEN rules and they are automatically woven into queries before execution:
SPARQLReasoner Constructor
execute_query() returns empty bindings when no triplet_store is configured. Pass a TripletStore instance via the triplet_store= kwarg to execute queries against a live backend.DatalogReasoner
Pure-Python bottom-up semi-naive fixpoint evaluation for recursive Horn clause rules. Termination is guaranteed: the engine detects fixpoint convergence and stops:DatalogFact, DatalogRule fields
DatalogReasoner Methods
TemporalReasoningEngine
Pure-Python Allen interval algebra: all 13 relations, no LLM calls:TemporalInterval.start expects a datetime object, not a string. Import datetime from the standard library and construct intervals with datetime(year, month, day).ExplanationGenerator
Generate structured explanations for anyInferenceResult:
ExplanationGenerator Methods
Key dataclass fields
Engine Selection Guide
- Knowledge Graph — The knowledge graph being reasoned over.
- Ontology — Ontology axioms and SHACL constraints for logical reasoning.
- Triplet Store — RDF backend for SPARQL-based reasoning.
- Context — Reasoning integrated into agent decision intelligence.
