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
convert(root_variable: str, ce: owlapy.class_expression.OWLClassExpression, for_all_de_morgan: bool = True, named_individuals: bool = False)[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.

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