engine.builtins =============== .. py:module:: engine.builtins .. autoapi-nested-parse:: Expose Python hooks for RETE rule predicates and callbacks. Builtins are split into read-only :class:`~rdflib_reasoning.engine.PredicateHook` used while evaluating rule conditions and :class:`~rdflib_reasoning.engine.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 ---------- .. autoapisummary:: engine.builtins.DEFAULT_CALLBACK_BUILTINS engine.builtins.DEFAULT_PREDICATE_BUILTINS Classes ------- .. autoapisummary:: engine.builtins.RuleContext engine.builtins.PredicateHook engine.builtins.CallbackHook engine.builtins.DifferentTermsPredicate engine.builtins.NotLiteralPredicate engine.builtins.IsLiteralPredicate engine.builtins.SameTermPredicate engine.builtins.TermInPredicate engine.builtins.TermNotInPredicate engine.builtins.LiteralSameValuePredicate engine.builtins.LiteralDifferentValuePredicate engine.builtins.LiteralInDatatypePredicate engine.builtins.LiteralNotInDatatypePredicate engine.builtins.Builtins Module Contents --------------- .. py:class:: RuleContext Read-only context passed to public predicates and callbacks. This provides an abstraction similar to Jackson's `DeserializationContext `_. .. py:method:: report_input_mismatch(message: str, inputs: collections.abc.Sequence[rdflib.term.Node]) -> Never Report a generic input error with formatted RDFLib node arguments. .. py:method:: 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. .. py:class:: PredicateHook Bases: :py:obj:`abc.ABC` Public read-only predicate hook used in rule conditions. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool :abstractmethod: Return whether the predicate holds for ``args`` in ``context``. .. py:class:: CallbackHook Bases: :py:obj:`abc.ABC` Public non-logical callback hook attached to a rule match. .. py:method:: run(context: RuleContext, *args: rdflib.term.Node) -> None :abstractmethod: Run the callback for ``args`` in ``context`` without mutating logical closure. .. py:class:: DifferentTermsPredicate Bases: :py:obj:`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 :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.SameTermPredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param args: A sequence of RDF nodes. It is an error to call this predicate with less than two arguments. :return: False when any two nodes in the sequence are identical; True otherwise. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether every argument is a distinct RDF node. .. py:class:: NotLiteralPredicate Bases: :py:obj:`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 :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.IsLiteralPredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param args: A sequence of RDF nodes. It is an error to call this predicate with less than one argument. :return: False when any argument is an RDF literal; True otherwise. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether no argument is an RDF literal. .. py:class:: IsLiteralPredicate Bases: :py:obj:`PredicateHook` True when all arguments are RDF literals. By default, registered as "is_literal" in :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.NotLiteralPredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param 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. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether every argument is an RDF literal. .. py:class:: SameTermPredicate Bases: :py:obj:`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 :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.DifferentTermsPredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param args: A sequence of RDF nodes. It is an error to call this predicate with less than two arguments. :return: False when any two nodes in the sequence are different; True otherwise. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether every argument is the same RDF node. .. py:class:: TermInPredicate Bases: :py:obj:`PredicateHook` True when the first term equals one of the remaining terms. By default, registered as "term_in" in :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.TermNotInPredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param args: A sequence of RDF nodes. It is an error to call this predicate with less than two arguments. :return: True when the first term equals one of the remaining terms. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether the first argument equals one of the remaining arguments. .. py:class:: TermNotInPredicate Bases: :py:obj:`PredicateHook` True when the first term equals none of the remaining terms. By default, registered as "term_not_in" in :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.TermInPredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param args: A sequence of RDF nodes. It is an error to call this predicate with less than two arguments. :return: False when the first term equals one of the remaining terms; True otherwise. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether the first argument equals none of the remaining arguments. .. py:class:: LiteralSameValuePredicate Bases: :py:obj:`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 :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.LiteralDifferentValuePredicate` for the related inequality predicate. :param context: The read-only context passed to the predicate. :param args: Exactly two RDF nodes. :returns: True when both arguments are literals that denote the same data value. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether two literals denote the same data value. .. py:class:: LiteralDifferentValuePredicate Bases: :py:obj:`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 :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.LiteralSameValuePredicate` for the related equality predicate. :param context: The read-only context passed to the predicate. :param args: Exactly two RDF nodes. :returns: True when both arguments are literals that denote different data values. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether two literals denote different data values. .. py:class:: LiteralInDatatypePredicate Bases: :py:obj:`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 :func:`~rdflib_reasoning.engine.owl2_rl_datatypes.literal_value_in_datatype`. By default, registered as "literal_in_datatype" in :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.LiteralNotInDatatypePredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param 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. .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether a literal's value is in a datatype value space. .. py:class:: LiteralNotInDatatypePredicate Bases: :py:obj:`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 :data:`~rdflib_reasoning.engine.builtins.DEFAULT_PREDICATE_BUILTINS`. See :class:`~rdflib_reasoning.engine.builtins.LiteralInDatatypePredicate` for the logical inverse of this predicate. :param context: The read-only context passed to the predicate. :param args: Exactly two RDF nodes ``(literal, datatype_iri)``. :returns: True when :class:`~rdflib_reasoning.engine.builtins.LiteralInDatatypePredicate` is False for the same arguments (including arity-valid non-membership). .. py:method:: test(context: RuleContext, *args: rdflib.term.Node) -> bool Return whether a literal's value is outside a datatype value space. .. py:data:: DEFAULT_CALLBACK_BUILTINS :type: Final[collections.abc.Mapping[str, CallbackHook]] Default callbacks available to registries. At this time, no default callbacks are provided. .. py:data:: DEFAULT_PREDICATE_BUILTINS :type: 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. .. py:class:: Builtins Registry for host-provided predicates and callbacks. Registries are immutable :func:`dataclasses.dataclass` instances, but they can be extended/overridden with :meth:`~rdflib_reasoning.engine.builtins.Builtins.extend`. The no-args initializer for ``Builtins`` defaults to using :data:`DEFAULT_CALLBACK_BUILTINS` and :data:`DEFAULT_PREDICATE_BUILTINS`. :attr:`~rdflib_reasoning.engine.builtins.Builtins.EMPTY` is also provided as a convenience. .. py:attribute:: callbacks :type: collections.abc.Mapping[str, CallbackHook] .. py:attribute:: predicates :type: collections.abc.Mapping[str, PredicateHook] .. py:attribute:: EMPTY :type: ClassVar[Self] .. py:method:: 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. .. py:method:: extend_defaults(more_callbacks: collections.abc.Mapping[str, CallbackHook] = {}, more_predicates: collections.abc.Mapping[str, PredicateHook] = {}) -> Self :classmethod: 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. :param more_callbacks: Additional callbacks to add to the registry. :param more_predicates: Additional predicates to add to the registry. :return: A new builtins registry.