DatatypeHandling

Most of the XSD datatypes are handled directly by RDFLib. However, in some cases, that is not good enough. There are two major reasons for this:

  1. Some datatypes are missing from RDFLib and required by OWL 2 RL and/or RDFS.

  2. In other cases, though the datatype is present, RDFLib is fairly lax in checking the lexical value of those datatypes. Typical case is boolean.

Some of these deficiencies are handled by this module. All the functions convert the lexical value into a python datatype (or return the original string if this is not possible) which will be used, e.g., for comparisons (equalities). If the lexical value constraints are not met, exceptions are raised.

Requires: RDFLib, 4.0.0 and higher.

License: This software is available for use under the W3C Software License.

Organization: World Wide Web Consortium

Author: Ivan Herman

owlrl.DatatypeHandling.AltXSDToPYTHON = {rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral'): <function _strToPlainLiteral>, rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral'): <function _strToXMLLiteral>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#NCName'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#NMTOKEN'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#Name'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#anyURI'): <function _strToAnyURI>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#base64Binary'): <function _strToBase64Binary>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#boolean'): <function _strToBool>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#byte'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#date'): <function _strToDate>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTimeStamp'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#decimal'): <function _strToDecimal>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'): <function _strToDouble>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#float'): <function _strToFloat>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#gDay'): <function _strTogDay>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#gMonth'): <function _strTogMonth>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#gMonthDay'): <function _strTogMonthDay>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#gYear'): <function _strTogYear>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#gYearMonth'): <function _strTogYearMonth>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#hexBinary'): <function _strToHexBinary>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#int'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'): <class 'int'>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#language'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#long'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#negativeInteger'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#nonNegativeInteger'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#nonPositiveInteger'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#normalizedString'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#positiveInteger'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#short'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#string'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#time'): <function _strToTime>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#token'): <function _strToToken>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#unsignedByte'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#unsignedInt'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#unsignedLong'): <function <lambda>>, rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#unsignedShort'): <function <lambda>>}

Replacement of RDFLib’s conversion function. Each entry assigns a function to an XSD datatype, attempting to convert a string to a Python datatype (or raise an exception if some problem is found)

owlrl.DatatypeHandling.use_Alt_lexical_conversions()[source]

Registering the datatypes item for RDFLib, ie, bind the dictionary values. The ‘bind’ method of RDFLib adds extra datatypes to the registered ones in RDFLib, though the table used here (I.e., AltXSDToPYTHON) actually overrides all of the default conversion routines. The method also add a Decimal entry to the PythonToXSD list of RDFLib.

owlrl.DatatypeHandling.use_RDFLib_lexical_conversions()[source]

Restore the original (ie, RDFLib) set of lexical conversion routines.

AltXSDToPYTHON Table

Note

The code below is not extracted automatically from the source code.

If there are any errors, please make a pull request or an issue: https://github.com/RDFLib/OWL-RL

AltXSDToPYTHON = {
    ns_xsd["language"]: lambda v: _strToVal_Regexp(v, _re_language),
    ns_xsd["NMTOKEN"]: lambda v: _strToVal_Regexp(v, _re_NMTOKEN, re.U),
    ns_xsd["Name"]: lambda v: _strToVal_Regexp(v, _re_NMTOKEN, re.U, _re_Name_ex),
    ns_xsd["NCName"]: lambda v: _strToVal_Regexp(v, _re_NCName, re.U, _re_NCName_ex),
    ns_xsd["token"]: _strToToken,
    ns_rdf["PlainLiteral"]: _strToPlainLiteral,
    ns_xsd["boolean"]: _strToBool,
    ns_xsd["decimal"]: _strToDecimal,
    ns_xsd["anyURI"]: _strToAnyURI,
    ns_xsd["base64Binary"]: _strToBase64Binary,
    ns_xsd["double"]: _strToDouble,
    ns_xsd["float"]: _strToFloat,
    ns_xsd["byte"]: lambda v: _strToBoundNumeral(v, _limits_byte, int),
    ns_xsd["int"]: lambda v: _strToBoundNumeral(v, _limits_int, int),
    ns_xsd["long"]: lambda v: _strToBoundNumeral(v, _limits_long, int),
    ns_xsd["positiveInteger"]: lambda v: _strToBoundNumeral(v, _limits_positiveInteger, int),
    ns_xsd["nonPositiveInteger"]: lambda v: _strToBoundNumeral(v, _limits_nonPositiveInteger, int),
    ns_xsd["negativeInteger"]: lambda v: _strToBoundNumeral(v, _limits_negativeInteger, int),
    ns_xsd["nonNegativeInteger"]: lambda v: _strToBoundNumeral(v, _limits_nonNegativeInteger, int),
    ns_xsd["short"]: lambda v: _strToBoundNumeral(v, _limits_short, int),
    ns_xsd["unsignedByte"]: lambda v: _strToBoundNumeral(v, _limits_unsignedByte, int),
    ns_xsd["unsignedShort"]: lambda v: _strToBoundNumeral(v, _limits_unsignedShort, int),
    ns_xsd["unsignedInt"]: lambda v: _strToBoundNumeral(v, _limits_unsignedInt, int),
    ns_xsd["unsignedLong"]: lambda v: _strToBoundNumeral(v, _limits_unsignedLong, int),
    ns_xsd["hexBinary"]: _strToHexBinary,
    ns_xsd["dateTime"]: lambda v: _strToDateTimeAndStamp(v, False),
    ns_xsd["dateTimeStamp"]: lambda v: _strToDateTimeAndStamp(v, True),
    ns_rdf["XMLLiteral"]: _strToXMLLiteral,
    ns_xsd["integer"]: int,
    ns_xsd["string"]: lambda v: v,
    ns_rdf["HTML"]: lambda v: v,
    ns_xsd["normalizedString"]: lambda v: _strToVal_Regexp(v, _re_token),

    # These are RDFS specific...
    ns_xsd["time"]: _strToTime,
    ns_xsd["date"]: _strToDate,
    ns_xsd["gYearMonth"]: _strTogYearMonth,
    ns_xsd["gYear"]: _strTogYear,
    ns_xsd["gMonthDay"]: _strTogMonthDay,
    ns_xsd["gDay"]: _strTogDay,
    ns_xsd["gMonth"]: _strTogMonth,
}

See also

View the source code DatatypeHandling.py.