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 callbacks available to registries. |
|
Default predicates available to registries. |
Classes¶
Read-only context passed to public predicates and callbacks. |
|
Public read-only predicate hook used in rule conditions. |
|
Public non-logical callback hook attached to a rule match. |
|
True when the all arguments are distinct RDF nodes. |
|
True when all arguments are not RDF literals. |
|
True when all arguments are RDF literals. |
|
True when the all arguments are identical RDF nodes. |
|
True when the first term equals one of the remaining terms. |
|
True when the first term equals none of the remaining terms. |
|
True when two literals denote the same data value. |
|
True when two literals denote different data values. |
|
True when a literal's data value is in a datatype's value space. |
|
True when a literal's data value is not in a datatype's value space. |
|
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.ABCPublic read-only predicate hook used in rule conditions.
- abstractmethod test(context: RuleContext, *args: rdflib.term.Node) bool¶
Return whether the predicate holds for
argsincontext.
- class engine.builtins.CallbackHook¶
Bases:
abc.ABCPublic non-logical callback hook attached to a rule match.
- abstractmethod run(context: RuleContext, *args: rdflib.term.Node) None¶
Run the callback for
argsincontextwithout mutating logical closure.
- class engine.builtins.DifferentTermsPredicate¶
Bases:
PredicateHookTrue 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. SeeSameTermPredicatefor 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:
PredicateHookTrue 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. SeeIsLiteralPredicatefor 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:
PredicateHookTrue when all arguments are RDF literals.
By default, registered as “is_literal” in
DEFAULT_PREDICATE_BUILTINS. SeeNotLiteralPredicatefor 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:
PredicateHookTrue 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. SeeDifferentTermsPredicatefor 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:
PredicateHookTrue when the first term equals one of the remaining terms.
By default, registered as “term_in” in
DEFAULT_PREDICATE_BUILTINS. SeeTermNotInPredicatefor 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:
PredicateHookTrue when the first term equals none of the remaining terms.
By default, registered as “term_not_in” in
DEFAULT_PREDICATE_BUILTINS. SeeTermInPredicatefor 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:
PredicateHookTrue 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. SeeLiteralDifferentValuePredicatefor 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:
PredicateHookTrue 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. SeeLiteralSameValuePredicatefor 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:
PredicateHookTrue 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. SeeLiteralNotInDatatypePredicatefor 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:
PredicateHookTrue 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. SeeLiteralInDatatypePredicatefor 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
LiteralInDatatypePredicateis 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 withextend().The no-args initializer for
Builtinsdefaults to usingDEFAULT_CALLBACK_BUILTINSandDEFAULT_PREDICATE_BUILTINS.EMPTYis 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.