RestrictedDatatype

Module to datatype restrictions, i.e., data ranges.

The module implements the following aspects of datatype restrictions:

  • a new datatype is created run-time and added to the allowed and accepted datatypes; literals are checked whether they abide to the restrictions
  • the new datatype is defined to be a ‘subClass’ of the restricted datatype
  • literals of the restricted datatype and that abide to the restrictions defined by the facets are also assigned to be of the new type

The last item is important to handle the following structures:

ex:RE a owl:Restriction ;
   owl:onProperty ex:p ;
   owl:someValuesFrom [
       a rdfs:Datatype ;
       owl:onDatatype xsd:string ;
       owl:withRestrictions (
           [ xsd:minLength "3"^^xsd:integer ]
           [ xsd:maxLength "6"^^xsd:integer ]
       )
   ]
.
ex:q ex:p "abcd"^^xsd:string.

In the case above the system can then infer that ex:q is also of type ex:RE.

Datatype restrictions are used by the OWLRLExtras.OWLRL_Extension extension class.

The implementation is not 100% complete. Some things that an ideal implementation should do are not done yet like:

  • checking whether a facet is of a datatype that is allowed for that facet
  • handling of non-literals in the facets (ie, if the resource is defined to be of type literal, but whose value is defined via a separate owl:sameAs somewhere else)

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

class owlrl.RestrictedDatatype.RestrictedDatatype(type_uri, base_type, facets)[source]

Bases: owlrl.RestrictedDatatype.RestrictedDatatypeCore

Implementation of a datatype with facets, ie, datatype with restrictions.

Parameters:
  • type_uri – URI of the datatype being defined.
  • base_type – URI of the base datatype, ie, the one being restricted.
  • facets – List of (facetURI, value) pairs.

:ivar datatype : The URI for this datatype.

Variables:
  • base_type – URI of the datatype that is restricted.
  • converter – Method to convert a literal of the base type to a Python value (DatatypeHandling.AltXSDToPYTHON).
  • minExclusive – Value for the :code`xsd:minExclusive` facet, initialized to None and set to the right value if a facet is around.
  • minInclusive – Value for the xsd:minInclusive facet, initialized to None and set to the right value if a facet is around.
  • maxExclusive – Value for the xsd:maxExclusive facet, initialized to None and set to the right value if a facet is around.
  • maxInclusive – Value for the xsd:maxInclusive facet, initialized to None and set to the right value if a facet is around.
  • minLength – Value for the xsd:minLength facet, initialized to None and set to the right value if a facet is around.
  • maxLength – Value for the xsd:maxLength facet, initialized to None and set to the right value if a facet is around.
  • length – Value for the xsd:length facet, initialized to None and set to the right value if a facet is around.
  • pattern – Array of patterns for the xsd:pattern facet, initialized to [] and set to the right value if a facet is around.
  • langRange – Array of language ranges for the rdf:langRange facet, initialized to [] and set to the right value if a facet is around.
  • check_methods – List of class methods that are relevant for the given base_type.
  • toPython – Function to convert a Literal of the specified type to a Python value. Is defined by lambda v: _lit_to_value(self, v), see _lit_to_value().
checkValue(value)[source]

Check whether the (Python) value abides to the constraints defined by the current facets.

Parameters:value – The value to be checked.
Return type:bool
class owlrl.RestrictedDatatype.RestrictedDatatypeCore(type_uri, base_type)[source]

Bases: object

An ‘abstract’ superclass for datatype restrictions. The instance variables listed here are used in general, without the specificities of the concrete restricted datatype.

This module defines the RestrictedDatatype class that corresponds to the datatypes and their restrictions defined in the OWL 2 standard. Other modules may subclass this class to define new datatypes with restrictions.

Variables:
  • type_uri – The URI for this datatype.
  • base_type – URI of the datatype that is restricted.
  • toPython – Function to convert a Literal of the specified type to a Python value.
checkValue(value)[source]

Check whether the (Python) value abides to the constraints defined by the current facets.

Parameters:value – The value to be checked.
Return type:bool
owlrl.RestrictedDatatype.extract_faceted_datatypes(core, graph)[source]

Extractions of restricted (i.e., faceted) datatypes from the graph.

Parameters:
  • core (Closure.Core) – The core closure instance that is being handled.
  • graph (RDFLib.Graph) – RDFLib graph.
Returns:

List of RestrictedDatatype instances.

Return type:

list

owlrl.RestrictedDatatype._lit_to_value(dt, v)[source]

This method is used to convert a string to a value with facet checking. RDF Literals are converted to Python values using this method; if there is a problem, an exception is raised (and caught higher up to generate an error message).

The method is the equivalent of all the methods in the DatatypeHandling module, and is registered to the system run time, as new restricted datatypes are discovered.

(Technically, the registration is done via a lambda v: _lit_to_value(self,v) setting from within a RestrictedDatatype instance).

Parameters:
  • dt (RestrictedDatatype) – Faceted datatype.
  • v – Literal to be converted and checked.
Raises:

ValueError – Invalid literal value.