engine.builtins

Expose Python hooks for RETE rule predicates and callbacks.

Builtins are split into read-only PredicateHook used while evaluating rule conditions and CallbackHook invoked as part of rule consequents.

These hooks MUST NOT add triples, retract triples, or otherwise mutate graph state. Treat them as observational hooks for your application and/or domain logic.

You MAY have your application mutate the graph in response to a callback AFTER the engine has reached a fixed-point inference closure, but you MUST NOT do so from within the callback. The behavior if modifications are made from within the callback is undefined. Modifications following from callbacks are unmanaged and will not be tracked by the engine’s truth-maintenance mechanisms.

Attributes

DEFAULT_CALLBACK_BUILTINS

Default callbacks available to registries.

DEFAULT_PREDICATE_BUILTINS

Default predicates available to registries.

Classes

RuleContext

Read-only context passed to public predicates and callbacks.

PredicateHook

Public read-only predicate hook used in rule conditions.

CallbackHook

Public non-logical callback hook attached to a rule match.

DifferentTermsPredicate

True when the all arguments are distinct RDF nodes.

NotLiteralPredicate

True when all arguments are not RDF literals.

IsLiteralPredicate

True when all arguments are RDF literals.

SameTermPredicate

True when the all arguments are identical RDF nodes.

TermInPredicate

True when the first term equals one of the remaining terms.

TermNotInPredicate

True when the first term equals none of the remaining terms.

LiteralSameValuePredicate

True when two literals denote the same data value.

LiteralDifferentValuePredicate

True when two literals denote different data values.

LiteralInDatatypePredicate

True when a literal's data value is in a datatype's value space.

LiteralNotInDatatypePredicate

True when a literal's data value is not in a datatype's value space.

Builtins

Registry for host-provided predicates and callbacks.

Module Contents

class engine.builtins.RuleContext

Read-only context passed to public predicates and callbacks.

This provides an abstraction similar to Jackson’s DeserializationContext.

report_input_mismatch(message: str, inputs: collections.abc.Sequence[rdflib.term.Node]) → Never

Report a generic input error with formatted RDFLib node arguments.

report_argument_count(inputs: collections.abc.Sequence[rdflib.term.Node], *, min_inclusive: pydantic.NonNegativeInt | None = None, max_inclusive: pydantic.NonNegativeInt | None = None) → Never

Report an argument count error.

class engine.builtins.PredicateHook

Bases: abc.ABC

Public read-only predicate hook used in rule conditions.

abstractmethod test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether the predicate holds for args in context.

class engine.builtins.CallbackHook

Bases: abc.ABC

Public non-logical callback hook attached to a rule match.

abstractmethod run(context: RuleContext, *args: rdflib.term.Node) → None

Run the callback for args in context without mutating logical closure.

class engine.builtins.DifferentTermsPredicate

Bases: PredicateHook

True when the all arguments are distinct RDF nodes.

This does not perform a semantic comparison of the terms which would depend on the ontology and rulesets’ definitions of equality.

By default, registered as “different_terms” in DEFAULT_PREDICATE_BUILTINS. See SameTermPredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – A sequence of RDF nodes. It is an error to call this predicate with less than two arguments.

Returns:

False when any two nodes in the sequence are identical; True otherwise.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether every argument is a distinct RDF node.

class engine.builtins.NotLiteralPredicate

Bases: PredicateHook

True when all arguments are not RDF literals.

Useful to test if a node is suitable as an RDF 1.1 subject.

By default, registered as “not_literal” in DEFAULT_PREDICATE_BUILTINS. See IsLiteralPredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – A sequence of RDF nodes. It is an error to call this predicate with less than one argument.

Returns:

False when any argument is an RDF literal; True otherwise.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether no argument is an RDF literal.

class engine.builtins.IsLiteralPredicate

Bases: PredicateHook

True when all arguments are RDF literals.

By default, registered as “is_literal” in DEFAULT_PREDICATE_BUILTINS. See NotLiteralPredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – A sequence of RDF nodes. It is an error to call this predicate with less than one argument.

Returns:

False when any argument is not an RDF literal; True otherwise.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether every argument is an RDF literal.

class engine.builtins.SameTermPredicate

Bases: PredicateHook

True when the all arguments are identical RDF nodes.

This does not perform a semantic comparison of the terms which would depend on the ontology and rulesets’ definitions of equality.

By default, registered as “same_term” in DEFAULT_PREDICATE_BUILTINS. See DifferentTermsPredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – A sequence of RDF nodes. It is an error to call this predicate with less than two arguments.

Returns:

False when any two nodes in the sequence are different; True otherwise.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether every argument is the same RDF node.

class engine.builtins.TermInPredicate

Bases: PredicateHook

True when the first term equals one of the remaining terms.

By default, registered as “term_in” in DEFAULT_PREDICATE_BUILTINS. See TermNotInPredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – A sequence of RDF nodes. It is an error to call this predicate with less than two arguments.

Returns:

True when the first term equals one of the remaining terms.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether the first argument equals one of the remaining arguments.

class engine.builtins.TermNotInPredicate

Bases: PredicateHook

True when the first term equals none of the remaining terms.

By default, registered as “term_not_in” in DEFAULT_PREDICATE_BUILTINS. See TermInPredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – A sequence of RDF nodes. It is an error to call this predicate with less than two arguments.

Returns:

False when the first term equals one of the remaining terms; True otherwise.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether the first argument equals none of the remaining arguments.

class engine.builtins.LiteralSameValuePredicate

Bases: PredicateHook

True when two literals denote the same data value.

Comparison uses RDFLib value-space equality (Literal.eq), not RDFLib node identity. Non-literal arguments and incomparable or ill-typed pairs yield False so rule matching stays boolean.

By default, registered as “literal_same_value” in DEFAULT_PREDICATE_BUILTINS. See LiteralDifferentValuePredicate for the related inequality predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – Exactly two RDF nodes.

Returns:

True when both arguments are literals that denote the same data value.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether two literals denote the same data value.

class engine.builtins.LiteralDifferentValuePredicate

Bases: PredicateHook

True when two literals denote different data values.

Comparison uses RDFLib value-space inequality (Literal.neq). Non-literal arguments and incomparable or ill-typed pairs yield False so rule matching does not treat unknown inequality as proven difference.

By default, registered as “literal_different_value” in DEFAULT_PREDICATE_BUILTINS. See LiteralSameValuePredicate for the related equality predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – Exactly two RDF nodes.

Returns:

True when both arguments are literals that denote different data values.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether two literals denote different data values.

class engine.builtins.LiteralInDatatypePredicate

Bases: PredicateHook

True when a literal’s data value is in a datatype’s value space.

Only OWL 2 RL supported datatype IRIs are recognized. See literal_value_in_datatype().

By default, registered as “literal_in_datatype” in DEFAULT_PREDICATE_BUILTINS. See LiteralNotInDatatypePredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – Exactly two RDF nodes (literal, datatype_iri).

Returns:

True when the first argument is a literal whose data value is contained in the value space of the datatype IRI given as the second argument.

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether a literal’s value is in a datatype value space.

class engine.builtins.LiteralNotInDatatypePredicate

Bases: PredicateHook

True when a literal’s data value is not in a datatype’s value space.

By default, registered as “literal_not_in_datatype” in DEFAULT_PREDICATE_BUILTINS. See LiteralInDatatypePredicate for the logical inverse of this predicate.

Parameters:
  • context – The read-only context passed to the predicate.

  • args – Exactly two RDF nodes (literal, datatype_iri).

Returns:

True when LiteralInDatatypePredicate is False for the same arguments (including arity-valid non-membership).

test(context: RuleContext, *args: rdflib.term.Node) → bool

Return whether a literal’s value is outside a datatype value space.

engine.builtins.DEFAULT_CALLBACK_BUILTINS: Final[collections.abc.Mapping[str, CallbackHook]]

Default callbacks available to registries.

At this time, no default callbacks are provided.

engine.builtins.DEFAULT_PREDICATE_BUILTINS: Final[collections.abc.Mapping[str, PredicateHook]]

Default predicates available to registries.

At this time, the following predicates are provided: - different_terms: True when the all arguments are distinct RDF nodes. - not_literal: True when all arguments are not RDF literals. - is_literal: True when all arguments are RDF literals. - same_term: True when the all arguments are identical RDF nodes. - term_in: True when the first term equals one of the remaining terms. - term_not_in: True when the first term equals none of the remaining terms. - literal_same_value: True when two literals denote the same data value. - literal_different_value: True when two literals denote different data values. - literal_in_datatype: True when a literal’s value is in a datatype value space. - literal_not_in_datatype: True when a literal’s value is not in a datatype value space.

class engine.builtins.Builtins

Registry for host-provided predicates and callbacks.

Registries are immutable dataclasses.dataclass() instances, but they can be extended/overridden with extend().

The no-args initializer for Builtins defaults to using DEFAULT_CALLBACK_BUILTINS and DEFAULT_PREDICATE_BUILTINS. EMPTY is also provided as a convenience.

callbacks: collections.abc.Mapping[str, CallbackHook]
predicates: collections.abc.Mapping[str, PredicateHook]
EMPTY: ClassVar[Self]
extend(more_callbacks: collections.abc.Mapping[str, CallbackHook] | None = None, more_predicates: collections.abc.Mapping[str, PredicateHook] | None = None) → Self

Return a new registry with additional callbacks and predicates merged in.

classmethod extend_defaults(more_callbacks: collections.abc.Mapping[str, CallbackHook] = {}, more_predicates: collections.abc.Mapping[str, PredicateHook] = {}) → Self

Create a new builtins registry, overriding the defaults with the provided callbacks and predicates.

Typically, we want to extend the standard library of predicates & callbacks. This fits into cases where we want to build a custom ruleset or add rules to an existing one.

Parameters:
  • more_callbacks – Additional callbacks to add to the registry.

  • more_predicates – Additional predicates to add to the registry.

Returns:

A new builtins registry.