Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drill bug fix #513

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ontolearn/incomplete_kb.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 24 additions & 0 deletions ontolearn/learners/celoe.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand Down
30 changes: 19 additions & 11 deletions ontolearn/learners/drill.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions ontolearn/learners/ocel.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 23 additions & 0 deletions ontolearn/owl_neural_reasoner.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
23 changes: 23 additions & 0 deletions ontolearn/scripts/litserve_neural_reasoner.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions ontolearn/scripts/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""


"""
# -----------------------------------------------------------------------------
# MIT License
#
Expand Down
6 changes: 1 addition & 5 deletions ontolearn/triple_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down