"""OWL Base classes"""
from abc import ABCMeta, abstractmethod
from typing import TypeVar
from .meta_classes import HasIRI
_I = TypeVar('_I', bound='IRI') # noqa: F821
[docs]
class OWLObject(metaclass=ABCMeta):
"""Base interface for OWL objects"""
__slots__ = ()
[docs]
@abstractmethod
def __eq__(self, other):
pass
[docs]
@abstractmethod
def __hash__(self):
pass
[docs]
@abstractmethod
def __repr__(self):
pass
# default
[docs]
def is_anonymous(self) -> bool:
return True
[docs]
class OWLObjectRenderer(metaclass=ABCMeta):
"""Abstract class with a render method to render an OWL Object into a string."""
[docs]
@abstractmethod
def render(self, o: OWLObject) -> str:
"""Render OWL Object to string.
Args:
o: OWL Object.
Returns:
String rendition of OWL object.
"""
pass
[docs]
class OWLObjectParser(metaclass=ABCMeta):
"""Abstract class with a parse method to parse a string to an OWL Object."""
[docs]
@abstractmethod
def parse_expression(self, expression_str: str) -> OWLObject:
"""Parse a string to an OWL Object.
Args:
expression_str (str): Expression string.
Returns:
The OWL Object which is represented by the string.
"""
pass
[docs]
class OWLNamedObject(OWLObject, HasIRI, metaclass=ABCMeta):
"""Represents a named object for example, class, property, ontology etc. - i.e. anything that has an
IRI as its name."""
__slots__ = ()
_iri: _I
[docs]
def __eq__(self, other):
if type(other) is type(self):
return self._iri == other._iri
else:
return False
# raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}")
[docs]
def __lt__(self, other):
if type(other) is type(self):
return self._iri.as_str() < other._iri.as_str()
return NotImplemented
[docs]
def __hash__(self):
return hash((type(self).__name__, self._iri))
[docs]
def __repr__(self):
return f"{type(self).__name__}({repr(self._iri)})"
[docs]
class OWLEntity(OWLNamedObject, metaclass=ABCMeta):
"""Represents Entities in the OWL 2 Specification.
(https://www.w3.org/TR/owl2-syntax/#Entities.2C_Literals.2C_and_Anonymous_Individuals)"""
__slots__ = ()
[docs]
def to_string_id(self) -> str:
return self.str
[docs]
def is_anonymous(self) -> bool:
return False
pass