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
  • DatalogReasoner guarantees termination via semi-naive fixpoint evaluation
  • TemporalReasoningEngine implements all 13 Allen interval algebra relations
  • ExplanationGenerator produces 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 the Reasoner for IF/THEN forward-chaining:
Or build rules programmatically using the 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

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

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:
All 13 Allen interval algebra relations:
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 any InferenceResult:

ExplanationGenerator Methods

Key dataclass fields

Engine Selection Guide

For recursive rules (e.g. ancestor, reachability, transitivity), use DatalogReasoner: it guarantees termination via semi-naive bottom-up fixpoint evaluation. Reasoner.forward_chain() has a max_iterations cap (default 50) and will silently stop early with deep recursion.
GraphReasoner requires a configured LLM provider. If the provider fails to initialize, reason() returns an error string instead of raising. Check reasoner.provider is not None before calling if you need to surface failures explicitly.
  • 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.