owlapy

Submodules

Classes

RDFLibReasoner

A pure Python OWL reasoner based on RDFLib.

Functions

owl_expression_to_sparql(→ str)

Convert an OWL Class Expression (https://www.w3.org/TR/owl2-syntax/#Class_Expressions) into a SPARQL query

owl_expression_to_sparql_with_confusion_matrix(→ str)

Convert an OWL Class Expression (https://www.w3.org/TR/owl2-syntax/#Class_Expressions) into a SPARQL query

get_dl_expressivity(→ str)

Compute the DL expressivity name of an ontology, e.g. "ALCHN(D)".

dl_to_owl_expression(dl_expression, namespace[, prefixes])

manchester_to_owl_expression(manchester_expression, ...)

owl_expression_to_dl(→ str)

owl_expression_to_manchester(→ str)

Package Contents

owlapy.owl_expression_to_sparql(expression: owlapy.class_expression.OWLClassExpression = None, root_variable: str = '?x', values: Iterable[owlapy.owl_individual.OWLNamedIndividual] | None = None, for_all_de_morgan: bool = True, named_individuals: bool = False, validate: bool = False) str[source]

Convert an OWL Class Expression (https://www.w3.org/TR/owl2-syntax/#Class_Expressions) into a SPARQL query root variable: the variable that will be projected expression: the class expression to be transformed to a SPARQL query

values: positive or negative examples from a class expression problem. Unclear for_all_de_morgan: if set to True, the SPARQL mapping will use the mapping containing the nested FILTER NOT EXISTS patterns for the universal quantifier (¬(∃r.¬C)), instead of the counting query named_individuals: if set to True, the generated SPARQL query will return only entities that are instances of owl:NamedIndividual validate: if set to True, validates the generated SPARQL query using rdflib.parseQuery (slower but safer)

owlapy.owl_expression_to_sparql_with_confusion_matrix(expression: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual] | None, negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual] | None, root_variable: str = '?x', for_all_de_morgan: bool = True, named_individuals: bool = False, validate: bool = False) str[source]

Convert an OWL Class Expression (https://www.w3.org/TR/owl2-syntax/#Class_Expressions) into a SPARQL query root variable: the variable that will be projected expression: the class expression to be transformed to a SPARQL query positive_examples: positive examples from a class expression problem negative_examples: positive examples from a class expression problem for_all_de_morgan: if set to True, the SPARQL mapping will use the mapping containing the nested FILTER NOT EXISTS patterns for the universal quantifier (¬(∃r.¬C)), instead of the counting query named_individuals: if set to True, the generated SPARQL query will return only entities that are instances of owl:NamedIndividual validate: if set to True, validates the generated SPARQL query using rdflib.parseQuery (slower but safer)

owlapy.get_dl_expressivity(ontology: owlapy.owl_ontology.SyncOntology) str[source]

Compute the DL expressivity name of an ontology, e.g. "ALCHN(D)".

Parameters:

ontology – A SyncOntology instance.

Returns:

The standard Description Logic name for the ontology’s expressivity.

Raises:

TypeError – If ontology is not backed by OWLAPI (only SyncOntology is supported, since it is the only ontology backend that exposes RBox axioms).

class owlapy.RDFLibReasoner(ontology: owlapy.abstracts.abstract_owl_ontology.AbstractOWLOntology | str, *, class_cache: bool = True, property_cache: bool = True, infer_property_values: bool = False, infer_data_property_values: bool = False)[source]

Bases: owlapy.abstracts.abstract_owl_reasoner.AbstractOWLReasoner

A pure Python OWL reasoner based on RDFLib.

This reasoner uses SPARQL queries over an RDF graph to perform reasoning tasks. It’s designed as a drop-in replacement for StructuralReasoner with better predictability and no owlready2/Java dependencies for basic operations.

Features: - Pure Python implementation using RDFLib - SPARQL-based reasoning - Efficient graph traversal with caching - Support for basic OWL 2 class expressions - Direct and indirect hierarchy navigation

Limitations: - Does not perform OWL 2 DL inference (use HermiT/Pellet via SyncReasoner for that) - Best suited for ontology navigation and simple instance retrieval - Does not handle all complex class expressions (nominals, cardinalities may be limited)

instances(ce: owlapy.class_expression.OWLClassExpression, direct: bool = False, timeout: int = 1000) Iterable[owlapy.owl_individual.OWLNamedIndividual][source]

Get individuals that are instances of the specified class expression.

Parameters:
  • ce – The class expression whose instances are to be retrieved.

  • direct – If True, retrieve only direct instances (not implemented - returns all).

  • timeout – Timeout in milliseconds (for compatibility, not enforced).

Returns:

Iterable of named individuals that are instances of ce.

sub_classes(ce: owlapy.class_expression.OWLClassExpression, direct: bool = False, only_named: bool = True) Iterable[owlapy.class_expression.OWLClassExpression][source]

Get subclasses of the specified class expression.

Parameters:
  • ce – The class expression whose subclasses are to be retrieved.

  • direct – If True, only direct subclasses; if False, all descendant subclasses.

  • only_named – If True, only return named classes (OWLClass instances).

Returns:

Iterable of subclass expressions.

super_classes(ce: owlapy.class_expression.OWLClassExpression, direct: bool = False, only_named: bool = True) Iterable[owlapy.class_expression.OWLClassExpression][source]

Get superclasses of the specified class expression.

Parameters:
  • ce – The class expression whose superclasses are to be retrieved.

  • direct – If True, only direct superclasses; if False, all ancestor superclasses.

  • only_named – If True, only return named classes (OWLClass instances).

Returns:

Iterable of superclass expressions.

equivalent_classes(ce: owlapy.class_expression.OWLClassExpression, only_named: bool = True) Iterable[owlapy.class_expression.OWLClassExpression][source]

Get classes equivalent to the specified class expression.

Parameters:
  • ce – The class expression.

  • only_named – If True, only return named classes.

Returns:

Iterable of equivalent class expressions.

disjoint_classes(ce: owlapy.class_expression.OWLClassExpression, only_named: bool = True) Iterable[owlapy.class_expression.OWLClassExpression][source]

Get classes disjoint with the specified class expression.

Parameters:
  • ce – The class expression.

  • only_named – If True, only return named classes.

Returns:

Iterable of disjoint class expressions.

object_property_values(ind: owlapy.owl_individual.OWLNamedIndividual, pe: owlapy.owl_property.OWLObjectPropertyExpression, direct: bool = True) Iterable[owlapy.owl_individual.OWLNamedIndividual][source]

Get object property values for an individual.

Parameters:
  • ind – The individual.

  • pe – The object property expression.

  • direct – Whether to consider only direct assertions.

Returns:

Iterable of individuals that are values of the property for ind.

flush() None[source]

Flush all cached data and reload the ontology.

get_root_ontology() owlapy.abstracts.abstract_owl_ontology.AbstractOWLOntology[source]

Gets the root ontology that is loaded into this reasoner.

types(ind: owlapy.owl_individual.OWLNamedIndividual, direct: bool = False) Iterable[owlapy.class_expression.OWLClass][source]

Gets the named classes which are (potentially direct) types of the specified named individual.

Parameters:
  • ind – The individual whose types are to be retrieved.

  • direct – If True, only direct types; if False, all types.

Returns:

Iterable of OWLClass representing the types of the individual.

data_property_domains(pe, direct: bool = False)[source]

Gets the class expressions that are the domains of this data property.

object_property_domains(pe, direct: bool = False)[source]

Gets the class expressions that are the domains of this object property.

object_property_ranges(pe, direct: bool = False)[source]

Gets the class expressions that are the ranges of this object property.

data_property_values(e, pe)[source]

Gets the data property values for the specified entity and data property.

different_individuals(ind: owlapy.owl_individual.OWLNamedIndividual)[source]

Gets the individuals that are different from the specified individual.

same_individuals(ind: owlapy.owl_individual.OWLNamedIndividual)[source]

Gets the individuals that are the same as the specified individual.

equivalent_object_properties(op)[source]

Gets the object properties that are equivalent to the specified object property.

equivalent_data_properties(dp)[source]

Gets the data properties that are equivalent to the specified data property.

disjoint_object_properties(op)[source]

Gets the object properties that are disjoint with the specified object property.

disjoint_data_properties(dp)[source]

Gets the data properties that are disjoint with the specified data property.

sub_data_properties(dp, direct: bool = False)[source]

Gets the sub data properties of the specified data property.

super_data_properties(dp, direct: bool = False)[source]

Gets the super data properties of the specified data property.

sub_object_properties(op, direct: bool = False)[source]

Gets the sub object properties of the specified object property.

super_object_properties(op, direct: bool = False)[source]

Gets the super object properties of the specified object property.

__repr__()[source]
owlapy.dl_to_owl_expression(dl_expression: str, namespace: str, prefixes: Mapping[str, str | owlapy.namespaces.Namespaces] | None = None)[source]
owlapy.manchester_to_owl_expression(manchester_expression: str, namespace: str, prefixes: Mapping[str, str | owlapy.namespaces.Namespaces] | None = None)[source]
owlapy.owl_expression_to_dl(o: owlapy.owl_object.OWLObject) str[source]
owlapy.owl_expression_to_manchester(o: owlapy.owl_object.OWLObject) str[source]