Skip to content

Commit

Permalink
Merge pull request #118 from dice-group/owlapi_mapper_tests
Browse files Browse the repository at this point in the history
Owlapi mapper tests
  • Loading branch information
Demirrr authored Nov 29, 2024
2 parents c4ce7f5 + baadd6b commit 6cfd36a
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 165 deletions.
6 changes: 3 additions & 3 deletions owlapy/class_expression/nary_boolean_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def __repr__(self):

def __eq__(self, other):
if type(other) is type(self):
return {i for i in self._operands} == { j for j in other.operands()}
else:
return False
return (set(self._operands) == set(other.operands())
and len(list(self._operands)) == len(list(other.operands())))
return False

def __hash__(self):
return hash(self._operands)
Expand Down
8 changes: 5 additions & 3 deletions owlapy/class_expression/restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,11 @@ def __hash__(self):

def __eq__(self, other):
if type(other) is type(self):
return self._values == other._values
else:
return False
if isinstance(self._values, OWLIndividual):
return self._values == other._values
else:
return set(self._values) == set(other._values) and len(self._values) == len(other._values)
return False

def __repr__(self):
return f'OWLObjectOneOf({self._values})'
Expand Down
33 changes: 21 additions & 12 deletions owlapy/owl_axiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ def operands(self) -> Iterable[OWLPropertyExpression]:
def __eq__(self, other):
if type(other) is type(self):
return self._class_expression == other._class_expression \
and self._property_expressions == other._property_expressions \
and self._annotations == other._annotations
return NotImplemented
and set(self._property_expressions) == set(other._property_expressions) \
and len(self._property_expressions) == len(other._property_expressions) \
and set(self._annotations) == set(other._annotations)
return False

def __hash__(self):
return hash((self._class_expression, *self._property_expressions, *self._annotations))
Expand Down Expand Up @@ -257,8 +258,11 @@ def as_pairwise_axioms(self) -> Iterable['OWLNaryClassAxiom']:

def __eq__(self, other):
if type(other) is type(self):
return self._class_expressions == other._class_expressions and self._annotations == other._annotations
return NotImplemented
# parsed to set to have order-insensitive comparison
return (set(self._class_expressions) == set(other._class_expressions)
and len(self._class_expressions) == len(other._class_expressions)
and set(self._annotations) == set(other._annotations))
return False

def __hash__(self):
return hash((*self._class_expressions, *self._annotations))
Expand Down Expand Up @@ -339,8 +343,10 @@ def as_pairwise_axioms(self) -> Iterable['OWLNaryIndividualAxiom']:

def __eq__(self, other):
if type(other) is type(self):
return self._individuals == other._individuals and self._annotations == other._annotations
return NotImplemented
return (set(self._individuals) == set(other._individuals)
and len(self._individuals) == len(other._individuals)
and set(self._annotations) == set(other._annotations))
return False

def __hash__(self):
return hash((*self._individuals, *self._annotations))
Expand Down Expand Up @@ -405,8 +411,10 @@ def as_pairwise_axioms(self) -> Iterable['OWLNaryPropertyAxiom']:

def __eq__(self, other):
if type(other) is type(self):
return self._properties == other._properties and self._annotations == other._annotations
return NotImplemented
# parsed to set to have order-insensitive comparison
return (set(self._properties) == set(other._properties) and len(self._properties) == len(other._properties)
and set(self._annotations) == set(other._annotations))
return False

def __hash__(self):
return hash((*self._properties, *self._annotations))
Expand Down Expand Up @@ -587,9 +595,10 @@ def get_owl_disjoint_classes_axiom(self) -> OWLDisjointClassesAxiom:

def __eq__(self, other):
if type(other) is type(self):
return self._cls == other._cls and self._class_expressions == other._class_expressions \
and self._annotations == other._annotations
return NotImplemented
return (self._cls == other._cls and set(self._class_expressions) == set(other._class_expressions)
and self._annotations == other._annotations
and len(self._class_expressions) == len(other._class_expressions))
return False

def __hash__(self):
return hash((self._cls, *self._class_expressions, *self._annotations))
Expand Down
6 changes: 3 additions & 3 deletions owlapy/owl_data_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
DataRange := Datatype | DataIntersectionOf | DataUnionOf | DataComplementOf | DataOneOf | DatatypeRestriction
"""

from .owl_object import OWLObject
from .meta_classes import HasOperands
from typing import Final, Sequence, Iterable
Expand Down Expand Up @@ -42,8 +41,9 @@ def __repr__(self):

def __eq__(self, other):
if type(other) is type(self):
return self._operands == other._operands
return NotImplemented
return (set(self._operands) == set(other._operands)
and len(list((self._operands))) == len(list((other._operands))))
return False

def __hash__(self):
return hash(self._operands)
Expand Down
14 changes: 9 additions & 5 deletions owlapy/owlapi_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
startJVM()
from org.semanticweb.owlapi.model import IRI as owlapi_IRI, OWLOntologyID as owlapi_OWLOntologyID
from org.semanticweb.owlapi.vocab import OWLFacet as owlapi_OWLFacet
from java.util import ArrayList, List, Set, LinkedHashSet, Optional
from java.util import ArrayList, List, Set, LinkedHashSet, Optional, Collections
from java.util.stream import Stream
from uk.ac.manchester.cs.owl.owlapi import (OWLClassImpl, OWLDataAllValuesFromImpl, OWL2DatatypeImpl,
OWLDataExactCardinalityImpl,OWLDataHasValueImpl,
Expand Down Expand Up @@ -329,24 +329,24 @@ def _(self, e):
@map_.register(OWLObjectPropertyDomainAxiom)
@map_.register(OWLDataPropertyDomainAxiom)
@map_.register(OWLAnnotationPropertyDomainAxiom)
@map_.register(OWLAnnotationPropertyRangeAxiom)
def _(self, e):
return init(e)(self.map_(e.get_property()), self.map_(e.get_domain()), self.map_(e.annotations()))

@map_.register(OWLObjectPropertyDomainAxiomImpl)
@map_.register(OWLDataPropertyDomainAxiomImpl)
@map_.register(OWLAnnotationPropertyDomainAxiomImpl)
@map_.register(OWLAnnotationPropertyRangeAxiomImpl)
def _(self, e):
return init(e)(self.map_(e.getProperty()), self.map_(e.getDomain()), self.map_(e.annotationsAsList()))

@map_.register(OWLObjectPropertyRangeAxiom)
@map_.register(OWLDataPropertyRangeAxiom)
@map_.register(OWLAnnotationPropertyRangeAxiom)
def _(self, e):
return init(e)(self.map_(e.get_property()), self.map_(e.get_range()), self.map_(e.annotations()))

@map_.register(OWLObjectPropertyRangeAxiomImpl)
@map_.register(OWLDataPropertyRangeAxiomImpl)
@map_.register(OWLAnnotationPropertyRangeAxiomImpl)
def _(self, e):
return init(e)(self.map_(e.getProperty()), self.map_(e.getRange()), self.map_(e.annotationsAsList()))

Expand Down Expand Up @@ -445,12 +445,12 @@ def _(self, e):
@map_.register(OWLDifferentIndividualsAxiom)
@map_.register(OWLSameIndividualAxiom)
def _(self, e):
return OWLDifferentIndividualsAxiomImpl(self.map_(e.individuals()), self.map_(e.annotations()))
return init(e)(self.map_(e.individuals()), self.map_(e.annotations()))

@map_.register(OWLDifferentIndividualsAxiomImpl)
@map_.register(OWLSameIndividualAxiomImpl)
def _(self, e):
return OWLDifferentIndividualsAxiom(self.map_(e.getIndividualsAsList()), self.map_(e.annotationsAsList()))
return init(e)(self.map_(e.getIndividualsAsList()), self.map_(e.annotationsAsList()))

@map_.register(OWLDisjointUnionAxiom)
def _(self, e):
Expand Down Expand Up @@ -504,6 +504,8 @@ def _(self, e):
if e and len(casted_list) > 0:
for obj in list(e):
python_list.append(self.map_(obj))
# reverse to have the same order as the mapped iterable object
python_list.reverse()
return python_list

@map_.register(list)
Expand All @@ -514,6 +516,8 @@ def _(self, e):
if e is not None and len(e) > 0:
for item in e:
java_list.add(self.map_(item))
# reverse to have the same order as the mapped iterable object
Collections.reverse(java_list)
return java_list

@map_.register(Stream)
Expand Down
99 changes: 94 additions & 5 deletions tests/test_owlapi_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
OWLObjectMinCardinality, OWLObjectMaxCardinality, OWLObjectExactCardinality, OWLDataMinCardinality, \
OWLDataMaxCardinality, OWLDataExactCardinality, OWLObjectHasSelf, OWLObjectHasValue, OWLObjectSomeValuesFrom, \
OWLObjectAllValuesFrom, OWLDataAllValuesFrom, OWLDataHasValue, OWLObjectUnionOf, OWLObjectOneOf, \
OWLFacetRestriction, OWLDatatypeRestriction, OWLDataOneOf
OWLFacetRestriction, OWLDatatypeRestriction, OWLDataOneOf, OWLThing
from owlapy.iri import IRI
from owlapy.owl_axiom import OWLAnnotationProperty, OWLAnnotation, OWLAnnotationAssertionAxiom
from owlapy.owl_axiom import OWLAnnotationProperty, OWLAnnotation, OWLAnnotationAssertionAxiom, OWLDeclarationAxiom, \
OWLClassAssertionAxiom, OWLObjectPropertyAssertionAxiom, OWLDataPropertyAssertionAxiom, \
OWLNegativeDataPropertyAssertionAxiom, OWLNegativeObjectPropertyAssertionAxiom, OWLObjectPropertyDomainAxiom, \
OWLDataPropertyDomainAxiom, OWLAnnotationPropertyDomainAxiom, OWLAnnotationPropertyRangeAxiom, \
OWLObjectPropertyRangeAxiom, OWLDataPropertyRangeAxiom, OWLEquivalentDataPropertiesAxiom, \
OWLEquivalentObjectPropertiesAxiom, OWLEquivalentClassesAxiom, OWLDisjointClassesAxiom, \
OWLDisjointDataPropertiesAxiom, OWLDisjointObjectPropertiesAxiom, OWLHasKeyAxiom, OWLSubClassOfAxiom, \
OWLSubDataPropertyOfAxiom, OWLSubObjectPropertyOfAxiom, OWLSubAnnotationPropertyOfAxiom, \
OWLAsymmetricObjectPropertyAxiom, OWLFunctionalObjectPropertyAxiom, OWLInverseFunctionalObjectPropertyAxiom, \
OWLIrreflexiveObjectPropertyAxiom, OWLReflexiveObjectPropertyAxiom, OWLSymmetricObjectPropertyAxiom, \
OWLTransitiveObjectPropertyAxiom, OWLFunctionalDataPropertyAxiom, OWLDatatypeDefinitionAxiom, \
OWLDifferentIndividualsAxiom, OWLSameIndividualAxiom, OWLDisjointUnionAxiom, OWLInverseObjectPropertiesAxiom
from owlapy.owl_data_ranges import OWLDataIntersectionOf, OWLDataUnionOf, OWLDataComplementOf
from owlapy.owl_datatype import OWLDatatype
from owlapy.owl_individual import OWLNamedIndividual
Expand Down Expand Up @@ -140,20 +151,98 @@ def test_datarange_mapping(self):
self.assertEqual(f, self.mapper.map_(self.mapper.map_(f)))
self.assertEqual(fr, self.mapper.map_(self.mapper.map_(fr)))
self.assertEqual(dtr, self.mapper.map_(self.mapper.map_(dtr)))
self.assertCountEqual(list(dio.operands()), list(self.mapper.map_(self.mapper.map_(dio)).operands()))
self.assertCountEqual(list(doo.operands()), list(self.mapper.map_(self.mapper.map_(doo)).operands()))
self.assertEqual(dio, self.mapper.map_(self.mapper.map_(dio)))
self.assertEqual(doo, self.mapper.map_(self.mapper.map_(doo)))
self.assertCountEqual(list(duo.operands()), list(self.mapper.map_(self.mapper.map_(duo)).operands()))
self.assertEqual(dco, self.mapper.map_(self.mapper.map_(dco)))

def test_axiom_mapping(self):

ap = OWLAnnotationProperty(IRI.create(self.test_ns + "test_annotation"))
new_ap = OWLAnnotationProperty(IRI.create(self.test_ns + "new_ap"))
av = OWLLiteral("Value of annotation")
test_iri = IRI.create(self.test_ns + "test_iri")
a = OWLAnnotation(ap, av)
aa = OWLAnnotationAssertionAxiom(IRI.create(self.test_ns + "test_annotation_subject"), a)

new_class = OWLClass(self.test_ns + "new_class")
new_ind = OWLNamedIndividual(self.test_ns + "new_ind")
new_dp = OWLDataProperty(self.test_ns + "new_dp")
new_op = OWLObjectProperty(self.test_ns + "new_op")
dtr = OWLDatatypeRestriction(DoubleOWLDatatype, OWLFacetRestriction(OWLFacet.MAX_EXCLUSIVE, OWLLiteral(0.1)))
da = OWLDeclarationAxiom(new_class, [a])
caa = OWLClassAssertionAxiom(self.i, new_class, [a])
opaa = OWLObjectPropertyAssertionAxiom(self.i, self.op, new_ind, [a])
dpaa = OWLDataPropertyAssertionAxiom(self.i, self.dp, OWLLiteral(1), [a])
ndpaa = OWLNegativeDataPropertyAssertionAxiom(self.i, self.dp, OWLLiteral(1), [a])
nopaa = OWLNegativeObjectPropertyAssertionAxiom(self.i, self.op, new_ind, [a])
opda = OWLObjectPropertyDomainAxiom(self.op, OWLThing, [a])
opra = OWLObjectPropertyRangeAxiom(self.op, OWLThing, [a])
dpda = OWLDataPropertyDomainAxiom(self.dp, OWLThing, [a])
dpra = OWLDataPropertyRangeAxiom(self.dp, IntegerOWLDatatype, [a])
apda = OWLAnnotationPropertyDomainAxiom(ap, test_iri, [a])
apra = OWLAnnotationPropertyRangeAxiom(ap, test_iri, [a])
edpa = OWLEquivalentDataPropertiesAxiom([self.dp, new_dp], [a])
eopa = OWLEquivalentObjectPropertiesAxiom([self.op, new_op], [a])
dopa = OWLDisjointObjectPropertiesAxiom([self.op, new_op], [a])
ddpa = OWLDisjointDataPropertiesAxiom([self.dp, new_dp], [a])
eca = OWLEquivalentClassesAxiom([self.c, new_class], [a])
dca = OWLDisjointClassesAxiom([self.c, new_class], [a])
hka = OWLHasKeyAxiom(self.c, [self.op, new_op], [a])
sca = OWLSubClassOfAxiom(self.c, new_class, [a])
sdpa = OWLSubDataPropertyOfAxiom(self.dp, new_dp, [a])
sopa = OWLSubObjectPropertyOfAxiom(self.op, new_op, [a])
sapa = OWLSubAnnotationPropertyOfAxiom(ap, new_ap, [a])
aopa = OWLAsymmetricObjectPropertyAxiom(self.op, [a])
fopa = OWLFunctionalObjectPropertyAxiom(self.op, [a])
ifopa = OWLInverseFunctionalObjectPropertyAxiom(self.op, [a])
iopa = OWLIrreflexiveObjectPropertyAxiom(self.op, [a])
ropa = OWLReflexiveObjectPropertyAxiom(self.op, [a])
smopa = OWLSymmetricObjectPropertyAxiom(self.op, [a])
topa = OWLTransitiveObjectPropertyAxiom(self.op, [a])
fdpa = OWLFunctionalDataPropertyAxiom(self.dp, [a])
dda = OWLDatatypeDefinitionAxiom(DoubleOWLDatatype, dtr, [a])
dia = OWLDifferentIndividualsAxiom([self.i, new_ind], [a])
sia = OWLSameIndividualAxiom([self.i, new_ind], [a])
dua = OWLDisjointUnionAxiom(self.c, [new_class], [a])
inopa = OWLInverseObjectPropertiesAxiom(self.op, new_op, [a])

self.assertEqual(ap, self.mapper.map_(self.mapper.map_(ap)))
self.assertEqual(av, self.mapper.map_(self.mapper.map_(av)))
self.assertEqual(a, self.mapper.map_(self.mapper.map_(a)))
self.assertEqual(aa, self.mapper.map_(self.mapper.map_(aa)))
self.assertEqual(da, self.mapper.map_(self.mapper.map_(da)))
self.assertEqual(caa, self.mapper.map_(self.mapper.map_(caa)))
self.assertEqual(opaa, self.mapper.map_(self.mapper.map_(opaa)))
self.assertEqual(dpaa, self.mapper.map_(self.mapper.map_(dpaa)))
self.assertEqual(ndpaa, self.mapper.map_(self.mapper.map_(ndpaa)))
self.assertEqual(nopaa, self.mapper.map_(self.mapper.map_(nopaa)))
self.assertEqual(opda, self.mapper.map_(self.mapper.map_(opda)))
self.assertEqual(opra, self.mapper.map_(self.mapper.map_(opra)))
self.assertEqual(dpda, self.mapper.map_(self.mapper.map_(dpda)))
self.assertEqual(dpra, self.mapper.map_(self.mapper.map_(dpra)))
self.assertEqual(apda, self.mapper.map_(self.mapper.map_(apda)))
self.assertEqual(apra, self.mapper.map_(self.mapper.map_(apra)))
self.assertEqual(edpa, self.mapper.map_(self.mapper.map_(edpa)))
self.assertEqual(eopa, self.mapper.map_(self.mapper.map_(eopa)))
self.assertEqual(dopa, self.mapper.map_(self.mapper.map_(dopa)))
self.assertEqual(ddpa, self.mapper.map_(self.mapper.map_(ddpa)))
self.assertEqual(eca, self.mapper.map_(self.mapper.map_(eca)))
self.assertEqual(dca, self.mapper.map_(self.mapper.map_(dca)))
self.assertEqual(hka, self.mapper.map_(self.mapper.map_(hka)))
self.assertEqual(sca, self.mapper.map_(self.mapper.map_(sca)))
self.assertEqual(sdpa, self.mapper.map_(self.mapper.map_(sdpa)))
self.assertEqual(sopa, self.mapper.map_(self.mapper.map_(sopa)))
self.assertEqual(sapa, self.mapper.map_(self.mapper.map_(sapa)))
self.assertEqual(aopa, self.mapper.map_(self.mapper.map_(aopa)))
self.assertEqual(fopa, self.mapper.map_(self.mapper.map_(fopa)))
self.assertEqual(ifopa, self.mapper.map_(self.mapper.map_(ifopa)))
self.assertEqual(iopa, self.mapper.map_(self.mapper.map_(iopa)))
self.assertEqual(ropa, self.mapper.map_(self.mapper.map_(ropa)))
self.assertEqual(smopa, self.mapper.map_(self.mapper.map_(smopa)))
self.assertEqual(topa, self.mapper.map_(self.mapper.map_(topa)))
self.assertEqual(fdpa, self.mapper.map_(self.mapper.map_(fdpa)))
self.assertEqual(dda, self.mapper.map_(self.mapper.map_(dda)))
self.assertEqual(dia, self.mapper.map_(self.mapper.map_(dia)))
self.assertEqual(sia, self.mapper.map_(self.mapper.map_(sia)))
self.assertEqual(dua, self.mapper.map_(self.mapper.map_(dua)))
self.assertEqual(inopa, self.mapper.map_(self.mapper.map_(inopa)))
4 changes: 2 additions & 2 deletions tests/test_owlapy_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_owlapy_entry_point(self):
onto = SyncOntologyManager().load_ontology("inferred_axioms_ontology.owl")

ops = onto.object_properties_in_signature()
self.assertEqual(list(ops), [OWLObjectProperty(IRI('http://www.benchmark.org/family#', 'hasChild')),
self.assertCountEqual(list(ops), [OWLObjectProperty(IRI('http://www.benchmark.org/family#', 'hasChild')),
OWLObjectProperty(IRI('http://www.benchmark.org/family#', 'hasParent')),
OWLObjectProperty(IRI('http://www.benchmark.org/family#', 'hasSibling')),
OWLObjectProperty(IRI('http://www.benchmark.org/family#', 'married')),
Expand All @@ -49,7 +49,7 @@ def test_owlapy_entry_point(self):
'topObjectProperty')), []))

classes = onto.classes_in_signature()
self.assertEqual(list(classes), [OWLClass(IRI('http://www.benchmark.org/family#', 'Brother')),
self.assertCountEqual(list(classes), [OWLClass(IRI('http://www.benchmark.org/family#', 'Brother')),
OWLClass(IRI('http://www.benchmark.org/family#', 'Child')),
OWLClass(IRI('http://www.benchmark.org/family#', 'Daughter')),
OWLClass(IRI('http://www.benchmark.org/family#', 'Father')),
Expand Down
Loading

0 comments on commit 6cfd36a

Please sign in to comment.