djangordf.reasoning

Pluggable reasoner layer. Materialise inferred triples into the configured backend. Pick a reasoner via settings.DJANGORDF_REASONER (dotted import path) or pass one to djangordf.reasoning.materialize().

Pluggable reasoner layer.

Materialise inferred triples into the configured backend. Pick a reasoner via settings.DJANGORDF_REASONER (dotted import path) or pass one directly to materialize(). Built-in reasoners cover RDFS and SKOS; an optional OWLRLReasoner wraps the third-party owlrl library when installed.

class djangordf.reasoning.CompositeReasoner(*reasoners)[source]

Bases: Reasoner

Run several reasoners sequentially within one fixpoint envelope.

Parameters:

reasoners (Reasoner)

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:
class djangordf.reasoning.OWLRLReasoner(profile=None)[source]

Bases: Reasoner

Materialise via the owlrl library’s OWL-RL closure.

The reasoner loads the source graph into an in-memory rdflib.Graph, runs the OWL-RL closure, and writes the new triples back into the target graph through the backend.

Parameters:

profile (object | None)

materialize(backend=None, *, source_graph=None, target_graph=None, max_iterations=50)[source]

Run rules to a fixpoint. Returns the number of newly inferred triples added to target_graph.

Return type:

int

Parameters:

max_iterations (int)

class djangordf.reasoning.RDFSReasoner[source]

Bases: Reasoner

Run canonical RDFS entailment rules to a fixpoint.

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:
class djangordf.reasoning.Reasoner[source]

Bases: object

Base class for reasoners.

Subclasses override rules() (returning a sequence of SPARQL INSERT-WHERE strings) to declare their inference rules. The base materialize() runs them in a fixpoint loop. Override materialize() directly for non-SPARQL reasoners (see OWLRLReasoner).

materialize(backend=None, *, source_graph=None, target_graph=None, max_iterations=50)[source]

Run rules to a fixpoint. Returns the number of newly inferred triples added to target_graph.

Return type:

int

Parameters:

max_iterations (int)

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:
class djangordf.reasoning.SKOSReasoner[source]

Bases: Reasoner

Run SKOS entailment rules to a fixpoint.

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:
djangordf.reasoning.materialize(reasoner=None, backend=None, *, source_graph=None, target_graph=None, max_iterations=50)[source]

Programmatic entry point. Resolves reasoner from the argument (instance / class / dotted path) or settings.DJANGORDF_REASONER and runs materialize on it.

Return type:

int

Parameters:

Reasoner ABC plus the public materialize entry point.

class djangordf.reasoning.base.Reasoner[source]

Bases: object

Base class for reasoners.

Subclasses override rules() (returning a sequence of SPARQL INSERT-WHERE strings) to declare their inference rules. The base materialize() runs them in a fixpoint loop. Override materialize() directly for non-SPARQL reasoners (see OWLRLReasoner).

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:
materialize(backend=None, *, source_graph=None, target_graph=None, max_iterations=50)[source]

Run rules to a fixpoint. Returns the number of newly inferred triples added to target_graph.

Return type:

int

Parameters:

max_iterations (int)

class djangordf.reasoning.base.CompositeReasoner(*reasoners)[source]

Bases: Reasoner

Run several reasoners sequentially within one fixpoint envelope.

Parameters:

reasoners (Reasoner)

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:
djangordf.reasoning.base.materialize(reasoner=None, backend=None, *, source_graph=None, target_graph=None, max_iterations=50)[source]

Programmatic entry point. Resolves reasoner from the argument (instance / class / dotted path) or settings.DJANGORDF_REASONER and runs materialize on it.

Return type:

int

Parameters:

Canonical RDFS reasoner.

Five rules covering rdfs:subClassOf transitivity, type propagation through subClassOf / subPropertyOf, and domain / range inference. Implemented as SPARQL INSERT-WHERE templates so they execute against any SPARQL 1.1 backend.

class djangordf.reasoning.rdfs.RDFSReasoner[source]

Bases: Reasoner

Run canonical RDFS entailment rules to a fixpoint.

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:

SKOS-specific reasoner.

Propagates skos:broader / skos:narrower to their transitive variants and closes the transitive predicates under composition. Also enforces skos:exactMatch symmetry.

class djangordf.reasoning.skos.SKOSReasoner[source]

Bases: Reasoner

Run SKOS entailment rules to a fixpoint.

rules(source_graph, target_graph)[source]

Return the SPARQL INSERT-WHERE strings for one pass.

Return type:

Sequence[str]

Parameters:

Optional wrapper around the third-party owlrl library.

Importing this module without owlrl installed raises ImportError; the materialize() method also raises ImproperlyConfigured with an install hint if the import fails late.

class djangordf.reasoning.owlrl.OWLRLReasoner(profile=None)[source]

Bases: Reasoner

Materialise via the owlrl library’s OWL-RL closure.

The reasoner loads the source graph into an in-memory rdflib.Graph, runs the OWL-RL closure, and writes the new triples back into the target graph through the backend.

Parameters:

profile (object | None)

materialize(backend=None, *, source_graph=None, target_graph=None, max_iterations=50)[source]

Run rules to a fixpoint. Returns the number of newly inferred triples added to target_graph.

Return type:

int

Parameters:

max_iterations (int)