owlapy.converter

Format converter.

Attributes

converter

Classes

VariablesMapping

Helper class for owl-to-sparql conversion.

Owl2SparqlConverter

Convert OWL class expressions to SPARQL queries.

Functions

peek(x)

Peek the last element of an array.

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

Module Contents

owlapy.converter.peek(x)[source]

Peek the last element of an array.

Returns:

The last element arr[-1].

class owlapy.converter.VariablesMapping[source]

Helper class for owl-to-sparql conversion.

__slots__ = ('class_cnt', 'prop_cnt', 'ind_cnt', 'dict')
class_cnt = 0
prop_cnt = 0
ind_cnt = 0
dict
get_variable(e: owlapy.owl_object.OWLEntity) → str[source]
new_individual_variable() → str[source]
new_property_variable() → str[source]
__contains__(item: owlapy.owl_object.OWLEntity) → bool[source]
__getitem__(item: owlapy.owl_object.OWLEntity) → str[source]
class owlapy.converter.Owl2SparqlConverter[source]

Convert OWL class expressions to SPARQL queries.

This class converts OWL class expressions from the owlapy model into equivalent SPARQL queries. It maintains internal state for variable management, parent-child relationships, and query building.

The converter uses a recursive approach to traverse the class expression tree and generates SPARQL patterns for each type of class expression (e.g., OWLClass, OWLObjectSomeValuesFrom, OWLObjectIntersectionOf, etc.).

ce

The root OWL class expression being converted

sparql

List of SPARQL query patterns being constructed

variables

List of variable names used in the query

parent

Stack of parent class expressions during traversal

parent_var

Stack of parent variable names during traversal

variable_entities

Set of OWL entities associated with variables

properties

Mapping from expression indices to their properties

_intersection

Mapping tracking whether expressions are in intersections

mapping

Variable mapping manager for De Morgan transformations

grouping_vars

Variables used in GROUP BY clauses for cardinality restrictions

having_conditions

HAVING clause conditions for cardinality restrictions

cnt

Counter for generating unique variable names

for_all_de_morgan

Whether to apply De Morgan’s laws for universal quantification

named_individuals

Whether to restrict results to named individuals only

__slots__ = ('ce', 'sparql', 'variables', 'parent', 'parent_var', 'properties', 'variable_entities', 'cnt',...
ce: owlapy.class_expression.OWLClassExpression
sparql: List[str]
variables: List[str]
parent: List[owlapy.class_expression.OWLClassExpression]
parent_var: List[str]
variable_entities: Set[owlapy.owl_object.OWLEntity]
properties: Dict[int, List[owlapy.owl_object.OWLEntity]]
mapping: VariablesMapping
grouping_vars: Dict[owlapy.class_expression.OWLClassExpression, Set[str]]
having_conditions: Dict[owlapy.class_expression.OWLClassExpression, Set[str]]
cnt: int
for_all_de_morgan: bool
named_individuals: bool
negation_default: bool
sub_property_resolver: Callable | None
inverse_property_resolver: Callable | None
subclass_resolver: Callable | None
convert(root_variable: str, ce: owlapy.class_expression.OWLClassExpression, for_all_de_morgan: bool = True, named_individuals: bool = False, negation_default: bool = True, sub_property_resolver: Callable | None = None, inverse_property_resolver: Callable | None = None, subclass_resolver: Callable | None = None)[source]

Used to convert owl class expression to SPARQL syntax.

Parameters:
  • root_variable (str) – Root variable name that will be used in SPARQL query.

  • ce (OWLClassExpression) – The owl class expression to convert.

  • named_individuals (bool) – If ‘True’ return only entities that are instances of owl:NamedIndividual.

  • negation_default (bool) – If ‘True’ (closed world, the default), OWLObjectComplementOf is translated to a FILTER NOT EXISTS pattern. If ‘False’ (open world), OWLObjectComplementOf always contributes no results, since under OWA nothing can be inferred to NOT be a member of a class without an explicit negative assertion.

  • sub_property_resolver – Optional callable mapping an OWLObjectProperty/OWLDataProperty to its sub-properties. When provided, every rendered property predicate is expanded into a SPARQL property-path alternation of itself and all of its sub-properties, so that queries also match individuals connected via a (semantically entailing) sub-property.

  • inverse_property_resolver – Optional callable mapping an OWLObjectProperty to its declared owl:inverseOf partner(s). When provided, every rendered object property predicate is additionally expanded with a reverse (^) property-path alternative for each declared inverse, so that a property with no physically-asserted triples of its own (but a declared inverse that does) is still correctly entailed.

  • subclass_resolver – Optional callable mapping an OWLClass to its subclasses. When provided, every named-class membership triple (?x a <cls>) is expanded to also match any subclass of cls, via a VALUES clause, so that individuals typed only at a leaf subclass are not silently missed.

Returns:

The SPARQL query.

Return type:

list[str]

property modal_depth
abstractmethod render(e)[source]
stack_variable(var)[source]
stack_parent(parent: owlapy.class_expression.OWLClassExpression)[source]
property current_variable
abstractmethod process(ce: owlapy.class_expression.OWLClassExpression)[source]
forAll(ce: owlapy.class_expression.OWLObjectAllValuesFrom)[source]
forAllDeMorgan(ce: owlapy.class_expression.OWLObjectAllValuesFrom)[source]
new_count_var() → str[source]
append_triple(subject, predicate, object_)[source]
append(frag)[source]
triple(subject, predicate, object_)[source]
as_query(root_variable: str, ce: owlapy.class_expression.OWLClassExpression, for_all_de_morgan: bool = True, count: bool = False, values: Iterable[owlapy.owl_individual.OWLNamedIndividual] | None = None, named_individuals: bool = False, validate: bool = False, negation_default: bool = True, sub_property_resolver: Callable | None = None, inverse_property_resolver: Callable | None = None, subclass_resolver: Callable | None = None) → str[source]
as_confusion_matrix_query(root_variable: str, ce: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], for_all_de_morgan: bool = True, named_individuals: bool = False, validate: bool = False) → str[source]
owlapy.converter.converter
owlapy.converter.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, negation_default: bool = True, sub_property_resolver: Callable | None = None, inverse_property_resolver: Callable | None = None, subclass_resolver: Callable | None = None) → 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) negation_default: if True (closed world, the default), OWLObjectComplementOf is translated via FILTER NOT EXISTS; if False (open world), OWLObjectComplementOf always contributes no results. sub_property_resolver: optional callable mapping a property to its sub-properties, used to expand every rendered property into a SPARQL property-path alternation of itself and its sub-properties. inverse_property_resolver: optional callable mapping an object property to its declared owl:inverseOf partner(s), used to expand every rendered object property with a reverse (^) path alternative. subclass_resolver: optional callable mapping a class to its subclasses, used to expand every named class membership triple to also match individuals typed only at a subclass.

owlapy.converter.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)