owlapy.marked_entity_generator_converter
Class & property generator converter – port of the Java generateClassQuery / generatePropertyQuery / SparqlBuildingVisitor.
This module provides a subclass of Owl2SparqlConverter that supports a
context position marker. When the marker appears in a class expression the
converter emits ?variable a ?class . instead of a concrete class IRI, which
makes it possible to generate SPARQL queries that discover classes rather
than retrieving instances of a fixed class.
For property discovery the same marker triggers ?variable ?prop [] .
(or [] ?prop ?variable . when inverted), mirroring the Java
Suggestor.generatePropertyQuery.
The top-level helpers owl_expression_to_class_query() and
owl_expression_to_property_query() mirror owl_expression_to_sparql()
but produce queries of the form produced by the Java suggestor.
Attributes
Classes
Extends |
Functions
Convert an OWL class expression with a |
|
Convert an OWL class expression with a |
|
Convert an OWL class expression with a |
Module Contents
- owlapy.marked_entity_generator_converter.CONTEXT_POSITION_MARKER
- class owlapy.marked_entity_generator_converter.QueryGenerator[source]
Bases:
owlapy.converter.Owl2SparqlConverterExtends
Owl2SparqlConverterwith marker-aware conversion and the ability to generategenerateClassQuery-style SPARQL queries that discover OWL classes with their positive/negative hit counts.The marker concept
CONTEXT_POSITION_MARKERcan be placed anywhere inside a class expression tree. When the converter encounters it the emitted triple pattern becomes?currentVar a ?class .instead of the usual?currentVar a <SomeConcreteIRI> .. If the marker appears inside a negation (OWLObjectComplementOf) the triple pattern is additionally wrapped inFILTER NOT EXISTS { … }, and a type axiom for?classis added (?class a owl:Class .).- __slots__ = ('ce', 'sparql', 'variables', 'parent', 'parent_var', 'properties', 'variable_entities', 'cnt',...
- convert(root_variable: str, ce: owlapy.class_expression.OWLClassExpression, for_all_de_morgan: bool = True, named_individuals: bool = False, marker_mode: bool = False, property_marker_mode: bool = False, inverted: bool = False, negated_class_marker_mode: bool = False, _preserve_mapping: bool = False)[source]
Like the parent
convertbut accepts extra marker flags.When marker_mode is
TruetheCONTEXT_POSITION_MARKERclass is treated specially – emitting?var a ?class ..When negated_class_marker_mode is
Truethe marker emits?class a owl:Class . FILTER NOT EXISTS { ?var a ?class . }instead, which discovers classes that the individual is not a member of. This mirrors the JavagenerateNegatedClassQuery.When property_marker_mode is
Truethe marker emits?var ?prop [] .(or[] ?prop ?var .when inverted isTrue).When _preserve_mapping is
True, the existingVariablesMappingcounters are preserved so that a subsequent conversion produces fresh intermediate variable names (e.g.?s_3instead of?s_1). This is essential when the positive and negative blocks appear in the same outer query scope.
- process(ce: owlapy.class_expression.OWLClassExpression)[source]
- as_class_query(context: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], root_variable_pos: str = '?pos', root_variable_neg: str = '?neg', for_all_de_morgan: bool = True, named_individuals: bool = False, filter_expression: owlapy.class_expression.OWLClassExpression | None = None, validate: bool = False) str[source]
Generate a SPARQL query that discovers OWL classes with hit-counts.
The generated query mirrors the Java
Suggestor.generateClassQuery:SELECT ?class (MAX(?tp) AS ?posHits) (COUNT(DISTINCT ?neg) AS ?negHits) WHERE { { SELECT ?class (COUNT(DISTINCT ?pos) AS ?tp) WHERE { VALUES ?pos { ... } <context pattern with ?pos as root and ?class at marker> } GROUP BY ?class } OPTIONAL { VALUES ?neg { ... } <same context pattern but with ?neg> } } GROUP BY ?class
- Parameters:
context – A class expression that must contain
CONTEXT_POSITION_MARKERat the position where?variable a ?class .should be injected.positive_examples – Positive example individuals.
negative_examples – Negative example individuals.
root_variable_pos – Variable name for positive examples (default
?pos).root_variable_neg – Variable name for negative examples (default
?neg).for_all_de_morgan – Passed through to
convert().named_individuals – Passed through to
convert().filter_expression – Optional additional class expression used to create a
FILTER NOT EXISTSconstraint on the root variable.validate – If
True, validates the generated SPARQL query usingrdflib.parseQuery(slower but safer).
- Returns:
A valid SPARQL SELECT query string.
- as_negated_class_query(context: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], root_variable_pos: str = '?pos', root_variable_neg: str = '?neg', for_all_de_morgan: bool = True, named_individuals: bool = False, filter_expression: owlapy.class_expression.OWLClassExpression | None = None, validate: bool = False) str[source]
Generate a SPARQL query that discovers OWL classes the positives are not members of, with hit-counts.
The generated query mirrors the Java
Suggestor.generateNegatedClassQuery:SELECT ?class (MAX(?tp) AS ?posHits) (MAX(?fp) AS ?negHits) WHERE { { SELECT ?class (COUNT(DISTINCT ?pos) AS ?tp) (0 AS ?fp) WHERE { VALUES ?pos { ... } ?class a owl:Class . FILTER NOT EXISTS { ?pos a ?class . } } GROUP BY ?class } UNION { SELECT ?class (0 AS ?tp) (COUNT(DISTINCT ?neg) AS ?fp) WHERE { VALUES ?neg { ... } ?class a owl:Class . FILTER NOT EXISTS { ?neg a ?class . } } GROUP BY ?class } } GROUP BY ?class
At the marker position the converter emits
?class a owl:Class . FILTER NOT EXISTS { ?var a ?class . }instead of?var a ?class ..- Parameters:
context – A class expression containing
CONTEXT_POSITION_MARKER.positive_examples – Positive example individuals.
negative_examples – Negative example individuals.
root_variable_pos – Variable name for positive examples (default
?pos).root_variable_neg – Variable name for negative examples (default
?neg).for_all_de_morgan – Passed through to
convert().named_individuals – Passed through to
convert().filter_expression – Optional additional class expression for
FILTER NOT EXISTSon the root variable.validate – If
True, validates the generated SPARQL query usingrdflib.parseQuery(slower but safer).
- Returns:
A valid SPARQL SELECT query string.
- as_property_query(context: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], root_variable_pos: str = '?pos', root_variable_neg: str = '?neg', for_all_de_morgan: bool = True, named_individuals: bool = False, inverted: bool = False, filter_expression: owlapy.class_expression.OWLClassExpression | None = None, validate: bool = False) str[source]
Generate a SPARQL query that discovers OWL properties with hit-counts.
The generated query mirrors the Java
Suggestor.generatePropertyQuery(UNION pattern):SELECT ?prop (MAX(?tp) AS ?posHits) (MAX(?fp) AS ?negHits) WHERE { { SELECT ?prop (COUNT(DISTINCT ?pos) AS ?tp) (0 AS ?fp) WHERE { VALUES ?pos { ... } <context pattern with ?prop at marker> } GROUP BY ?prop } UNION { SELECT ?prop (0 AS ?tp) (COUNT(DISTINCT ?neg) AS ?fp) WHERE { VALUES ?neg { ... } <same pattern with ?neg> } GROUP BY ?prop } } GROUP BY ?prop
- Parameters:
context – A class expression containing
CONTEXT_POSITION_MARKERat the position where?var ?prop [] .should be injected.positive_examples – Positive example individuals.
negative_examples – Negative example individuals.
root_variable_pos – Variable name for positive examples (default
?pos).root_variable_neg – Variable name for negative examples (default
?neg).for_all_de_morgan – Passed through to
convert().named_individuals – Passed through to
convert().inverted – If
True, emits[] ?prop ?var .instead of?var ?prop [] .at the marker position.filter_expression – Optional additional class expression for
FILTER NOT EXISTSon the root variable.validate – If
True, validates the generated SPARQL query usingrdflib.parseQuery(slower but safer).
- Returns:
A valid SPARQL SELECT query string.
- owlapy.marked_entity_generator_converter.generator
- owlapy.marked_entity_generator_converter.owl_expression_to_class_query(context: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], root_variable_pos: str = '?pos', root_variable_neg: str = '?neg', for_all_de_morgan: bool = True, named_individuals: bool = False, filter_expression: owlapy.class_expression.OWLClassExpression | None = None, validate: bool = False) str[source]
Convert an OWL class expression with a
CONTEXT_POSITION_MARKERinto a SPARQL query that discovers OWL classes and counts how many positive / negative examples each class covers within the given context.This is the Python equivalent of the Java
Suggestor.generateClassQuery.- Parameters:
context – Class expression containing
CONTEXT_POSITION_MARKER.positive_examples – Positive example individuals.
negative_examples – Negative example individuals.
root_variable_pos – SPARQL variable for positives (default
?pos).root_variable_neg – SPARQL variable for negatives (default
?neg).for_all_de_morgan – Use De Morgan rewriting for universal quantifiers.
named_individuals – Restrict to
owl:NamedIndividualinstances.filter_expression – Optional filter CE (wrapped in FILTER NOT EXISTS).
validate – If
True, validates the generated SPARQL query usingrdflib.parseQuery(slower but safer).
- Returns:
A valid SPARQL SELECT query string.
- owlapy.marked_entity_generator_converter.owl_expression_to_property_query(context: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], root_variable_pos: str = '?pos', root_variable_neg: str = '?neg', for_all_de_morgan: bool = True, named_individuals: bool = False, inverted: bool = False, filter_expression: owlapy.class_expression.OWLClassExpression | None = None, validate: bool = False) str[source]
Convert an OWL class expression with a
CONTEXT_POSITION_MARKERinto a SPARQL query that discovers OWL properties and counts how many positive / negative examples each property covers within the given context.This is the Python equivalent of the Java
Suggestor.generatePropertyQuery.- Parameters:
context – Class expression containing
CONTEXT_POSITION_MARKER.positive_examples – Positive example individuals.
negative_examples – Negative example individuals.
root_variable_pos – SPARQL variable for positives (default
?pos).root_variable_neg – SPARQL variable for negatives (default
?neg).for_all_de_morgan – Use De Morgan rewriting for universal quantifiers.
named_individuals – Restrict to
owl:NamedIndividualinstances.inverted – If
True, emits[] ?prop ?var .at the marker.filter_expression – Optional filter CE (wrapped in FILTER NOT EXISTS).
validate – If
True, validates the generated SPARQL query usingrdflib.parseQuery(slower but safer).
- Returns:
A valid SPARQL SELECT query string.
- owlapy.marked_entity_generator_converter.owl_expression_to_negated_class_query(context: owlapy.class_expression.OWLClassExpression, positive_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], negative_examples: Iterable[owlapy.owl_individual.OWLNamedIndividual], root_variable_pos: str = '?pos', root_variable_neg: str = '?neg', for_all_de_morgan: bool = True, named_individuals: bool = False, filter_expression: owlapy.class_expression.OWLClassExpression | None = None, validate: bool = False) str[source]
Convert an OWL class expression with a
CONTEXT_POSITION_MARKERinto a SPARQL query that discovers OWL classes that the positive examples are not members of, together with positive / negative hit counts.This is the Python equivalent of the Java
Suggestor.generateNegatedClassQuery.At the marker position the converter emits:
?class a owl:Class . FILTER NOT EXISTS { ?var a ?class . }instead of
?var a ?class ..- Parameters:
context – Class expression containing
CONTEXT_POSITION_MARKER.positive_examples – Positive example individuals.
negative_examples – Negative example individuals.
root_variable_pos – SPARQL variable for positives (default
?pos).root_variable_neg – SPARQL variable for negatives (default
?neg).for_all_de_morgan – Use De Morgan rewriting for universal quantifiers.
named_individuals – Restrict to
owl:NamedIndividualinstances.filter_expression – Optional filter CE (wrapped in FILTER NOT EXISTS).
validate – If
True, validates the generated SPARQL query usingrdflib.parseQuery(slower but safer).
- Returns:
A valid SPARQL SELECT query string.