From 72af81d7984be78f8c067fcf30d382433c6f00d0 Mon Sep 17 00:00:00 2001 From: Alkid Date: Mon, 4 Nov 2024 16:33:52 +0100 Subject: [PATCH] added other integer types of XMLSchema --- owlapy/owl_literal.py | 66 ++++++++++++++++++++++++++++++++++++++----- owlapy/vocab.py | 5 +++- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/owlapy/owl_literal.py b/owlapy/owl_literal.py index d9b1487a..592f111d 100644 --- a/owlapy/owl_literal.py +++ b/owlapy/owl_literal.py @@ -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): @@ -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: @@ -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): @@ -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 @@ -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): @@ -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. diff --git a/owlapy/vocab.py b/owlapy/vocab.py index be129777..f5e76473 100644 --- a/owlapy/vocab.py +++ b/owlapy/vocab.py @@ -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" #: @@ -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."""