Skip to content

Commit

Permalink
Class expression script will become a python module
Browse files Browse the repository at this point in the history
  • Loading branch information
Demirrr committed Apr 9, 2024
1 parent 066e046 commit 2c480d5
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 155 deletions.
2 changes: 2 additions & 0 deletions owlapy/class_expression/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .class_expression import OWLClassExpression, OWLAnonymousClassExpression, OWLBooleanClassExpression, OWLObjectComplementOf
from .owl_class import OWLClass
107 changes: 107 additions & 0 deletions owlapy/class_expression/class_expression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from ..ranges import OWLPropertyRange, OWLDataRange
from abc import abstractmethod, ABCMeta
from ..meta_classes import HasOperands

from typing import Final, Iterable
class OWLClassExpression(OWLPropertyRange):
"""An OWL 2 Class Expression (https://www.w3.org/TR/owl2-syntax/#Class_Expressions) """
__slots__ = ()

@abstractmethod
def is_owl_thing(self) -> bool:
"""Determines if this expression is the built in class owl:Thing. This method does not determine if the class
is equivalent to owl:Thing.
Returns:
True if this expression is owl:Thing.
"""
pass

@abstractmethod
def is_owl_nothing(self) -> bool:
"""Determines if this expression is the built in class owl:Nothing. This method does not determine if the class
is equivalent to owl:Nothing.
"""
pass

@abstractmethod
def get_object_complement_of(self) -> 'OWLObjectComplementOf':
"""Gets the object complement of this class expression.
Returns:
A class expression that is the complement of this class expression.
"""
pass

@abstractmethod
def get_nnf(self) -> 'OWLClassExpression':
"""Gets the negation normal form of the complement of this expression.
Returns:
A expression that represents the NNF of the complement of this expression.
"""
pass


class OWLAnonymousClassExpression(OWLClassExpression, metaclass=ABCMeta):
"""A Class Expression which is not a named Class."""

def is_owl_nothing(self) -> bool:
# documented in parent
return False

def is_owl_thing(self) -> bool:
# documented in parent
return False

def get_object_complement_of(self) -> 'OWLObjectComplementOf':
# documented in parent
return OWLObjectComplementOf(self)

def get_nnf(self) -> 'OWLClassExpression':
# documented in parent
from owlapy.util import NNF
return NNF().get_class_nnf(self)


class OWLBooleanClassExpression(OWLAnonymousClassExpression, metaclass=ABCMeta):
"""Represent an anonymous boolean class expression."""
__slots__ = ()
pass


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

_operand: OWLClassExpression

def __init__(self, op: OWLClassExpression):
"""
Args:
op: Class expression to complement.
"""
self._operand = op

def get_operand(self) -> OWLClassExpression:
"""
Returns:
The wrapped expression.
"""
return self._operand

def operands(self) -> Iterable[OWLClassExpression]:
# documented in parent
yield self._operand

def __repr__(self):
return f"OWLObjectComplementOf({repr(self._operand)})"

def __eq__(self, other):
if type(other) is type(self):
return self._operand == other._operand
return NotImplemented

def __hash__(self):
return hash(self._operand)
57 changes: 57 additions & 0 deletions owlapy/class_expression/owl_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from .class_expression import OWLClassExpression, OWLObjectComplementOf
from ..owlobject import OWLObject, OWLEntity
from typing import Final, Union
from ..iri import IRI


class OWLClass(OWLClassExpression, OWLEntity):
"""An OWL 2 named Class"""
__slots__ = '_iri', '_is_nothing', '_is_thing'
type_index: Final = 1001

_iri: 'IRI'
_is_nothing: bool
_is_thing: bool

def __init__(self, iri: Union[IRI, str]):
"""Gets an instance of OWLClass that has the specified IRI.
Args:
iri:
"""
if isinstance(iri, IRI):
self._iri = iri
else:
self._iri = IRI.create(iri)

self._is_nothing = self._iri.is_nothing()
self._is_thing = self._iri.is_thing()

def get_iri(self) -> 'IRI':
# documented in parent
return self._iri

def is_owl_thing(self) -> bool:
# documented in parent
return self._is_thing

def is_owl_nothing(self) -> bool:
# documented in parent
return self._is_nothing

def get_object_complement_of(self) -> OWLObjectComplementOf:
# documented in parent
return OWLObjectComplementOf(self)

def get_nnf(self) -> 'OWLClass':
# documented in parent
return self

@property
def str(self):
return self.get_iri().as_str()

@property
def reminder(self) -> str:
"""The reminder of the IRI """
return self.get_iri().get_remainder()
157 changes: 2 additions & 155 deletions owlapy/owl_class_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,162 +9,9 @@
from .iri import IRI
from owlapy.vocab import OWLRDFVocabulary, XSDVocabulary

from .class_expression import (OWLClassExpression, OWLAnonymousClassExpression, OWLBooleanClassExpression,
OWLObjectComplementOf,OWLClass)

class OWLClassExpression(OWLPropertyRange):
"""An OWL 2 Class Expression."""
__slots__ = ()

@abstractmethod
def is_owl_thing(self) -> bool:
"""Determines if this expression is the built in class owl:Thing. This method does not determine if the class
is equivalent to owl:Thing.
Returns:
True if this expression is owl:Thing.
"""
pass

@abstractmethod
def is_owl_nothing(self) -> bool:
"""Determines if this expression is the built in class owl:Nothing. This method does not determine if the class
is equivalent to owl:Nothing.
"""
pass

@abstractmethod
def get_object_complement_of(self) -> 'OWLObjectComplementOf':
"""Gets the object complement of this class expression.
Returns:
A class expression that is the complement of this class expression.
"""
pass

@abstractmethod
def get_nnf(self) -> 'OWLClassExpression':
"""Gets the negation normal form of the complement of this expression.
Returns:
A expression that represents the NNF of the complement of this expression.
"""
pass


class OWLAnonymousClassExpression(OWLClassExpression, metaclass=ABCMeta):
"""A Class Expression which is not a named Class."""

def is_owl_nothing(self) -> bool:
# documented in parent
return False

def is_owl_thing(self) -> bool:
# documented in parent
return False

def get_object_complement_of(self) -> 'OWLObjectComplementOf':
# documented in parent
return OWLObjectComplementOf(self)

def get_nnf(self) -> 'OWLClassExpression':
# documented in parent
from owlapy.util import NNF
return NNF().get_class_nnf(self)


class OWLBooleanClassExpression(OWLAnonymousClassExpression, metaclass=ABCMeta):
"""Represent an anonymous boolean class expression."""
__slots__ = ()
pass


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

_operand: OWLClassExpression

def __init__(self, op: OWLClassExpression):
"""
Args:
op: Class expression to complement.
"""
self._operand = op

def get_operand(self) -> OWLClassExpression:
"""
Returns:
The wrapped expression.
"""
return self._operand

def operands(self) -> Iterable[OWLClassExpression]:
# documented in parent
yield self._operand

def __repr__(self):
return f"OWLObjectComplementOf({repr(self._operand)})"

def __eq__(self, other):
if type(other) is type(self):
return self._operand == other._operand
return NotImplemented

def __hash__(self):
return hash(self._operand)


class OWLClass(OWLClassExpression, OWLEntity):
"""An OWL 2 named Class"""
__slots__ = '_iri', '_is_nothing', '_is_thing'
type_index: Final = 1001

_iri: 'IRI'
_is_nothing: bool
_is_thing: bool

def __init__(self, iri: Union[IRI,str]):
"""Gets an instance of OWLClass that has the specified IRI.
Args:
iri:
"""
if isinstance(iri, IRI):
self._iri = iri
else:
self._iri = IRI.create(iri)

self._is_nothing = self._iri.is_nothing()
self._is_thing = self._iri.is_thing()

def get_iri(self) -> 'IRI':
# documented in parent
return self._iri

def is_owl_thing(self) -> bool:
# documented in parent
return self._is_thing

def is_owl_nothing(self) -> bool:
# documented in parent
return self._is_nothing

def get_object_complement_of(self) -> OWLObjectComplementOf:
# documented in parent
return OWLObjectComplementOf(self)

def get_nnf(self) -> 'OWLClass':
# documented in parent
return self

@property
def str(self):
return self.get_iri().as_str()

@property
def reminder(self) -> str:
"""The reminder of the IRI """
return self.get_iri().get_remainder()

class OWLNaryBooleanClassExpression(OWLBooleanClassExpression, HasOperands[OWLClassExpression]):
"""OWLNaryBooleanClassExpression."""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_class_expression_semantics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from owlapy.iri import IRI

from owlapy.owl_class_expression import OWLClass, OWLObjectIntersectionOf, OWLObjectComplementOf, OWLObjectUnionOf
from owlapy.owl_property import OWLObjectProperty
from owlapy.owl_restriction import OWLObjectSomeValuesFrom, OWLObjectAllValuesFrom

from owlapy.owl2sparql.converter import owl_expression_to_sparql
from owlapy.render import owl_expression_to_dl
from owlapy.owl_class_expression import OWLClassExpression


class TestClassExpression:
def test_iri(self):
# Create the male class
C = OWLClass("http://example.com/society#C")
assert isinstance(C, OWLClassExpression)
Not_C=OWLObjectComplementOf(C)
assert isinstance(Not_C, OWLClassExpression)
C_AND_C=OWLObjectIntersectionOf([C, C])
assert isinstance(C_AND_C, OWLClassExpression)
C_OR_C = OWLObjectUnionOf([C, C])
assert isinstance(C_OR_C, OWLClassExpression)

hasChild = OWLObjectProperty("http://example.com/society#hasChild")
assert isinstance(OWLObjectSomeValuesFrom(hasChild, C), OWLClassExpression)

assert isinstance(OWLObjectAllValuesFrom(hasChild, C), OWLClassExpression)

0 comments on commit 2c480d5

Please sign in to comment.