Skip to content

Commit

Permalink
First Phase of Refactoring is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Demirrr committed Apr 9, 2024
1 parent d676885 commit 0e52d17
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 183 deletions.
159 changes: 2 additions & 157 deletions owlapy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from owlapy.meta_classes import HasIRI, HasOperands, HasFiller, HasCardinality
from owlapy.owl_class_expression import OWLNaryBooleanClassExpression, OWLClassExpression, OWLObjectComplementOf, \
OWLAnonymousClassExpression, OWLBooleanClassExpression, OWLPropertyRange, OWLDataRange, OWLClass, OWLObjectUnionOf, \
OWLObjectIntersectionOf
OWLObjectIntersectionOf, OWLThing, OWLNothing
from owlapy.owl_property import OWLObjectPropertyExpression, OWLProperty, OWLPropertyExpression, \
OWLDataPropertyExpression, OWLDataProperty, OWLObjectProperty
from owlapy.owl_restriction import (OWLRestriction, OWLObjectAllValuesFrom, OWLObjectSomeValuesFrom,
Expand All @@ -26,8 +26,6 @@
OWLDataPropertyDomainAxiom, OWLAxiom, OWLDataPropertyRangeAxiom,
OWLObjectPropertyDomainAxiom, OWLObjectPropertyRangeAxiom)
from owlapy.types import OWLDatatype
# Data and object should be merged
# from owlapy.owl_data import OWLDataComplementOf, OWLDataIntersectionOf,OWLDataUnionOf, OWLNaryDataRange
from owlapy.owl_literal import OWLLiteral


Expand All @@ -41,89 +39,6 @@
_M = TypeVar('_M', bound='OWLOntologyManager') #:


class OWLObjectHasValue(OWLHasValueRestriction[OWLIndividual], OWLObjectRestriction):
"""Represents an ObjectHasValue class expression in the OWL 2 Specification."""
__slots__ = '_property', '_v'
type_index: Final = 3007

_property: OWLObjectPropertyExpression
_v: OWLIndividual

def __init__(self, property: OWLObjectPropertyExpression, individual: OWLIndividual):
"""
Args:
property: The property that the restriction acts along.
individual: Individual for restriction.
Returns:
A HasValue restriction with specified property and value
"""
super().__init__(individual)
self._property = property

def get_property(self) -> OWLObjectPropertyExpression:
# documented in parent
return self._property

def as_some_values_from(self) -> OWLClassExpression:
"""A convenience method that obtains this restriction as an existential restriction with a nominal filler.
Returns:
The existential equivalent of this value restriction. simp(HasValue(p a)) = some(p {a}).
"""
return OWLObjectSomeValuesFrom(self.get_property(), OWLObjectOneOf(self.get_filler()))

def __repr__(self):
return f'OWLObjectHasValue(property={self.get_property()}, individual={self._v})'


class OWLObjectOneOf(OWLAnonymousClassExpression, HasOperands[OWLIndividual]):
"""Represents an ObjectOneOf class expression in the OWL 2 Specification."""
__slots__ = '_values'
type_index: Final = 3004

def __init__(self, values: Union[OWLIndividual, Iterable[OWLIndividual]]):
if isinstance(values, OWLIndividual):
self._values = values,
else:
for _ in values:
assert isinstance(_, OWLIndividual)
self._values = tuple(values)

def individuals(self) -> Iterable[OWLIndividual]:
"""Gets the individuals that are in the oneOf. These individuals represent the exact instances (extension)
of this class expression.
Returns:
The individuals that are the values of this {@code ObjectOneOf} class expression.
"""
yield from self._values

def operands(self) -> Iterable[OWLIndividual]:
# documented in parent
yield from self.individuals()

def as_object_union_of(self) -> OWLClassExpression:
"""Simplifies this enumeration to a union of singleton nominals.
Returns:
This enumeration in a more standard DL form.
simp({a}) = {a} simp({a0, ... , {an}) = unionOf({a0}, ... , {an})
"""
if len(self._values) == 1:
return self
return OWLObjectUnionOf(map(lambda _: OWLObjectOneOf(_), self.individuals()))

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

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

def __repr__(self):
return f'OWLObjectOneOf({self._values})'


class OWLOntologyID:
Expand Down Expand Up @@ -189,75 +104,6 @@ def __eq__(self, other):
return NotImplemented


class OWLDatatypeRestriction(OWLDataRange):
"""Represents a DatatypeRestriction data range in the OWL 2 Specification."""
__slots__ = '_type', '_facet_restrictions'

type_index: Final = 4006

_type: OWLDatatype
_facet_restrictions: Sequence['OWLFacetRestriction']

def __init__(self, type_: OWLDatatype, facet_restrictions: Union['OWLFacetRestriction',
Iterable['OWLFacetRestriction']]):
self._type = type_
if isinstance(facet_restrictions, OWLFacetRestriction):
facet_restrictions = facet_restrictions,
self._facet_restrictions = tuple(facet_restrictions)

def get_datatype(self) -> OWLDatatype:
return self._type

def get_facet_restrictions(self) -> Sequence['OWLFacetRestriction']:
return self._facet_restrictions

def __eq__(self, other):
if type(other) is type(self):
return self._type == other._type \
and self._facet_restrictions == other._facet_restrictions
return NotImplemented

def __hash__(self):
return hash((self._type, self._facet_restrictions))

def __repr__(self):
return f'OWLDatatypeRestriction({repr(self._type)}, {repr(self._facet_restrictions)})'


class OWLFacetRestriction(OWLObject):
"""A facet restriction is used to restrict a particular datatype."""

__slots__ = '_facet', '_literal'

type_index: Final = 4007

_facet: OWLFacet
_literal: 'OWLLiteral'

def __init__(self, facet: OWLFacet, literal: Literals):
self._facet = facet
if isinstance(literal, OWLLiteral):
self._literal = literal
else:
self._literal = OWLLiteral(literal)

def get_facet(self) -> OWLFacet:
return self._facet

def get_facet_value(self) -> 'OWLLiteral':
return self._literal

def __eq__(self, other):
if type(other) is type(self):
return self._facet == other._facet and self._literal == other._literal
return NotImplemented

def __hash__(self):
return hash((self._facet, self._literal))

def __repr__(self):
return f'OWLFacetRestriction({self._facet}, {repr(self._literal)})'


class OWLImportsDeclaration(HasIRI):
"""Represents an import statement in an ontology."""
Expand Down Expand Up @@ -926,8 +772,7 @@ class expression with respect to the imports closure of the root ontology.

"""Important constant objects section"""
# @TODO: Some of them must be removed from here as they are defined under owl literal
OWLThing: Final = OWLClass(OWLRDFVocabulary.OWL_THING.get_iri()) #: : :The OWL Class corresponding to owl:Thing
OWLNothing: Final = OWLClass(OWLRDFVocabulary.OWL_NOTHING.get_iri()) #: : :The OWL Class corresponding to owl:Nothing

#: the built in top object property
OWLTopObjectProperty: Final = OWLObjectProperty(OWLRDFVocabulary.OWL_TOP_OBJECT_PROPERTY.get_iri())
#: the built in bottom object property
Expand Down
3 changes: 2 additions & 1 deletion owlapy/model/providers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""OWL Datatype restriction constructors."""
from typing import Union
from datetime import datetime, date
from owlapy.model import OWLDatatypeRestriction, OWLFacet, OWLFacetRestriction, OWLLiteral
from owlapy.owl_literal import OWLLiteral
from owlapy.owl_restriction import OWLDatatypeRestriction, OWLFacet, OWLFacetRestriction
from pandas import Timedelta

Restriction_Literals = Union[OWLLiteral, int, float, Timedelta, datetime, date]
Expand Down
162 changes: 162 additions & 0 deletions owlapy/owl_restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
from .owl_property import OWLPropertyExpression, OWLObjectPropertyExpression, OWLDataPropertyExpression
from .ranges import OWLPropertyRange, OWLDataRange
from .owl_literal import OWLLiteral
from .owl_individual import OWLIndividual
from .types import OWLDatatype
from .owlobject import OWLObject
from owlapy.vocab import OWLRDFVocabulary, XSDVocabulary, OWLFacet
from datetime import datetime, date
from pandas import Timedelta

_T = TypeVar('_T') #:
_F = TypeVar('_F', bound=OWLPropertyRange) #:

Literals = Union['OWLLiteral', int, float, bool, Timedelta, datetime, date, str] #:

class OWLRestriction(OWLAnonymousClassExpression):
"""Represents an Object Property Restriction or Data Property Restriction in the OWL 2 specification."""
__slots__ = ()
Expand Down Expand Up @@ -530,6 +539,56 @@ def get_property(self) -> OWLDataPropertyExpression:
return self._property



class OWLObjectOneOf(OWLAnonymousClassExpression, HasOperands[OWLIndividual]):
"""Represents an ObjectOneOf class expression in the OWL 2 Specification."""
__slots__ = '_values'
type_index: Final = 3004

def __init__(self, values: Union[OWLIndividual, Iterable[OWLIndividual]]):
if isinstance(values, OWLIndividual):
self._values = values,
else:
for _ in values:
assert isinstance(_, OWLIndividual)
self._values = tuple(values)

def individuals(self) -> Iterable[OWLIndividual]:
"""Gets the individuals that are in the oneOf. These individuals represent the exact instances (extension)
of this class expression.
Returns:
The individuals that are the values of this {@code ObjectOneOf} class expression.
"""
yield from self._values

def operands(self) -> Iterable[OWLIndividual]:
# documented in parent
yield from self.individuals()

def as_object_union_of(self) -> OWLClassExpression:
"""Simplifies this enumeration to a union of singleton nominals.
Returns:
This enumeration in a more standard DL form.
simp({a}) = {a} simp({a0, ... , {an}) = unionOf({a0}, ... , {an})
"""
if len(self._values) == 1:
return self
return OWLObjectUnionOf(map(lambda _: OWLObjectOneOf(_), self.individuals()))

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

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

def __repr__(self):
return f'OWLObjectOneOf({self._values})'


class OWLDataOneOf(OWLDataRange, HasOperands[OWLLiteral]):
"""Represents DataOneOf in the OWL 2 Specification."""
type_index: Final = 4003
Expand Down Expand Up @@ -566,3 +625,106 @@ def __eq__(self, other):

def __repr__(self):
return f'OWLDataOneOf({self._values})'


class OWLObjectHasValue(OWLHasValueRestriction[OWLIndividual], OWLObjectRestriction):
"""Represents an ObjectHasValue class expression in the OWL 2 Specification."""
__slots__ = '_property', '_v'
type_index: Final = 3007

_property: OWLObjectPropertyExpression
_v: OWLIndividual

def __init__(self, property: OWLObjectPropertyExpression, individual: OWLIndividual):
"""
Args:
property: The property that the restriction acts along.
individual: Individual for restriction.
Returns:
A HasValue restriction with specified property and value
"""
super().__init__(individual)
self._property = property

def get_property(self) -> OWLObjectPropertyExpression:
# documented in parent
return self._property

def as_some_values_from(self) -> OWLClassExpression:
"""A convenience method that obtains this restriction as an existential restriction with a nominal filler.
Returns:
The existential equivalent of this value restriction. simp(HasValue(p a)) = some(p {a}).
"""
return OWLObjectSomeValuesFrom(self.get_property(), OWLObjectOneOf(self.get_filler()))

def __repr__(self):
return f'OWLObjectHasValue(property={self.get_property()}, individual={self._v})'
class OWLDatatypeRestriction(OWLDataRange):
"""Represents a DatatypeRestriction data range in the OWL 2 Specification."""
__slots__ = '_type', '_facet_restrictions'

type_index: Final = 4006

_type: OWLDatatype
_facet_restrictions: Sequence['OWLFacetRestriction']

def __init__(self, type_: OWLDatatype, facet_restrictions: Union['OWLFacetRestriction',
Iterable['OWLFacetRestriction']]):
self._type = type_
if isinstance(facet_restrictions, OWLFacetRestriction):
facet_restrictions = facet_restrictions,
self._facet_restrictions = tuple(facet_restrictions)

def get_datatype(self) -> OWLDatatype:
return self._type

def get_facet_restrictions(self) -> Sequence['OWLFacetRestriction']:
return self._facet_restrictions

def __eq__(self, other):
if type(other) is type(self):
return self._type == other._type \
and self._facet_restrictions == other._facet_restrictions
return NotImplemented

def __hash__(self):
return hash((self._type, self._facet_restrictions))

def __repr__(self):
return f'OWLDatatypeRestriction({repr(self._type)}, {repr(self._facet_restrictions)})'
class OWLFacetRestriction(OWLObject):
"""A facet restriction is used to restrict a particular datatype."""

__slots__ = '_facet', '_literal'

type_index: Final = 4007

_facet: OWLFacet
_literal: 'OWLLiteral'

def __init__(self, facet: OWLFacet, literal: Literals):
self._facet = facet
if isinstance(literal, OWLLiteral):
self._literal = literal
else:
self._literal = OWLLiteral(literal)

def get_facet(self) -> OWLFacet:
return self._facet

def get_facet_value(self) -> 'OWLLiteral':
return self._literal

def __eq__(self, other):
if type(other) is type(self):
return self._facet == other._facet and self._literal == other._literal
return NotImplemented

def __hash__(self):
return hash((self._facet, self._literal))

def __repr__(self):
return f'OWLFacetRestriction({self._facet}, {repr(self._literal)})'

Loading

0 comments on commit 0e52d17

Please sign in to comment.