From 5fcd3685fb9315a8bc49dfbc73557881422ef16b Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 11 Feb 2025 13:53:01 +0100 Subject: [PATCH 1/3] Added license terms --- ontolearn/incomplete_kb.py | 23 ++++++++++++++++++ ontolearn/learners/celoe.py | 24 +++++++++++++++++++ ontolearn/learners/ocel.py | 24 +++++++++++++++++++ ontolearn/owl_neural_reasoner.py | 23 ++++++++++++++++++ ontolearn/scripts/litserve_neural_reasoner.py | 23 ++++++++++++++++++ ontolearn/scripts/run.py | 4 ---- 6 files changed, 117 insertions(+), 4 deletions(-) diff --git a/ontolearn/incomplete_kb.py b/ontolearn/incomplete_kb.py index 4a4574f0..826f98fa 100644 --- a/ontolearn/incomplete_kb.py +++ b/ontolearn/incomplete_kb.py @@ -1,3 +1,26 @@ +# ----------------------------------------------------------------------------- +# MIT License +# +# Copyright (c) 2024 Ontolearn Team +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# ----------------------------------------------------------------------------- from owlready2 import * import random from typing import Set diff --git a/ontolearn/learners/celoe.py b/ontolearn/learners/celoe.py index 33baa919..d988176b 100644 --- a/ontolearn/learners/celoe.py +++ b/ontolearn/learners/celoe.py @@ -1,3 +1,27 @@ +# ----------------------------------------------------------------------------- +# MIT License +# +# Copyright (c) 2024 Ontolearn Team +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# ----------------------------------------------------------------------------- + from ..base_concept_learner import RefinementBasedConceptLearner from ..abstracts import AbstractScorer, BaseRefinement, AbstractHeuristic, EncodedPosNegLPStandardKind, \ diff --git a/ontolearn/learners/ocel.py b/ontolearn/learners/ocel.py index a72f5e93..8cdc6610 100644 --- a/ontolearn/learners/ocel.py +++ b/ontolearn/learners/ocel.py @@ -1,3 +1,27 @@ +# ----------------------------------------------------------------------------- +# MIT License +# +# Copyright (c) 2024 Ontolearn Team +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# ----------------------------------------------------------------------------- + from .celoe import CELOE from typing import Optional import owlapy diff --git a/ontolearn/owl_neural_reasoner.py b/ontolearn/owl_neural_reasoner.py index 3c724e18..38b0fd07 100644 --- a/ontolearn/owl_neural_reasoner.py +++ b/ontolearn/owl_neural_reasoner.py @@ -1,3 +1,26 @@ +# ----------------------------------------------------------------------------- +# MIT License +# +# Copyright (c) 2024 Ontolearn Team +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# ----------------------------------------------------------------------------- from owlapy.owl_property import ( OWLDataProperty, OWLObjectInverseOf, diff --git a/ontolearn/scripts/litserve_neural_reasoner.py b/ontolearn/scripts/litserve_neural_reasoner.py index bbb96884..2f4e81bd 100644 --- a/ontolearn/scripts/litserve_neural_reasoner.py +++ b/ontolearn/scripts/litserve_neural_reasoner.py @@ -1,3 +1,26 @@ +# ----------------------------------------------------------------------------- +# MIT License +# +# Copyright (c) 2024 Ontolearn Team +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# ----------------------------------------------------------------------------- import argparse import litserve as ls from ontolearn.owl_neural_reasoner import TripleStoreNeuralReasoner diff --git a/ontolearn/scripts/run.py b/ontolearn/scripts/run.py index d2fa6fe5..9409d36b 100644 --- a/ontolearn/scripts/run.py +++ b/ontolearn/scripts/run.py @@ -1,7 +1,3 @@ -""" - - -""" # ----------------------------------------------------------------------------- # MIT License # From 9b6bf6eb1f24038799c292327e5f4382cee94796 Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 11 Feb 2025 15:01:39 +0100 Subject: [PATCH 2/3] `types` no longer ignores owl:NamedIndividuals --- ontolearn/triple_store.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ontolearn/triple_store.py b/ontolearn/triple_store.py index 199f6513..3d0693c9 100644 --- a/ontolearn/triple_store.py +++ b/ontolearn/triple_store.py @@ -565,11 +565,7 @@ def types(self, ind: OWLNamedIndividual, direct: bool = False) -> Iterable[OWLCl query = "SELECT ?x WHERE {" + f"<{ind.str}> a" + " ?x. }" else: query = rdfs_prefix + "SELECT DISTINCT ?x WHERE {" + f"<{ind.str}> a ?cls. " " ?cls rdfs:subClassOf* ?x}" - yield from [ - i - for i in send_http_request_to_ts_and_fetch_results(self.url, query, OWLClass) - if i != OWLClass(IRI("http://www.w3.org/2002/07/owl#", "NamedIndividual")) - ] + yield from send_http_request_to_ts_and_fetch_results(self.url, query, OWLClass) def get_root_ontology(self) -> AbstractOWLOntology: return self.ontology From df1a5cdbeb6fbc5110e2ca164c487e41c3e30d80 Mon Sep 17 00:00:00 2001 From: Alkid Date: Tue, 11 Feb 2025 15:03:21 +0100 Subject: [PATCH 3/3] Added support for the scenario when owl:Thing is not set explicitly --- ontolearn/learners/drill.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/ontolearn/learners/drill.py b/ontolearn/learners/drill.py index d864efe1..3c2e9ebc 100644 --- a/ontolearn/learners/drill.py +++ b/ontolearn/learners/drill.py @@ -25,9 +25,11 @@ import pandas as pd import json -from owlapy.class_expression import OWLClassExpression +from owlapy.class_expression import OWLClassExpression, OWLThing, OWLClass +from owlapy.iri import IRI from owlapy.owl_individual import OWLNamedIndividual from owlapy import owl_expression_to_dl + from ontolearn.base_concept_learner import RefinementBasedConceptLearner from ontolearn.refinement_operators import LengthBasedRefinement from ontolearn.abstracts import AbstractNode, AbstractKnowledgeBase @@ -51,8 +53,8 @@ from tqdm import tqdm from owlapy.converter import owl_expression_to_sparql_with_confusion_matrix -from ..triple_store import TripleStore -from ..utils.static_funcs import make_iterable_verbose +from ontolearn.triple_store import TripleStore +from ontolearn.utils.static_funcs import make_iterable_verbose from owlapy.utils import get_expression_length @@ -438,17 +440,23 @@ def compute_quality_of_class_expression(self, state: RL_State) -> None: # (3) Increment the number of tested concepts attribute. """ - if isinstance(self.kb,TripleStore): - sparql_query=owl_expression_to_sparql_with_confusion_matrix(expression=state.concept, - positive_examples=self.pos, - negative_examples=self.neg) - bindings=self.kb.query(sparql_query).json()["results"]["bindings"] + if isinstance(self.kb, TripleStore): + c = state.concept + if c is OWLThing: + tp = list(self.kb.reasoner.types(list(self.pos)[0], True)) # get types of a lp example + if OWLThing not in tp: # if owl:Thing not explicitly specified check for owl:NamedIndividual + named_individual = OWLClass(IRI('http://www.w3.org/2002/07/owl#', 'NamedIndividual')) + if named_individual in tp: + c = named_individual + + sparql_query = owl_expression_to_sparql_with_confusion_matrix(expression=c, positive_examples=self.pos, + negative_examples=self.neg) + bindings = self.kb.query(sparql_query).json()["results"]["bindings"] assert len(bindings) == 1 - bindings=bindings.pop() - confusion_matrix={k : v["value"]for k,v in bindings.items()} + bindings = bindings.pop() + confusion_matrix = {k: v["value"]for k, v in bindings.items()} quality = self.quality_func(confusion_matrix=confusion_matrix) - else: individuals = frozenset([i for i in self.kb.individuals(state.concept)]) quality = self.quality_func(individuals=individuals, pos=self.pos, neg=self.neg)