engine.api

Public construction API for canonical RDFLib reasoners.

This module is the stable assembly surface for callers that want to construct a reasoner-backed RDFLib dataset, store, or per-context RETE engine factory without wiring the lower-level RETE components by hand. Start with a ReasonerSpecification constant such as RDFS_RETE_MATERIALIZED, OWL2_RL_RETE_MATERIALIZED, or NO_RULES, then ask it for the RDFLib integration object you need.

Examples

Create a materializing RDFS dataset backed by RDFLib’s in-memory store:

from engine.api import RDFS_RETE_MATERIALIZED

dataset = RDFS_RETE_MATERIALIZED.dataset()
graph = dataset.default_graph

Wrap an existing RDFLib store with the canonical RDFS reasoner:

from rdflib.plugins.stores.memory import Memory
from engine.api import RDFS_RETE_MATERIALIZED

store = RDFS_RETE_MATERIALIZED.store(Memory())

Advanced callers that coordinate contexts directly can request a fresh engine factory and construct per-context engines:

factory = RDFS_RETE_MATERIALIZED.engine_factory()
engine = factory.new_engine(dataset.default_graph.identifier)

ReasonerSpecification documents extending specification constants to add new rules, logical predicates, or observational callbacks.

The low-level RETEEngine, RETEEngineFactory, RETEStore, and RETE error classes are re-exported here for convenience, but ordinary callers should prefer the typed specifications over assembling those pieces directly.

Attributes

ContradictionPolicy

NO_RULES

No-op RETE-backed reasoner specification with an empty rule set.

RDFS_RETE_MATERIALIZED

Canonical materializing RDFS RETE reasoner specification.

OWL2_RL_RETE_MATERIALIZED

Canonical materializing OWL 2 RL RETE reasoner specification.

Classes

ReasonerSpecification

Canonical configuration for assembling an RDFLib-backed reasoner.

Module Contents

engine.api.ContradictionPolicy
class engine.api.ReasonerSpecification

Canonical configuration for assembling an RDFLib-backed reasoner.

A specification encapsulates the process of assembling a rdflib.Dataset with rdflib-reasoning enabled. There is a lot that is configurable and boilerplate during this assembly process, most of which is only relevant to advanced use cases. Using a ReasonerSpecification is the preferred mechanism if you just need to obtain a ready-to-use reasoner.

A common use case is to add a few domain-specific rules and/or callbacks to an existing reasoner specification. Use extend() for a simple way to add rules and builtins to an existing specification.

Most advanced configurations can be supported by extending any of the existing reasoner specifications using dataclasses.replace(). Specifications are immutable dataclasses.dataclass`es. The following example demonstrates the same behavior as the :func:`extend() helper, extending the default RDFS reasoner with a new rule which uses custom callbacks and predicates:

from dataclasses import replace

from engine.api import RDFS_RETE_MATERIALIZED

spec = replace(
    RDFS_RETE_MATERIALIZED,
    rules=tuple(RDFS_RETE_MATERIALIZED.rules) + (your_rule,),
    builtins=RDFS_RETE_MATERIALIZED.builtins.extend(
        more_callbacks,
        more_predicates,
    ),
)
name: str
rules: collections.abc.Sequence[engine.rules.Rule]
builtins: engine.builtins.Builtins
literal_subject_policy: engine.rete_engine.LiteralSubjectPolicy
predicate_term_policy: engine.rete_engine.PredicateTermPolicy
derivation_logger_factory: collections.abc.Callable[[axiom.common.ContextIdentifier], engine.derivation.DerivationLogger]
contradiction_recorder_factory: collections.abc.Callable[[axiom.common.ContextIdentifier], engine.derivation.ContradictionRecorder]
materializes_inferences: bool
supports_retraction: bool
supports_derivation_logging: bool
contradiction_policy: ContradictionPolicy
engine_factory_config() → engine.rete_factory.RETEEngineFactoryConfig

Return a fresh explicit factory configuration for this specification.

engine_factory() → engine.rete_factory.RETEEngineFactory

Return a fresh RETE engine factory using a fresh configuration.

extend(more_rules: collections.abc.Iterable[engine.rules.Rule], more_callbacks: collections.abc.Mapping[str, engine.builtins.CallbackHook] = {}, more_predicates: collections.abc.Mapping[str, engine.builtins.PredicateHook] = {}) → ReasonerSpecification

Return a new reasoner specification with the additional rules.

This is the preferred way to take a reasoning profile and add a few domain-specific inference rules and/or callbacks. For example, if you simply want RDFS entailment plus some callback triggers, you can do:

from engine.api import RDFS_RETE_MATERIALIZED

spec = RDFS_RETE_MATERIALIZED.extend(
    more_rules=(your_rule,),
    more_callbacks={"your_callback": your_callback},
)
Parameters:
  • more_rules – Additional rules to add to the reasoner specification.

  • more_callbacks – Additional callbacks if needed by the new rules.

  • more_predicates – Additional predicates if needed by the new rules.

Returns:

A new reasoner specification with the additional rules and builtins.

store(backing_store: rdflib.store.Store) → engine.rete_store.RETEStore

Return a new materializing RETE store over the supplied backing store using a fresh engine factory.

dataset(backing_store: rdflib.store.Store | None = None) → rdflib.Dataset

Return a new RDFLib dataset backed by this reasoner specification using a new RETE store.

engine.api.NO_RULES: Final[ReasonerSpecification]

No-op RETE-backed reasoner specification with an empty rule set.

This profile constructs the same RDFLib RETEStore integration used by reasoning profiles, but supplies no inference rules. It is useful when callers want a canonical specification object, store, or dataset without entailment. Ordinary RDFLib graph mutation still works, and asserted triples can be retracted.

This specification’s defaults intend for it to be extended. By default, no builtins are provided, derivation logging is disabled, contradictions are raised, and strict RDF term validation is enforced. Stores and datasets constructed from this specification materialize inference closure through ordinary RDFLib graph mutation and support dependency-aware retraction of asserted triples.

engine.api.RDFS_RETE_MATERIALIZED: Final[ReasonerSpecification]

Canonical materializing RDFS RETE reasoner specification.

Extends the NO_RULES specification with the production RDFS rule set and provides the default Builtins. This specification is is intended to be extended in cases where you want RDFS entailment plus some domain-specific rules and/or callbacks.

engine.api.OWL2_RL_RETE_MATERIALIZED: Final[ReasonerSpecification]

Canonical materializing OWL 2 RL RETE reasoner specification.

Extends the NO_RULES specification with OWL2_RL_RULES (entailment families plus contradiction diagnostics) and the default Builtins. Contradiction-producing rules use the dual-channel diagnostics path; the default recorder policy raises when a contradiction is detected.