OWLRLExtras

Extension to OWL 2 RL, ie, some additional rules added to the system from OWL 2 Full. It is implemented through the OWLRL_Extension class, whose reference has to be passed to the relevant semantic class (i.e., either the OWL 2 RL or the combined closure class) as an ‘extension’.

The added rules and features are:

  • self restriction
  • owl:rational datatype
  • datatype restrictions via facets

In more details, the rules that are added:

  1. self restriction 1: ?z owl:hasSelf ?x. ?x owl:onProperty ?p. ?y rdf:type ?z. => ?y ?p ?y.
  2. self restriction 2: ?z owl:hasSelf ?x. ?x owl:onProperty ?p. ?y ?p ?y. => ?y rdf:type ?z.

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.OWLRLExtras.OWLRL_Extension(graph, axioms, daxioms, rdfs=False)[source]

Bases: owlrl.CombinedClosure.RDFS_OWLRL_Semantics

Additional rules to OWL 2 RL. The initialization method also adds the owl:rational datatype to the set of allowed datatypes with the _strToRational() function as a conversion between the literal form and a Rational. The xsd:decimal datatype is also set to be a subclass of owl:rational. Furthermore, the restricted datatypes are extracted from the graph using a separate method in a different module (RestrictedDatatype.extract_faceted_datatypes()), and all those datatypes are also added to the set of allowed datatypes. In the case of the restricted datatypes and extra subsumption relationship is set up between the restricted and the base datatypes.

Variables:
  • extra_axioms – Additional axioms that have to be added to the deductive closure (in case the axiomatic triples are required).
  • restricted_datatypes – list of the datatype restriction from RestrictedDatatype.
add_axioms()[source]

Add the OWLRL_Extension.extra_axioms, related to the self restrictions.

add_d_axioms()

This is not really complete, because it just uses the comparison possibilities that RDFLib provides.

add_error(message)

Add an error message

Parameters:message (str) – Error message.
static add_new_datatype(uri, conversion_function, datatype_list, subsumption_dict=None, subsumption_key=None, subsumption_list=None)

If an extension wants to add new datatypes, this method should be invoked at initialization time.

Parameters:
  • uri – URI for the new datatypes, like owl_ns[“Rational”].
  • conversion_function – A function converting the lexical representation of the datatype to a Python value, possibly raising an exception in case of unsuitable lexical form.
  • datatype_list (list) – List of datatypes already in use that has to be checked.
  • subsumption_dict (dict) – Dictionary of subsumption hierarchies (indexed by the datatype URI-s).
  • subsumption_key (str) – Key in the dictionary, if None, the uri parameter is used.
  • subsumption_list (list) – List of subsumptions associated to a subsumption key (ie, all datatypes that are superclasses of the new datatype).
closure()

Generate the closure the graph. This is the real ‘core’.

The processing rules store new triples via the separate method Core.store_triple() which stores them in the added_triples array. If that array is empty at the end of a cycle, it means that the whole process can be stopped.

If required, the relevant axiomatic triples are added to the graph before processing in cycles. Similarly the exchange of literals against bnodes is also done in this step (and restored after all cycles are over).

empty_stored_triples()

Empty the internal store for triples.

extra_axioms = [(rdflib.term.URIRef('http://www.w3.org/2002/07/owl#hasSelf'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#Property')), (rdflib.term.URIRef('http://www.w3.org/2002/07/owl#hasSelf'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#domain'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#Property'))]
flush_stored_triples()

Send the stored triples to the graph, and empty the container.

full_binding_triples = [(rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Thing'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')), (rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Class'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Class')), (rdflib.term.URIRef('http://www.w3.org/2002/07/owl#DataRange'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Datatype'))]
one_time_rules()[source]

This method is invoked only once at the beginning, and prior of, the forward chaining process.

At present, only the L{subsumption} of restricted datatypes<_subsume_restricted_datatypes>} is performed.

post_process()

Do some post-processing step. This method when all processing is done, but before handling possible errors (I.e., the method can add its own error messages). By default, this method is empty, subclasses can add content to it by overriding it.

pre_process()

Do some pre-processing step. This method before anything else in the closure. By default, this method is empty, subclasses can add content to it by overriding it.

restriction_typing_check(v, t)[source]

Helping method to check whether a type statement is in line with a possible restriction. This method is invoked by rule “cls-avf” before setting a type on an allValuesFrom restriction.

The method is a placeholder at this level. It is typically implemented by subclasses for extra checks, e.g., for datatype facet checks.

Parameters:
  • v – the resource that is to be ‘typed’.
  • t – the targeted type (i.e., Class).
Returns:

Boolean.

Return type:

bool

rules(t, cycle_num)[source]

Go through the additional rules implemented by this module.

Parameters:
  • t (tuple) – A triple (in the form of a tuple).
  • cycle_num (int) – Which cycle are we in, starting with 1. This value is forwarded to all local rules; it is also used locally to collect the bnodes in the graph.
store_triple(t)

In contrast to its name, this does not yet add anything to the graph itself, it just stores the tuple in an internal set (Core.added_triples). (It is important for this to be a set: some of the rules in the various closures may generate the same tuples several times.) Before adding the tuple to the set, the method checks whether the tuple is in the final graph already (if yes, it is not added to the set).

The set itself is emptied at the start of every processing cycle; the triples are then effectively added to the graph at the end of such a cycle. If the set is actually empty at that point, this means that the cycle has not added any new triple, and the full processing can stop.

Parameters:t (tuple (s,p,o)) – The triple to be added to the graph, unless it is already there
class owlrl.OWLRLExtras.OWLRL_Extension_Trimming(graph, axioms, daxioms, rdfs=False)[source]

Bases: owlrl.OWLRLExtras.OWLRL_Extension

This Class adds only one feature to OWLRL_Extension: to initialize with a trimming flag set to True by default.

This is pretty experimental and probably contentious: this class removes a number of triples from the Graph at the very end of the processing steps. These triples are either the by-products of the deductive closure calculation or are axiom like triples that are added following the rules of OWL 2 RL. While these triples are necessary for the correct inference of really ‘useful’ triples, they may not be of interest for the application for the end result. The triples that are removed are of the form (following a SPARQL-like notation):

  • ?x owl:sameAs ?x, ?x rdfs:subClassOf ?x, ?x rdfs:subPropertyOf ?x, ?x owl:equivalentClass ?x type triples.
  • ?x rdfs:subClassOf rdf:Resource, ?x rdfs:subClassOf owl:Thing, ?x rdf:type rdf:Resource, owl:Nothing rdfs:subClassOf ?x type triples.
  • For a datatype that does not appear explicitly in a type assignments (ie, in a ?x rdf:type dt) the corresponding dt rdf:type owl:Datatype and dt rdf:type owl:DataRange triples, as well as the disjointness statements with other datatypes.
  • annotation property axioms.
  • a number of axiomatic triples on owl:Thing, owl:Nothing and rdf:Resource (eg, owl:Nothing rdf:type owl:Class, owl:Thing owl:equivalentClass rdf:Resource, etc).

Trimming is the only feature of this class, done in the post_process() step. If users extend OWLRL_Extension, this class can be safely mixed in via multiple inheritance.

Parameters:
  • graph (rdflib.Graph) – The RDF graph to be extended.
  • axioms (bool) – Whether (non-datatype) axiomatic triples should be added or not.
  • daxioms (bool) – Whether datatype axiomatic triples should be added or not.
  • rdfs (bool) – Whether RDFS extension is done.
add_axioms()

Add the OWLRL_Extension.extra_axioms, related to the self restrictions.

add_d_axioms()

This is not really complete, because it just uses the comparison possibilities that RDFLib provides.

add_error(message)

Add an error message

Parameters:message (str) – Error message.
static add_new_datatype(uri, conversion_function, datatype_list, subsumption_dict=None, subsumption_key=None, subsumption_list=None)

If an extension wants to add new datatypes, this method should be invoked at initialization time.

Parameters:
  • uri – URI for the new datatypes, like owl_ns[“Rational”].
  • conversion_function – A function converting the lexical representation of the datatype to a Python value, possibly raising an exception in case of unsuitable lexical form.
  • datatype_list (list) – List of datatypes already in use that has to be checked.
  • subsumption_dict (dict) – Dictionary of subsumption hierarchies (indexed by the datatype URI-s).
  • subsumption_key (str) – Key in the dictionary, if None, the uri parameter is used.
  • subsumption_list (list) – List of subsumptions associated to a subsumption key (ie, all datatypes that are superclasses of the new datatype).
closure()

Generate the closure the graph. This is the real ‘core’.

The processing rules store new triples via the separate method Core.store_triple() which stores them in the added_triples array. If that array is empty at the end of a cycle, it means that the whole process can be stopped.

If required, the relevant axiomatic triples are added to the graph before processing in cycles. Similarly the exchange of literals against bnodes is also done in this step (and restored after all cycles are over).

empty_stored_triples()

Empty the internal store for triples.

extra_axioms = [(rdflib.term.URIRef('http://www.w3.org/2002/07/owl#hasSelf'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#Property')), (rdflib.term.URIRef('http://www.w3.org/2002/07/owl#hasSelf'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#domain'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#Property'))]
flush_stored_triples()

Send the stored triples to the graph, and empty the container.

full_binding_triples = [(rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Thing'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')), (rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Class'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Class')), (rdflib.term.URIRef('http://www.w3.org/2002/07/owl#DataRange'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Datatype'))]
one_time_rules()

This method is invoked only once at the beginning, and prior of, the forward chaining process.

At present, only the L{subsumption} of restricted datatypes<_subsume_restricted_datatypes>} is performed.

post_process()[source]

Do some post-processing step performing the trimming of the graph. See the OWLRL_Extension_Trimming class for further details.

pre_process()

Do some pre-processing step. This method before anything else in the closure. By default, this method is empty, subclasses can add content to it by overriding it.

restriction_typing_check(v, t)

Helping method to check whether a type statement is in line with a possible restriction. This method is invoked by rule “cls-avf” before setting a type on an allValuesFrom restriction.

The method is a placeholder at this level. It is typically implemented by subclasses for extra checks, e.g., for datatype facet checks.

Parameters:
  • v – the resource that is to be ‘typed’.
  • t – the targeted type (i.e., Class).
Returns:

Boolean.

Return type:

bool

rules(t, cycle_num)

Go through the additional rules implemented by this module.

Parameters:
  • t (tuple) – A triple (in the form of a tuple).
  • cycle_num (int) – Which cycle are we in, starting with 1. This value is forwarded to all local rules; it is also used locally to collect the bnodes in the graph.
store_triple(t)

In contrast to its name, this does not yet add anything to the graph itself, it just stores the tuple in an internal set (Core.added_triples). (It is important for this to be a set: some of the rules in the various closures may generate the same tuples several times.) Before adding the tuple to the set, the method checks whether the tuple is in the final graph already (if yes, it is not added to the set).

The set itself is emptied at the start of every processing cycle; the triples are then effectively added to the graph at the end of such a cycle. If the set is actually empty at that point, this means that the cycle has not added any new triple, and the full processing can stop.

Parameters:t (tuple (s,p,o)) – The triple to be added to the graph, unless it is already there