From d9ba9610a5186c96ee1dc2969081f5ff67cf6786 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Mon, 5 Aug 2024 12:00:14 +0200 Subject: [PATCH 1/9] WIP: first commit of owl_to_sentence_via_triple_store() --- owlapy/iri.py | 2 +- owlapy/render.py | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/owlapy/iri.py b/owlapy/iri.py index 6fa36a0..c723441 100644 --- a/owlapy/iri.py +++ b/owlapy/iri.py @@ -43,7 +43,7 @@ def __init__(self, namespace: Union[str, Namespaces], remainder: str): if isinstance(namespace, Namespaces): namespace = namespace.ns else: - assert namespace[-1] in ("/", ":", "#") + assert namespace[-1] in ("/", ":", "#"), "It should be a valid IRI based on /, :, and #" import sys self._namespace = sys.intern(namespace) self._remainder = remainder diff --git a/owlapy/render.py b/owlapy/render.py index e756445..b1502b0 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -21,7 +21,6 @@ from .class_expression import OWLObjectHasValue, OWLFacetRestriction, OWLDatatypeRestriction, OWLObjectOneOf from .owl_datatype import OWLDatatype - _DL_SYNTAX = types.SimpleNamespace( SUBCLASS="⊑", EQUIVALENT_TO="≡", @@ -57,6 +56,40 @@ def _simple_short_form_provider(e: OWLEntity) -> str: return sf +from typing import Dict + + +def owl_to_sentence_via_triple_store(e: OWLEntity, triplestore, rules: Dict[str:str] = None) -> str: + """ + e: + rules: A mapping from OWLEntity to predicates, + + Keys in rules can be OWLNamedIndividual, OWLClass, e.g., + IRI to IRI s.t. the second IRI must be a predicate leading to literal + + + triplestore: or KnowledgeBase + + """ + iri: IRI = e.iri + sf = iri.reminder + + if rules is None: + # TODO: Generate a SPARQL query! + # Endpoint: https://dbpedia.org/sparql select ?o where { + # ?o} + sparql = f"""select ?o where {{ <{e.str}> ?o}}""" + # TODO:CD: Extract results from binding (Note: there could be multiple results) + # TODO:CD: Select only one or concat the results + else: + str_type = str(type(e)) + if selected_predicate := rules.get(str_type, None): + sparql = f"""select ?o where {{ <{e.str}> <{selected_predicate}> ?o}}""" + else: + pass + return triplestore.query(sparql) + + class DLSyntaxObjectRenderer(OWLObjectRenderer): """DL Syntax renderer for OWL Objects.""" __slots__ = '_sfp' @@ -226,7 +259,7 @@ def _render_operands(self, c: OWLNaryBooleanClassExpression) -> List[str]: def _render_nested(self, c: OWLClassExpression) -> str: if isinstance(c, OWLBooleanClassExpression) or isinstance(c, OWLRestriction) \ - or isinstance(c, OWLNaryDataRange): + or isinstance(c, OWLNaryDataRange): return "(%s)" % self.render(c) else: return self.render(c) @@ -420,7 +453,7 @@ def _render_operands(self, c: OWLNaryBooleanClassExpression) -> List[str]: def _render_nested(self, c: OWLClassExpression) -> str: if isinstance(c, OWLBooleanClassExpression) or isinstance(c, OWLRestriction) \ - or isinstance(c, OWLNaryDataRange): + or isinstance(c, OWLNaryDataRange): return "(%s)" % self.render(c) else: return self.render(c) From 376fb684c09844e6f56ae604fcd08cb2709f54a2 Mon Sep 17 00:00:00 2001 From: Alkid Date: Mon, 5 Aug 2024 15:44:28 +0200 Subject: [PATCH 2/9] added translating short form renderer --- owlapy/render.py | 71 +++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/owlapy/render.py b/owlapy/render.py index b1502b0..30e6144 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -7,10 +7,10 @@ from owlapy import namespaces from .iri import IRI -from .owl_individual import OWLNamedIndividual +from .owl_individual import OWLNamedIndividual, OWLIndividual from .owl_literal import OWLLiteral from .owl_object import OWLObjectRenderer, OWLEntity, OWLObject -from .owl_property import OWLObjectInverseOf, OWLPropertyExpression +from .owl_property import OWLObjectInverseOf, OWLPropertyExpression, OWLDataProperty, OWLObjectProperty from .class_expression import OWLClassExpression, OWLBooleanClassExpression, OWLClass, OWLObjectSomeValuesFrom, \ OWLObjectAllValuesFrom, OWLObjectUnionOf, OWLObjectIntersectionOf, OWLObjectComplementOf, OWLObjectMinCardinality, \ OWLObjectExactCardinality, OWLObjectMaxCardinality, OWLObjectHasSelf, OWLDataSomeValuesFrom, OWLDataAllValuesFrom, \ @@ -20,6 +20,7 @@ from .owl_data_ranges import OWLNaryDataRange, OWLDataComplementOf, OWLDataUnionOf, OWLDataIntersectionOf from .class_expression import OWLObjectHasValue, OWLFacetRestriction, OWLDatatypeRestriction, OWLObjectOneOf from .owl_datatype import OWLDatatype +from .owl_reasoner import OWLReasoner _DL_SYNTAX = types.SimpleNamespace( SUBCLASS="⊑", @@ -56,38 +57,58 @@ def _simple_short_form_provider(e: OWLEntity) -> str: return sf -from typing import Dict +mapper = { + 'OWLNamedIndividual': "http://www.w3.org/2002/07/owl#NamedIndividual", + 'OWLObjectProperty': "http://www.w3.org/2002/07/owl#ObjectProperty", + 'OWLDataProperty': "http://www.w3.org/2002/07/owl#DatatypeProperty", + 'OWLClass': "http://www.w3.org/2002/07/owl#Class" +} -def owl_to_sentence_via_triple_store(e: OWLEntity, triplestore, rules: Dict[str:str] = None) -> str: +def translating_short_form_provider(e: OWLEntity, reasoner, rules: dict[str:str] = None) -> str: """ - e: + e: entity. + reasoner: OWLReasoner or Triplestore(from Ontolearn) rules: A mapping from OWLEntity to predicates, - - Keys in rules can be OWLNamedIndividual, OWLClass, e.g., - IRI to IRI s.t. the second IRI must be a predicate leading to literal - - - triplestore: or KnowledgeBase - + Keys in rules can be general or specific iris, e.g., + IRI to IRI s.t. the second IRI must be a predicate leading to literal """ - iri: IRI = e.iri - sf = iri.reminder + label_iri = "http://www.w3.org/2000/01/rdf-schema#label" + + def get_label(entity, r, predicate=label_iri): + if isinstance(r, OWLReasoner): + values = list(r.data_property_values(OWLNamedIndividual(e.iri), OWLDataProperty(label_iri))) + if values: + return str(values[0].get_literal()) + else: + return _simple_short_form_provider(entity) + else: + # else we have a TripleStore + sparql = f"""select ?o where {{ <{entity.str}> <{predicate}> ?o}}""" + if results := list(r.query(sparql)): + return str(results[0]) + else: + return _simple_short_form_provider(entity) if rules is None: - # TODO: Generate a SPARQL query! - # Endpoint: https://dbpedia.org/sparql select ?o where { - # ?o} - sparql = f"""select ?o where {{ <{e.str}> ?o}}""" - # TODO:CD: Extract results from binding (Note: there could be multiple results) - # TODO:CD: Select only one or concat the results + return get_label(e, reasoner) else: - str_type = str(type(e)) - if selected_predicate := rules.get(str_type, None): - sparql = f"""select ?o where {{ <{e.str}> <{selected_predicate}> ?o}}""" + # Check if a predicate is set for a specific IRI: + # (e.g "http://www.example.org/SomeSpecificClass":"http://www.example.org/SomePredicate") + # WARNING: This will only replace the specified class not individuals belonging to this class. + # This is to avoid confusion, because entity can also be a property and properties does not classify individual. + # So to avoid confusion, the specified predicate in the rules will only be used to 'label' the specified entity + # iri. + if specific_predicate := rules.get(e.str, None): + return get_label(e, reasoner, specific_predicate) + # Check if a predicate is set for a general IRI: + # (e.g "http://www.w3.org/2002/07/owl#NamedIndividual":"http://www.example.org/SomePredicate") + # then it will label any entity of that type using the given predicate. + elif general_predicate := rules.get(mapper[str(type(e))], None): + return get_label(e, reasoner, general_predicate) + # No specific rule set, use http://www.w3.org/2000/01/rdf-schema#label (by default) else: - pass - return triplestore.query(sparql) + return get_label(e, reasoner) class DLSyntaxObjectRenderer(OWLObjectRenderer): From 9ddf2f555c20fe726de5c4958bff505c6d15d1bf Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Mon, 5 Aug 2024 21:47:03 +0200 Subject: [PATCH 3/9] The first input ofr reasoner.data_proerty_values() should not be necessearly an OWL Individuall --- owlapy/render.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owlapy/render.py b/owlapy/render.py index 30e6144..02379f6 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -77,7 +77,7 @@ def translating_short_form_provider(e: OWLEntity, reasoner, rules: dict[str:str] def get_label(entity, r, predicate=label_iri): if isinstance(r, OWLReasoner): - values = list(r.data_property_values(OWLNamedIndividual(e.iri), OWLDataProperty(label_iri))) + values = list(r.data_property_values(e.iri, OWLDataProperty(label_iri))) if values: return str(values[0].get_literal()) else: From f76db067025f135645777d7d759170e4c6728903 Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 6 Aug 2024 13:37:20 +0200 Subject: [PATCH 4/9] data_property_values accepts OWLEntity --- owlapy/owl_reasoner.py | 25 ++++++++++++++----------- owlapy/owlapi_adaptor.py | 9 +++++---- owlapy/render.py | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/owlapy/owl_reasoner.py b/owlapy/owl_reasoner.py index 013a96c..04b90a9 100644 --- a/owlapy/owl_reasoner.py +++ b/owlapy/owl_reasoner.py @@ -21,6 +21,7 @@ from owlapy.owl_axiom import OWLAxiom, OWLSubClassOfAxiom from owlapy.owl_data_ranges import OWLDataRange, OWLDataComplementOf, OWLDataUnionOf, OWLDataIntersectionOf from owlapy.owl_datatype import OWLDatatype +from owlapy.owl_object import OWLEntity from owlapy.owl_ontology import OWLOntology, Ontology, _parse_concept_to_owlapy, ToOwlready2 from owlapy.owl_ontology_manager import OntologyManager from owlapy.owl_property import OWLObjectPropertyExpression, OWLDataProperty, OWLObjectProperty, OWLObjectInverseOf, \ @@ -187,16 +188,19 @@ def equivalent_data_properties(self, dp: OWLDataProperty) -> Iterable[OWLDataPro pass @abstractmethod - def data_property_values(self, ind: OWLNamedIndividual, pe: OWLDataProperty, direct: bool = True) \ + def data_property_values(self, e: OWLEntity, pe: OWLDataProperty, direct: bool = True) \ -> Iterable['OWLLiteral']: - """Gets the data property values for the specified individual and data property expression. + """Gets the data property values for the specified entity and data property expression. Args: - ind: The individual that is the subject of the data property values. - pe: The data property expression whose values are to be retrieved for the specified individual. + e: The owl entity (usually an individual) that is the subject of the data property values. + pe: The data property expression whose values are to be retrieved for the specified entity. direct: Specifies if the direct values should be retrieved (True), or if all values should be retrieved (False), so that sub properties are taken into account. + Note: Can be used to get values, for example, of 'label' property of owl entities such as classes and properties + too (not only individuals). + Returns: A set of OWLLiterals containing literals such that for each literal l in the set, the set of reasoner axioms entails DataPropertyAssertion(pe ind l). @@ -610,9 +614,9 @@ def same_individuals(self, ind: OWLNamedIndividual) -> Iterable[OWLNamedIndividu yield from (OWLNamedIndividual(IRI.create(d_i.iri)) for d_i in i.equivalent_to if isinstance(d_i, owlready2.Thing)) - def data_property_values(self, ind: OWLNamedIndividual, pe: OWLDataProperty, direct: bool = True) \ + def data_property_values(self, e: OWLEntity, pe: OWLDataProperty, direct: bool = True) \ -> Iterable[OWLLiteral]: - i: owlready2.Thing = self._world[ind.str] + i: owlready2.Thing = self._world[e.str] p: owlready2.DataPropertyClass = self._world[pe.str] retrieval_func = p._get_values_for_individual if direct else p._get_indirect_values_for_individual for val in retrieval_func(i): @@ -1140,9 +1144,9 @@ def different_individuals(self, ce: OWLNamedIndividual) -> Iterable[OWLNamedIndi def same_individuals(self, ce: OWLNamedIndividual) -> Iterable[OWLNamedIndividual]: yield from self._base_reasoner.same_individuals(ce) - def data_property_values(self, ind: OWLNamedIndividual, pe: OWLDataProperty, direct: bool = True) \ + def data_property_values(self, e: OWLEntity, pe: OWLDataProperty, direct: bool = True) \ -> Iterable[OWLLiteral]: - yield from self._base_reasoner.data_property_values(ind, pe, direct) + yield from self._base_reasoner.data_property_values(e, pe, direct) def all_data_property_values(self, pe: OWLDataProperty, direct: bool = True) -> Iterable[OWLLiteral]: yield from self._base_reasoner.all_data_property_values(pe, direct) @@ -1645,9 +1649,8 @@ def different_individuals(self, ind: OWLNamedIndividual) -> Iterable[OWLNamedInd def same_individuals(self, ind: OWLNamedIndividual) -> Iterable[OWLNamedIndividual]: yield from self.adaptor.same_individuals(ind) - def data_property_values(self, ind: OWLNamedIndividual, pe: OWLDataProperty, direct: bool = True) -> Iterable[ - OWLLiteral]: - yield from self.adaptor.data_property_values(ind, pe) + def data_property_values(self, e: OWLEntity, pe: OWLDataProperty, direct: bool = True) -> Iterable[OWLLiteral]: + yield from self.adaptor.data_property_values(e, pe) def object_property_values(self, ind: OWLNamedIndividual, pe: OWLObjectPropertyExpression, direct: bool = False) -> \ Iterable[OWLNamedIndividual]: diff --git a/owlapy/owlapi_adaptor.py b/owlapy/owlapi_adaptor.py index 1f24ea3..a6cddb0 100644 --- a/owlapy/owlapi_adaptor.py +++ b/owlapy/owlapi_adaptor.py @@ -8,6 +8,7 @@ from owlapy.class_expression import OWLClassExpression from owlapy.owl_individual import OWLNamedIndividual +from owlapy.owl_object import OWLEntity from owlapy.owl_property import OWLDataProperty, OWLObjectProperty from typing import List @@ -389,11 +390,11 @@ def object_property_values(self, i: OWLNamedIndividual, p: OWLObjectProperty): yield from [self.mapper.map_(ind) for ind in self.reasoner.getObjectPropertyValues(self.mapper.map_(i), self.mapper.map_(p)).getFlattened()] - def data_property_values(self, i: OWLNamedIndividual, p: OWLDataProperty): - """Gets the data property values for the specified individual and data property expression. + def data_property_values(self, e: OWLEntity, p: OWLDataProperty): + """Gets the data property values for the specified entity and data property expression. Args: - i: The individual that is the subject of the data property values. + e: The entity (usually an individual) that is the subject of the data property values. p: The data property expression whose values are to be retrieved for the specified individual. Returns: @@ -401,7 +402,7 @@ def data_property_values(self, i: OWLNamedIndividual, p: OWLDataProperty): axioms entails DataPropertyAssertion(pe ind l). """ yield from [self.mapper.map_(literal) for literal in - to_list(self.reasoner.dataPropertyValues(self.mapper.map_(i), self.mapper.map_(p)))] + to_list(self.reasoner.dataPropertyValues(self.mapper.map_(e), self.mapper.map_(p)))] def disjoint_object_properties(self, p: OWLObjectProperty): """Gets the simplified object properties that are disjoint with the specified object property with respect diff --git a/owlapy/render.py b/owlapy/render.py index 02379f6..6bf41df 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -77,7 +77,7 @@ def translating_short_form_provider(e: OWLEntity, reasoner, rules: dict[str:str] def get_label(entity, r, predicate=label_iri): if isinstance(r, OWLReasoner): - values = list(r.data_property_values(e.iri, OWLDataProperty(label_iri))) + values = list(r.data_property_values(entity, OWLDataProperty(label_iri))) if values: return str(values[0].get_literal()) else: From a4d47798d6f96fd0fe6fe3de036c9306a6db28bb Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 6 Aug 2024 13:50:13 +0200 Subject: [PATCH 5/9] updated comments --- owlapy/render.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/owlapy/render.py b/owlapy/render.py index 6bf41df..e32f4a6 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -93,17 +93,17 @@ def get_label(entity, r, predicate=label_iri): if rules is None: return get_label(e, reasoner) else: - # Check if a predicate is set for a specific IRI: + # Check if a rule exist for a specific IRI: # (e.g "http://www.example.org/SomeSpecificClass":"http://www.example.org/SomePredicate") - # WARNING: This will only replace the specified class not individuals belonging to this class. - # This is to avoid confusion, because entity can also be a property and properties does not classify individual. - # So to avoid confusion, the specified predicate in the rules will only be used to 'label' the specified entity - # iri. + # WARNING: If the entity is an OWLClass, the rule specified for that class will only be used to replace the + # class itself not individuals belonging to that class. The reason for that is that the entity can also be a + # property and properties does not classify individuals. So to avoid confusion, the specified predicate in the + # rules will only be used to 'label' the specified entity. if specific_predicate := rules.get(e.str, None): return get_label(e, reasoner, specific_predicate) - # Check if a predicate is set for a general IRI: + # Check if a rule exist for a general IRI: # (e.g "http://www.w3.org/2002/07/owl#NamedIndividual":"http://www.example.org/SomePredicate") - # then it will label any entity of that type using the given predicate. + # then it will label any entity of that type using the value retrieved from the given predicate. elif general_predicate := rules.get(mapper[str(type(e))], None): return get_label(e, reasoner, general_predicate) # No specific rule set, use http://www.w3.org/2000/01/rdf-schema#label (by default) From b3d92c4ea4d9ef225dfd8d1c9005e8abbdc0b9bf Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Tue, 6 Aug 2024 14:10:45 +0200 Subject: [PATCH 6/9] Initivial version of owl to sentence is implemented. --- owlapy/render.py | 70 +++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 48 deletions(-) diff --git a/owlapy/render.py b/owlapy/render.py index 02379f6..80cb56f 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -21,7 +21,9 @@ from .class_expression import OWLObjectHasValue, OWLFacetRestriction, OWLDatatypeRestriction, OWLObjectOneOf from .owl_datatype import OWLDatatype from .owl_reasoner import OWLReasoner - +from typing import Union, Tuple +import requests +import warnings _DL_SYNTAX = types.SimpleNamespace( SUBCLASS="⊑", EQUIVALENT_TO="≡", @@ -57,58 +59,30 @@ def _simple_short_form_provider(e: OWLEntity) -> str: return sf -mapper = { - 'OWLNamedIndividual': "http://www.w3.org/2002/07/owl#NamedIndividual", - 'OWLObjectProperty': "http://www.w3.org/2002/07/owl#ObjectProperty", - 'OWLDataProperty': "http://www.w3.org/2002/07/owl#DatatypeProperty", - 'OWLClass': "http://www.w3.org/2002/07/owl#Class" -} - - -def translating_short_form_provider(e: OWLEntity, reasoner, rules: dict[str:str] = None) -> str: +def translating_short_form_provider(e: OWLEntity, reasoner: Union[Tuple[OWLReasoner, str]], + rules: dict[str:str] = None) -> str: """ e: entity. - reasoner: OWLReasoner or Triplestore(from Ontolearn) - rules: A mapping from OWLEntity to predicates, - Keys in rules can be general or specific iris, e.g., - IRI to IRI s.t. the second IRI must be a predicate leading to literal + reasoner: OWLReasoner or a string representing an endpoint of a triple store + rules: A mapping from python classes to string IRI leading to a literal """ - label_iri = "http://www.w3.org/2000/01/rdf-schema#label" - - def get_label(entity, r, predicate=label_iri): - if isinstance(r, OWLReasoner): - values = list(r.data_property_values(e.iri, OWLDataProperty(label_iri))) - if values: - return str(values[0].get_literal()) + # () Iterate over rules + for owlapy_class, str_predicate in rules.items(): + # () Check whether an OWL entity is an instance of specified class + if isinstance(e, owlapy_class): + sparql = f"""select ?o where {{ <{e.str}> <{str_predicate}> ?o}}""" + response = requests.post(url=reasoner, data={"query": sparql}) + results = response.json()["results"]["bindings"] + if len(results) > 0: + return results[0]["o"]["value"] else: - return _simple_short_form_provider(entity) - else: - # else we have a TripleStore - sparql = f"""select ?o where {{ <{entity.str}> <{predicate}> ?o}}""" - if results := list(r.query(sparql)): - return str(results[0]) - else: - return _simple_short_form_provider(entity) + print(sparql) + print(f"No literal found\n{sparql}\n{e}") + continue - if rules is None: - return get_label(e, reasoner) - else: - # Check if a predicate is set for a specific IRI: - # (e.g "http://www.example.org/SomeSpecificClass":"http://www.example.org/SomePredicate") - # WARNING: This will only replace the specified class not individuals belonging to this class. - # This is to avoid confusion, because entity can also be a property and properties does not classify individual. - # So to avoid confusion, the specified predicate in the rules will only be used to 'label' the specified entity - # iri. - if specific_predicate := rules.get(e.str, None): - return get_label(e, reasoner, specific_predicate) - # Check if a predicate is set for a general IRI: - # (e.g "http://www.w3.org/2002/07/owl#NamedIndividual":"http://www.example.org/SomePredicate") - # then it will label any entity of that type using the given predicate. - elif general_predicate := rules.get(mapper[str(type(e))], None): - return get_label(e, reasoner, general_predicate) - # No specific rule set, use http://www.w3.org/2000/01/rdf-schema#label (by default) - else: - return get_label(e, reasoner) + warnings.warn(f"No matching rules for OWL Entity:{e}!") + # No mathing rule found + return _simple_short_form_provider(e) class DLSyntaxObjectRenderer(OWLObjectRenderer): From 7bd8d8befbc4ae38c3f45b46e961bfc7bd329462 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Tue, 6 Aug 2024 14:24:47 +0200 Subject: [PATCH 7/9] Docstring of translating_short_form_endpoint improved --- owlapy/render.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/owlapy/render.py b/owlapy/render.py index 77d6bfa..6439dc9 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -117,9 +117,27 @@ def get_label(entity, r, predicate=label_iri): def translating_short_form_endpoint(e: OWLEntity, endpoint: str, rules: dict[abc.ABCMeta:str] = None) -> str: """ - e: entity. - reasoner: OWLReasoner or a string representing an endpoint of a triple store - rules: A mapping from python classes to string IRI leading to a literal + Translates an OWLEntity to a short form string using provided rules and an endpoint. + + Parameters: + e (OWLEntity): The OWL entity to be translated. + endpoint (str): The endpoint of a triple store to query against. + rules (dict[abc.ABCMeta:str], optional): A dictionary mapping OWL classes to string IRIs leading to a literal. + + Returns: + str: The translated short form of the OWL entity. If no matching rules are found, a simple short form is returned. + + This function iterates over the provided rules to check if the given OWL entity is an instance of any specified class. + If a match is found, it constructs a SPARQL query to retrieve the literal value associated with the entity and predicate. + If a literal is found, it is returned as the short form. If no literals are found, the SPARQL query and entity information + are printed for debugging purposes. If no matching rules are found, a warning is issued and a simple short form is returned. + + + Example: + >>> e = OWLEntity("http://example.org/entity") + >>> endpoint = "http://example.org/sparql" + >>> rules = {SomeOWLClass: "http://example.org/predicate"} + >>> translating_short_form_endpoint(e, endpoint, rules) """ # () Iterate over rules for owlapy_class, str_predicate in rules.items(): From 0a2092035cadb655335760485ad0668ba86842b0 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Tue, 6 Aug 2024 14:27:14 +0200 Subject: [PATCH 8/9] requests added into install_requires --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 10587f1..4003d62 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ }, install_requires=[ "pandas>=1.5.0", + "requests>=2.32.3", "rdflib>=6.0.2", "parsimonious>=0.8.1", "pytest>=8.1.1", From 24a28d71d83d9454838d2848c1a3f4d1407c9bd3 Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 6 Aug 2024 14:31:10 +0200 Subject: [PATCH 9/9] fixed general rules scenario --- owlapy/render.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/owlapy/render.py b/owlapy/render.py index 6439dc9..a8bb5a8 100644 --- a/owlapy/render.py +++ b/owlapy/render.py @@ -80,7 +80,7 @@ def translating_short_form_provider(e: OWLEntity, reasoner, rules: dict[str:str] def get_label(entity, r, predicate=label_iri): if isinstance(r, OWLReasoner): - values = list(r.data_property_values(entity, OWLDataProperty(label_iri))) + values = list(r.data_property_values(entity, OWLDataProperty(predicate))) if values: return str(values[0].get_literal()) else: @@ -107,7 +107,7 @@ def get_label(entity, r, predicate=label_iri): # Check if a rule exist for a general IRI: # (e.g "http://www.w3.org/2002/07/owl#NamedIndividual":"http://www.example.org/SomePredicate") # then it will label any entity of that type using the value retrieved from the given predicate. - elif general_predicate := rules.get(mapper[str(type(e))], None): + elif general_predicate := rules.get(mapper[e.__class__.__name__], None): return get_label(e, reasoner, general_predicate) # No specific rule set, use http://www.w3.org/2000/01/rdf-schema#label (by default) else: