-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Class expression script will become a python module
- Loading branch information
Showing
5 changed files
with
195 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |