hermitpy

hermitpy.Reasoner

Use

Construct a Reasoner from an rdflib graph that is already parsed. Turtle, RDF/XML, and the other formats rdflib accepts all work, because the loader sees the graph, not the file.

Construct

Reasoner(graph: Graph)

Construction loads the graph. A construct outside the fragment raises UnsupportedConstruct here, before any query. Source: reasoner.py.

Methods

MethodReturnsWhen it fails
is_consistent()boolNever, for a graph that loaded. The answer is cached on the reasoner.
unsatisfiable_classes()list[str]Named class IRIs that are empty in every model, sorted. If the ontology itself is inconsistent, every named class is returned.
direct_subclasses()list[tuple[str, str]]Direct (subclass, superclass) IRI pairs, sorted. Raises InconsistentOntology when the ontology has no model.

IRIs are full strings, including http://www.w3.org/2002/07/owl#Thing and http://www.w3.org/2002/07/owl#Nothing. How those pairs are chosen is on the Classification page.

A small hierarchy

from rdflib import Graph
from hermitpy import Reasoner

graph = Graph()
graph.parse(data='''
@prefix ex: <https://example.org/> .
ex:Dog rdfs:subClassOf ex:Animal .
ex:Animal rdfs:subClassOf ex:Organism .
''', format="turtle")

reasoner = Reasoner(graph)
print(reasoner.is_consistent())
for child, parent in reasoner.direct_subclasses():
    print(child, "subClassOf", parent)

The pairs are Animal under Organism, Dog under Animal, and Organism under owl:Thing. Dog is not listed directly under Organism or owl:Thing. The same shape is examples/hierarchy.py.

An inconsistent ontology

from hermitpy import InconsistentOntology

if reasoner.is_consistent():
    pairs = reasoner.direct_subclasses()
else:
    empty = reasoner.unsatisfiable_classes()

Calling direct_subclasses on an ontology with no model raises InconsistentOntology. examples/inconsistent.py parses a file and prints is_consistent().