Skip to content

Commit

Permalink
added other integer types of XMLSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
alkidbaci committed Nov 4, 2024
1 parent 0edcafe commit 72af81d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
66 changes: 59 additions & 7 deletions owlapy/owl_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def __new__(cls, value, type_: Optional[OWLDatatype] = None):
return super().__new__(_OWLLiteralImplDateTime)
elif type_ == DurationOWLDatatype:
return super().__new__(_OWLLiteralImplDuration)
elif type_ == PositiveIntegerOWLDatatype:
return super().__new__(_OWLLiteralImplPositiveInteger)
elif type_ == NegativeIntegerOWLDatatype:
return super().__new__(_OWLLiteralImplNegativeInteger)
elif type_ == NonPositiveIntegerOWLDatatype:
return super().__new__(_OWLLiteralImplNonPositiveInteger)
elif type_ == NonNegativeIntegerOWLDatatype:
return super().__new__(_OWLLiteralImplNonNegativeInteger)
else:
return super().__new__(_OWLLiteralImpl)
if isinstance(value, bool):
Expand All @@ -61,7 +69,6 @@ def __new__(cls, value, type_: Optional[OWLDatatype] = None):
return super().__new__(_OWLLiteralImplDate)
elif isinstance(value, Timedelta):
return super().__new__(_OWLLiteralImplDuration)
# TODO XXX
raise NotImplementedError(value)

def get_literal(self) -> str:
Expand Down Expand Up @@ -226,17 +233,20 @@ def get_datatype(self) -> OWLDatatype:
return DoubleOWLDatatype


@total_ordering
class _OWLLiteralImplInteger(OWLLiteral):
__slots__ = '_v'
class _OWLLiteralIntegerInterface(OWLLiteral):
__slots__ = '_v', '_type'

_v: int
_type: type

def __init__(self, value, type_=None):
assert type_ is None or type_ == IntegerOWLDatatype
assert type_ is None or type_ in [IntegerOWLDatatype,
NonNegativeIntegerOWLDatatype,
NonPositiveIntegerOWLDatatype]
if not isinstance(value, int):
value = int(value)
self._v = value
self._type = type_

def __eq__(self, other):
if type(other) is type(self):
Expand All @@ -252,7 +262,7 @@ def __hash__(self):
return hash(self._v)

def __repr__(self):
return f'OWLLiteral({self._v})'
return f'OWLLiteral({self._v}, {self._type})'

def is_integer(self) -> bool:
return True
Expand All @@ -264,7 +274,45 @@ def parse_integer(self) -> int:
# noinspection PyMethodMayBeStatic
def get_datatype(self) -> OWLDatatype:
# documented in parent
return IntegerOWLDatatype
return self._type


@total_ordering
class _OWLLiteralImplInteger(_OWLLiteralIntegerInterface):

def __init__(self, value):
super().__init__(value, IntegerOWLDatatype)


@total_ordering
class _OWLLiteralImplNonNegativeInteger(_OWLLiteralIntegerInterface):

def __init__(self, value):
assert value >= 0, "Negative value used to initialize a literal of type: " + str(type(self))
super().__init__(value, NonNegativeIntegerOWLDatatype)


@total_ordering
class _OWLLiteralImplNonPositiveInteger(_OWLLiteralIntegerInterface):

def __init__(self, value):
assert value <= 0, "Positive value used to initialize a literal of type: " + str(type(self))
super().__init__(value, NonPositiveIntegerOWLDatatype)


@total_ordering
class _OWLLiteralImplPositiveInteger(_OWLLiteralIntegerInterface):

def __init__(self, value):
assert value <= 0, "Non-Positive value used to initialize a literal of type: " + str(type(self))
super().__init__(value, PositiveIntegerOWLDatatype)


@total_ordering
class _OWLLiteralImplNegativeInteger(_OWLLiteralIntegerInterface):
def __init__(self, value):
assert value <= 0, "Non-Negative value used to initialize a literal of type: " + str(type(self))
super().__init__(value, NegativeIntegerOWLDatatype)


class _OWLLiteralImplBoolean(OWLLiteral):
Expand Down Expand Up @@ -516,6 +564,10 @@ def __repr__(self):

DoubleOWLDatatype: Final = OWLDatatype(XSDVocabulary.DOUBLE) #: An object representing a double datatype.
IntegerOWLDatatype: Final = OWLDatatype(XSDVocabulary.INTEGER) #: An object representing an integer datatype.
NonNegativeIntegerOWLDatatype: Final = OWLDatatype(XSDVocabulary.NONNEGATIVEINTEGER) #: An object representing a non negative integer datatype.
NonPositiveIntegerOWLDatatype: Final = OWLDatatype(XSDVocabulary.NONPOSITIVEINTEGER) #: An object representing a non positive integer datatype.
NegativeIntegerOWLDatatype: Final = OWLDatatype(XSDVocabulary.NEGATIVEINTEGER) #: An object representing a negative integer datatype.
PositiveIntegerOWLDatatype: Final = OWLDatatype(XSDVocabulary.POSITIVEINTEGER) #: An object representing a positive integer datatype.
BooleanOWLDatatype: Final = OWLDatatype(XSDVocabulary.BOOLEAN) #: An object representing the boolean datatype.
StringOWLDatatype: Final = OWLDatatype(XSDVocabulary.STRING) #: An object representing the string datatype.
DateOWLDatatype: Final = OWLDatatype(XSDVocabulary.DATE) #: An object representing the date datatype.
Expand Down
5 changes: 4 additions & 1 deletion owlapy/vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def __init__(self, remainder: str):
super().__init__(namespaces.XSD, remainder)
DECIMAL: Final = "decimal" #:
INTEGER: Final = "integer" #:
NONNEGATIVEINTEGER: Final = "nonNegativeInteger" #:
NONPOSITIVEINTEGER: Final = "nonPositiveInteger" #:
POSITIVEINTEGER: Final = "positiveInteger" #:
NEGATIVEINTEGER: Final = "negativeInteger" #:
LONG: Final = "long" #:
DOUBLE: Final = "double" #:
FLOAT: Final = "float" #:
Expand All @@ -84,7 +88,6 @@ def __init__(self, remainder: str):

_X = TypeVar('_X')


# TODO: Add langRange facet
class OWLFacet(_Vocabulary, Enum, metaclass=_meta_Enum):
"""Enumerations for OWL facets."""
Expand Down

0 comments on commit 72af81d

Please sign in to comment.