Skip to content

Commit f5645f8

Browse files
committed
brought back code removed by mistake
1 parent 7238c91 commit f5645f8

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

owlapy/owl_ontology.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
OWLDisjointDataPropertiesAxiom, OWLDisjointObjectPropertiesAxiom, OWLEquivalentDataPropertiesAxiom, \
4040
OWLEquivalentObjectPropertiesAxiom, OWLInverseObjectPropertiesAxiom, OWLNaryPropertyAxiom, OWLNaryIndividualAxiom, \
4141
OWLDifferentIndividualsAxiom, OWLDisjointClassesAxiom, OWLSameIndividualAxiom, OWLClassAxiom, \
42-
OWLDataPropertyDomainAxiom, OWLDataPropertyRangeAxiom, OWLObjectPropertyDomainAxiom
42+
OWLDataPropertyDomainAxiom, OWLDataPropertyRangeAxiom, OWLObjectPropertyDomainAxiom, OWLSubPropertyChainAxiom
4343
from owlapy.static_funcs import startJVM
4444
from owlapy.vocab import OWLFacet
4545
import os
@@ -510,6 +510,27 @@ def _(axiom: OWLObjectPropertyCharacteristicAxiom, ontology: AbstractOWLOntology
510510
raise ValueError(f'ObjectPropertyCharacteristicAxiom ({axiom}) is not defined.')
511511

512512

513+
@_add_axiom.register
514+
def _(axiom: OWLSubPropertyChainAxiom, ontology: AbstractOWLOntology, world: owlready2.namespace.World):
515+
conv = ToOwlready2(world)
516+
ont_x: owlready2.Ontology = conv.map_object(ontology)
517+
518+
super_property = axiom.get_super_property()
519+
property_chain = list(axiom.get_property_chain())
520+
521+
# Ensure all properties in the chain and the super property are declared
522+
_add_axiom(OWLDeclarationAxiom(super_property), ontology, world)
523+
for prop in property_chain:
524+
if isinstance(prop, OWLObjectInverseOf):
525+
_add_axiom(OWLDeclarationAxiom(prop.get_named_property()), ontology, world)
526+
else:
527+
_add_axiom(OWLDeclarationAxiom(prop), ontology, world)
528+
529+
with ont_x:
530+
super_property_x = conv._to_owlready2_property(super_property)
531+
property_chain_x = [conv._to_owlready2_property(prop) for prop in property_chain]
532+
super_property_x.property_chain.append(owlready2.PropertyChain(property_chain_x))
533+
513534
@_add_axiom.register
514535
def _(axiom: OWLDataPropertyCharacteristicAxiom, ontology: AbstractOWLOntology, world: owlready2.namespace.World):
515536
conv = ToOwlready2(world)
@@ -820,6 +841,28 @@ def _(axiom: OWLDataPropertyCharacteristicAxiom, ontology: AbstractOWLOntology,
820841
and owlready2.FunctionalProperty in property_x.is_a:
821842
property_x.is_a.remove(owlready2.FunctionalProperty)
822843

844+
@_remove_axiom.register
845+
def _(axiom: OWLSubPropertyChainAxiom, ontology: AbstractOWLOntology, world: owlready2.namespace.World):
846+
conv = ToOwlready2(world)
847+
ont_x: owlready2.Ontology = conv.map_object(ontology)
848+
849+
super_property = axiom.get_super_property()
850+
property_chain = list(axiom.get_property_chain())
851+
852+
with ont_x:
853+
super_property_x = conv._to_owlready2_property(super_property)
854+
if super_property_x is None:
855+
return
856+
857+
property_chain_x = [conv._to_owlready2_property(prop) for prop in property_chain]
858+
if not all(property_chain_x):
859+
return
860+
861+
# Find and remove the matching property chain
862+
for pc in super_property_x.property_chain:
863+
if list(pc.properties) == property_chain_x:
864+
super_property_x.property_chain.remove(pc)
865+
break
823866

824867
class Ontology(AbstractOWLOntology):
825868
__slots__ = '_iri', '_world', '_onto', 'is_modified'
@@ -2137,6 +2180,36 @@ def predict(self, h: List[str] = None, r: List[str] = None, t: List[str] = None)
21372180
else:
21382181
topk = len(self.model.entity_to_idx)
21392182

2183+
# Filter out unknown relations; return empty list (0 scores) if none remain
2184+
if r is not None:
2185+
if isinstance(r, str):
2186+
if r not in self.model.relation_to_idx:
2187+
return []
2188+
else:
2189+
r = [ri for ri in r if ri in self.model.relation_to_idx]
2190+
if not r:
2191+
return []
2192+
2193+
# Filter out unknown head entities; return empty list (0 scores) if none remain
2194+
if h is not None:
2195+
if isinstance(h, str):
2196+
if h not in self.model.entity_to_idx:
2197+
return []
2198+
else:
2199+
h = [hi for hi in h if hi in self.model.entity_to_idx]
2200+
if not h:
2201+
return []
2202+
2203+
# Filter out unknown tail entities; return empty list (0 scores) if none remain
2204+
if t is not None:
2205+
if isinstance(t, str):
2206+
if t not in self.model.entity_to_idx:
2207+
return []
2208+
else:
2209+
t = [ti for ti in t if ti in self.model.entity_to_idx]
2210+
if not t:
2211+
return []
2212+
21402213
return [(top_entity, score) for row in
21412214
self.model.predict_topk(h=h, r=r, t=t, topk=topk, batch_size=self.batch_size) for top_entity, score in
21422215
row if score >= self.gamma and is_valid_entity(top_entity)]

0 commit comments

Comments
 (0)