dice-group / owlapy

OWLAPY is a Python Framework for creating and manipulating OWL Ontologies.
MIT License
21 stars 2 forks source link

complement of a complement of an owl class should be equal to the owl class #101

Closed Demirrr closed 1 week ago

Demirrr commented 3 weeks ago
A=OWLClass("http://example.com/father#A")
assert A.get_object_complement_of().get_object_complement_of() == A
LckyLke commented 1 week ago

Changing this: https://github.com/dice-group/owlapy/blob/a82f76f52a76b6c100fa4e6c39a90d3a93761451/owlapy/class_expression/class_expression.py#L64-L66

To check if the provide class expression is already a complement:

    def get_object_complement_of(self) -> 'OWLObjectComplementOf':
        # documented in parent
        if isinstance(self, OWLObjectComplementOf):
            return self.get_operand()
        return OWLObjectComplementOf(self)
LckyLke commented 1 week ago

or for more general solution, we could check this in the init of OWLObejctComplementOf? Depends on if this automatic simplification is desired, I guess...

That is by overwriting the __new__ method:

class OWLObjectComplementOf(OWLBooleanClassExpression, HasOperands[OWLClassExpression]):
    """Represents an ObjectComplementOf class expression in the OWL 2 Specification."""
    __slots__ = '_operand'
    type_index: Final = 3003

    _operand: OWLClassExpression

    def __new__(cls, op: OWLClassExpression):
        """
        Creates a new instance or returns the operand if op is already a complement.
        """
        if isinstance(op, OWLObjectComplementOf):
            return op.get_operand()
        else:
            return super(OWLObjectComplementOf, cls).__new__(cls)

    def __init__(self, op: OWLClassExpression):
        """
        Initializes the instance with the given operand.
        """
        self._operand = op

which would also simplify:

print(OWLObjectComplementOf(OWLObjectComplementOf(A)))

-> OWLClass(IRI('http://example.com/father#', 'A'))

Demirrr commented 1 week ago

I like your proposed solution. What do you think @alkidbaci ?

alkidbaci commented 1 week ago

This is a great solution